如何知道是否可以从位图或资源中绘制图像视图

时间:2013-11-27 06:19:31

标签: android bitmap imageview

ImageView iv;
Drawable d = imageView.getDrawable();
if (d instanceof BitmapDrawable) {
    if (d is not resources) // How to check ??? iv.setImageResources(resId);
        ((BitmapDrawable) d).getBitmap().recycle();
}

我想回收位图,除了资源drawable ...

1 个答案:

答案 0 :(得分:1)

你不能那样检查......但这种方法可以帮助你...

    private void setImage(Drawable drawable,boolean isFromResource) {
        imageView.setImageDrawable(drawable);
        imageView.setTag(isFromResource);
    }

    private void recycle() {
        Object object = imageView.getTag();
        if(object != null && object instanceof Boolean) {
            boolean isFromResource = (Boolean) object;
            if(!isFromResource) {
                Drawable drawable = imageView.getDrawable();
                if(drawable != null && drawable instanceof BitmapDrawable) {
                    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
                    Bitmap bitmap = bitmapDrawable.getBitmap();
                    bitmap.recycle();
                    imageView.setImageDrawable(setToNewDrawable);
                }
            }
        }
    }