华域联盟 Andriod Android使用AsyncTask加载图片的操作流程

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

加载图片基本操作

一、创建AsyncTask子类

  • 将ImageView的弱引用设置为成员变量,创建构造函数传入ImageView对象。
  • 调用指定大小解析Bitmap方法。
  • 因为是弱引用,所以必须判断引用是否被回收。如果异步任务完成前,用户离开Activity或者设置发生改变,ImageView也可能不存在。
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;
}

总结

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

本文由 华域联盟 原创撰写:华域联盟 » Android使用AsyncTask加载图片的操作流程

转载请保留出处和原文链接:https://www.cnhackhy.com/106300.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们