画布:尝试使用回收的位图错误 [英] Canvas: Trying to use a recycled bitmap error

查看:74
本文介绍了画布:尝试使用回收的位图错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个相当新的程序员,所以任何建议都将不胜感激.我有一个在每次调用时都在循环中运行AsyncTask的类.AsyncTask看起来像这样:

I'm a fairly new programmer so any advice would be appreciated. I have a class that runs an AsyncTask in a loop everytime it is called. The AsyncTask looks like this:

public class LoadImageTask extends AsyncTask<Void, Void, Void> {

    Context c;
    ViewHolder vh;
    public Bitmap bm;
    ViewGroup container;
    LinearLayout layout;

    public LoadImageTask(Context c, ViewHolder vh, ViewGroup container, 
    LinearLayout linlay) {

        this.vh = vh;
        this.c = c;
        this.container = container;
        this.layout = linlay;
    }

    protected Void doInBackground(Void... params) {
        Tools tools = new Tools();
        this.bm = tools.getAlbumart(this.c, vh.albumID);
        return null;
    }

    protected void onPostExecute(Void param) {
        vh.iv.setImageBitmap(this.bm);
        this.layout.addView(this.container);
        if (bm!=null) {
            bm.recycle();
        }

    }
}

ViewHolder vh -包含2个文本视图和1个Imageview的类

ViewHolder vh - class that holds 2 textviews and an Imageview

ViewGroup容器-用于使我制作的xml设计膨胀的容器(带有2个textview和一个ImageView)

ViewGroup container - the container being used to inflate an xml design i've made (with 2 textviews and an ImageView)

LinearLayout linlay -我要向其中添加容器的滚动视图布局(以根据该视图中所需的元素数量来扩展它)

LinearLayout linlay - a scrollview layout that I am adding the container to (to expand it based on the amount of elements I want in that view)

我创建的位图使用了太多的内存,因此我想回收它们,但是每次在将容器添加到布局后尝试回收它们时,它说我正在尝试使用回收的位图(当我在将容器添加到屏幕后清楚地调用回收站时)...在这一点上,我很困惑任何帮助都很好.

The bitmaps that I create as using way too much memory, so I want to recycle them, but every time I try to recycle them after I add the container to the layout, it says I am trying to use a recycled bitmap (when I'm clearly calling recycle after adding the container to the screen)... I'm stumped at this point. Any help would be nice.

推荐答案

  1. 如果位图使用了过多的内存,则应尝试将其缩小!(例如:256x256的Albumart是不错的选择)

  1. If bitmap using too much memory you should try to scale it down! (Ex: Albumart with 256x256 is good choice)

如果您的应用是在Android 3.0及更高版本上运行的,则无需调用recycle(),因为GC会很好地处理它.

If you run your app on Android 3.0 and above, it's not needed to call recycle() as the GC will take care of it perfectly.

请参阅PärNils Amsen的答案.

See Pär Nils Amsen answer.

这篇关于画布:尝试使用回收的位图错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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