Android使用AsyncTask加载图片的操作流程

发布时间:2020-09-29 06:41:53 作者:timshinlee
来源:脚本之家 阅读:260

加载图片基本操作

一、创建AsyncTask子类

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);
      }
    }
  }
}

二、创建异步任务实例对象,开始执行

public void loadBitmap(int resId, ImageView imageView) {
  BitmapWorkerTask task = new BitmapWorkerTask(imageView);
  task.execute(resId);
}

处理并发

因为ListView和GridView复用子布局,无法保证异步任务完成时,相关的布局没有被回收。也无法保证异步任务完成时间的先后顺序。所以必须处理并发事件。

一、创建BitmapDrawable子类

该类专门用来保存Bitmap及其对应的异步任务

static class AsyncDrawable extends BitmapDrawable {
  private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
  public AsyncDrawable(Resources res, Bitmap bitmap,
      BitmapWorkerTask bitmapWorkerTask) {
    super(res, bitmap);
    bitmapWorkerTaskReference =
      new WeakReference<BitmapWorkerTask>(bitmapWorkerTask);
  }
  public BitmapWorkerTask getBitmapWorkerTask() {
    return bitmapWorkerTaskReference.get();
  }
}

二、绑定AsyncDrawable

创建AsyncDrawable并传入BitmapWorkerTask对象,imageView设置为该AsyncDrawable再开始异步任务

public void loadBitmap(int resId, ImageView imageView) {
  if (cancelPotentialWork(resId, imageView)) {
    final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
    final AsyncDrawable asyncDrawable =
        new AsyncDrawable(getResources(), mPlaceHolderBitmap, task);
    imageView.setImageDrawable(asyncDrawable);
    task.execute(resId);
  }
}

cancelPotentialWork这个方法用于调用方法获取控件对应异步任务,判断是否与当前任务一致

public static boolean cancelPotentialWork(int data, ImageView imageView) {
  final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
  if (bitmapWorkerTask != null) {
    final int bitmapData = bitmapWorkerTask.data;
    // If bitmapData is not yet set or it differs from the new data
    if (bitmapData == 0 || bitmapData != data) {
      // Cancel previous task
      bitmapWorkerTask.cancel(true);
    } else {
      // The same work is already in progress
      return false;
    }
  }
  // No task associated with the ImageView, or an existing task was cancelled
  return true;
}

这个getBitmapWorkerTask()方法用于获取图片对应异步任务

private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
  if (imageView != null) {
    final Drawable drawable = imageView.getDrawable();
    if (drawable instanceof AsyncDrawable) {
      final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
      return asyncDrawable.getBitmapWorkerTask();
    }
  }
  return null;
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对亿速云的支持。如果你想了解更多相关内容请查看下面相关链接

推荐阅读:
  1. android中的异步任务-----------AsyncTask的使用
  2. android AsyncTask介绍

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android asynctask 加载

上一篇:Android设计模式之单例模式解析

下一篇:解决SpringBoot2多线程无法注入的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》