回收 ImageView 的 Bitmap [英] Recycle ImageView's Bitmap

查看:36
本文介绍了回收 ImageView 的 Bitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的事情:

Bitmap.Config conf = Bitmap.Config.ARGB_8888;
WeakReference<Bitmap> bm = new WeakReference<Bitmap>(Bitmap.createBitmap(3000 + 3000, 2000, conf));

Canvas canvas = new Canvas(bm.get());
canvas.drawBitmap(firstBitmap, 0, 0, null);
canvas.drawBitmap(bm, firstBitmap.getWidth(), 0, null);

imageView.setImageBitmap(bm);

我将其应用于 10 多个一一创建的 imageView.每当我创建新的 ImageView 时,我都想从第一个对象中回收 'bm' 对象,导致这段代码在那里,导致我的堆越来越大,然后抛出 OutOfMemoryError,所以我这样做:

And I apply this on more than 10 imageView's which are created one by one. Whenever I create new ImageView, I want to recycle the 'bm' object from the first one, cause this code up there, causes my heap to grow more and more and then throw OutOfMemoryError, so I do:

bm.recycle()

在我将 Bitmap (bm) 设置为 imageView 对象之后.这会导致 ImageView 的画布想要绘制回收的 Bitmap 的异常.

right after I set the Bitmap (bm) to the imageView object. This causes exception that the ImageView's canvas wants to draw recycled Bitmap.

在ImageView上回收一个已经作为图片的Bitmap有什么办法?

What is the way to recycle a Bitmap that has already been put as image on ImageView?

谢谢b

推荐答案

在你的 onDestroy 方法中,你可以尝试这样的事情:

In your onDestroy method you could try something like this:

ImageView imageView = (ImageView)findViewById(R.id.my_image);
Drawable drawable = imageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    Bitmap bitmap = bitmapDrawable.getBitmap();
    bitmap.recycle();
}

演员应该工作,因为 setImageBitmap 实现为

The cast should work since setImageBitmap is implemented as

public void setImageBitmap(Bitmap bm) {
    setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}

这篇关于回收 ImageView 的 Bitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆