使用asynctask的Gridview回收问题

时间:2013-03-21 23:23:22

标签: android android-view android-gridview recycle

当我在GridView中来回滚动时,我的图像无法正确回收,最终会在整个网格中显示相同的图像。

Recycling problem

适配器

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> mList;
    private int mheight;
    private int mwidth;
    private Bitmap nBitmap;

    public ImageAdapter(Context context, List<String> list, int height, int width) {
        mContext = context;
        mList = list;
        mheight = height;
        mwidth = width;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position).toString();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            InputStream is;
            try {
                is = mContext.getAssets().open(mList.get(position));
                Bitmap bm = BitmapFactory.decodeStream(is);
                Bitmap mBitmap = Bitmap.createScaledBitmap(bm, mwidth / 3, mwidth / 3, false);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

                this.nBitmap = mBitmap;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageBitmap(nBitmap);
        return imageView;
    }
}

我尝试解码Ui线程滚动的位图快速工作但Thumbnails在启动应用程序时逐个加载,并且回收的缩略图视图从orignal视图和低低的memoery设备应用程序崩溃,同时从AsyncTask加载缩略图更新了codde

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private List<String> mList;
private int mheight;
private int mwidth;
private InputStream is;

public ImageAdapter(Context context, List<String> list, int height, int width) {
    mContext = context;
    mList = list;
    mheight = height;
    mwidth = width;
}


@Override
public int getCount() {
    return mList.size();
}

@Override
public Object getItem(int position) {
    return mList.get(position).toString();
}

@Override
public long getItemId(int position) {
    return 0;
}





@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
    } else {
        imageView = (ImageView) convertView;
    }


    InputStream is;
    try {
        is = mContext.getAssets().open(mList.get(position));
        Loadimage task = new Loadimage(imageView , mheight , mwidth);
        task.execute(is);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return imageView ;

}
 public class Loadimage extends AsyncTask<InputStream, Void, Bitmap>{
private final WeakReference<ImageView> imageViewReference;


private InputStream is = null;
private int width;


public Loadimage(ImageView imageView, int mheight, int mwidth) {
     imageViewReference = new WeakReference<ImageView>(imageView);
     this.width=mwidth;

    // TODO Auto-generated constructor stub
}

@Override
protected Bitmap doInBackground(InputStream... params) {
    is = params[0];

    if (is !=null) {

        Bitmap bitmap  = BitmapFactory.decodeStream(is);
        Bitmap nBitmap =Bitmap.createScaledBitmap(bitmap,width/3 , width/3, false);
        return nBitmap;     
    }
    return null; 
  }
 @Override
 protected void onPostExecute(Bitmap bitmap) {
    if (imageViewReference != null && bitmap != null) {
        final ImageView imageView = imageViewReference.get();
        if (imageView != null) {
            imageView.setImageBitmap(bitmap);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }


}

}

1 个答案:

答案 0 :(得分:3)

您需要移动在if/then之外生成位图的块。现在,您只在convertView == null时生成新的位图。

我相信你的代码是正确的:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> mList;
    private int mheight;
    private int mwidth;

    public ImageAdapter(Context context, List<String> list, int height, int width) {
        mContext = context;
        mList = list;
        mheight = height;
        mwidth = width;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position).toString();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
        } else {
            imageView = (ImageView) convertView;
        }

        InputStream is;
        try {
            is = mContext.getAssets().open(mList.get(position));
            Bitmap bm = BitmapFactory.decodeStream(is);
            Bitmap bitmap = Bitmap.createScaledBitmap(bm, mwidth / 3, mwidth / 3, false);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return imageView;
    }
}

你的滚动不是很流畅,你会得到很多丢帧,因为你的Bitmap解码代码效率低(没有缓存),而且这一切都发生在UI线程上,这对响应性有害。当前代码中的瓶颈不是ImageView的分配和垃圾收集。

瓶颈是(可能是到目前为止)你处理和创建Bitmap s。

您可以查看以下链接以获取建议:

Displaying Bitmaps Efficiently

特别是这一个:

Processing Bitmaps Off the UI Thread