图形图像处理demo----drawable,bitmap,bitfactory的使用

简单使用图片,一个button,一个imageView,单击button后,在imageView中显示下一张图片,图片存放在assets中,源代码如下:

public class BitmapTestActivity extends Activity {
String[] images = null;
AssetManager assets = null;
int currentImage = 0;
ImageView imageView = null;
Button showBtn = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 图片显示区域
imageView = (ImageView) findViewById(R.id.image);
// 显示下一张图片按钮
showBtn = (Button) findViewById(R.id.showBtn);
try {
// 获得assets下的所有资源
assets = getAssets();
images = assets.list("");
} catch (Exception e) {
e.printStackTrace();
}
showBtn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// 如果越界则从第一张图片开始
if (currentImage >= images.length) {
currentImage = 0;
}
// 找到下一张要显示的图片
while (!images[currentImage].endsWith(".png") && !images[currentImage].endsWith(".jpg")
&& !images[currentImage].endsWith(".gif")) {
currentImage++;
if (currentImage >= images.length) {
currentImage = 0;
}
}
InputStream assetFile=null;
try {
//打开制定资源对应的输入流
assetFile=assets.open(images[currentImage++]);
} catch (Exception e) {
e.printStackTrace();
}
BitmapDrawable bitmapDrawable=(BitmapDrawable)imageView.getDrawable();
if(bitmapDrawable!=null&&!bitmapDrawable.getBitmap().isRecycled()){
bitmapDrawable.getBitmap().recycle();
}
//改变显示图片
imageView.setImageBitmap(BitmapFactory.decodeStream(assetFile));
}
});
}
}

你可能感兴趣的:(drawable)