7 图像 下一张图片 Bitmap & assets
来源:互联网 发布:微信矩阵群 编辑:程序博客网 时间:2024/04/28 23:08
-----------------------------------------main.java---------------------
package com.example.sd;
import java.io.IOException;
import java.io.InputStream;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
String []images = null;
AssetManager assetManager = null;
int currentImage = 0;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
assetManager = getAssets();
//获取/assets/目录下的所有文件
try {
images = assetManager.list("");
} catch (IOException e) {
e.printStackTrace();
}
}
public void getNextOnclick(View view) {
//if发生数组越界
if(currentImage >= images.length) {
currentImage = 0;
}
//找到下一个图片文件
while(!images[currentImage].endsWith(".png")
&& !images[currentImage].endsWith(".jpg")
&& !images[currentImage].endsWith(".gif")) {
currentImage++;
//if已经发生数组越界
if(currentImage >= images.length) {
currentImage = 0;
}
}
InputStream inputStreamAssetFile = null;
try {
//打开指定资源对应的输入流
inputStreamAssetFile = assetManager.open(images[currentImage++]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
//如果图片还未回收,先强制回收该图片
if(bitmapDrawable != null && !bitmapDrawable.getBitmap().isRecycled()) {
bitmapDrawable.getBitmap().recycle();
}
//改变ImageView显示的图片
imageView.setImageBitmap(BitmapFactory.decodeStream(inputStreamAssetFile));
}
}
。。。。。。。。。。。。。。。。。main.xml。。。。。。。。。。。。。。。。。。。。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="next图片"
android:onClick="getNextOnclick"
/>
</LinearLayout>
- 7 图像 下一张图片 Bitmap & assets
- Bitmap存为一张图片
- android Bitmap拷贝一张图片
- 从Android读取Unity assets下的图片并保存为Bitmap格式
- Android中把assets目录下所有图片转换成bitmap存入list集合
- 图片上一张与下一张切换
- C#图片上一张下一张
- 浏览图片显示下一张
- 使用Bitmap画一张自定义文字的图片
- 用BitMap画出一张图片的某一部分
- 详细讲解把一张bitmap图片写到sd卡!
- 读取Assets文件夹下所有图片资源
- 读取assets目录下的图片
- Android显示assets目录下的图片
- as3 初步实现图片下一张下一张功能
- 显示图片实现上一张下一张功能
- 点击按钮查看上一张或下一张图片
- 一段图片左右箭头之上一张下一张
- 老龄化社会与移民问题
- POJ 3211 Washing Clothes
- 黑马程序员——12-C语言之预处理指令
- Repeated DNA Sequences (Java)
- Google hack 语法
- 7 图像 下一张图片 Bitmap & assets
- 临界区,互斥量,信号量,事件的区别
- easyui combobox post方式传参数到后台
- Linux信号实践(3) --信号内核表示
- HDU 1513 Palindrome
- 中国如何应对人口老龄化挑战
- regret
- random模块
- elasticsearch 配置ik分词器