android ImageView 频繁切换图片 OOM

最近在帮一个朋友写一个android 软件,频繁的用ImageView 更换图片。

结果常常出现OOM

后来修改把ImageView加载图片自己来管理,问题解决了,不敢独享,拿出来与各位同学分享


private void showImage(int index) {

int resid = R.drawable.circle_01;

switch (index) {

case 1:

resid = R.drawable.circle_01;

break;

case 2:

resid = R.drawable.circle_02;

break;

case 3:

resid = R.drawable.circle_03;

break;

case 4:

resid = R.drawable.circle_04;

break;

case 5:

resid = R.drawable.circle_05;

break;

case 6:

resid = R.drawable.circle_06;

break;

case 7:

resid = R.drawable.circle_07;

break;

case 8:

resid = R.drawable.circle_08;

break;

case 9:

resid = R.drawable.circle_09;

break;

case 10:

resid = R.drawable.circle_10;

break;

case 11:

resid = R.drawable.circle_11;

break;

case 12:

resid = R.drawable.circle_12;

break;

}

//把历史的ImageView 图片对象(image_)释放

BitmapDrawable bitmapDrawable = (BitmapDrawable)image_.getBackground();

if(bitmapDrawable!=null){

Bitmap hisBitmap = bitmapDrawable.getBitmap();

if(hisBitmap.isRecycled()==false)

{

hisBitmap.recycle();

}

}

//上面的代码一定要做,否则不会超过30分钟可定会出现exit,当然我的timer设置的时间比较短。。。。。timer.schedule(task, 5, 5);


//下面代码是网上通常搜到的源码例子,但是不能解决,智能优化,只有加上上面的才能彻底解决内存的问题。

BitmapFactory.Options options = new BitmapFactory.Options();

options.inPurgeable = true; // bitmap can be purged to disk

options.inInputShareable = true;

Bitmap bm = BitmapFactory.decodeStream(getResources().openRawResource(

resid));

Drawable bd = new BitmapDrawable(getResources(), bm);

image_.setBackgroundDrawable(bd);



}

你可能感兴趣的:(软件技术)