在非UI线程中处理Bitmap

译文出自谷歌安卓官网

BitmapFactory.decode*方法不能在UI线程中执行。因为加载这些图片所花费的时间是不可预知的,取决于各种各样的因素(从磁盘或网络上的加载速度,图片的大小,CPU的性能等等)。如果图片加载任务阻塞了UI线程,系统会为你的应用程序标记一个ANR,并且你可以选择关闭你的应用程序。

本课程引导你进入使用AsyncTask作为背景线程来处理图片加载任务,并且向你展示如何处理并发问题


使用AsyncTask

这个AsyncTask类提供了一个背景执行工作和将执行结果发布到UI线程中。要想使用AsyncTask,你得创建一个基类并重写其方法。

如下使用AsyncTask将图片加载到ImageView中,并且使用了decodeSampledBitmapFromResource()的例子

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;
    private int data = 0;

    public BitmapWorkerTask(ImageView imageView) {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(Integer... params) {
        data = params[0];
        return decodeSampledBitmapFromResource(getResources(), data, 100, 100));
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

这个对ImageView的WeakReference确保AsyncTask不会阻碍ImageView被GC回收。当任务结束的时候,无法保证ImageView仍然在当前任务中,因此在onPostExecute( )中你必须检查ImageView的索引。例如,当用户从这个界面离开或者任务结束前配置发生了改变,这些会导致ImageView不存在,

开始异步加载Bitmap,并执行它

public void loadBitmap(int resId, ImageView imageView) {
    BitmapWorkerTask task = new BitmapWorkerTask(imageView);
    task.execute(resId);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值