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

加载图片基本操作

创新互联专业为企业提供奈曼网站建设、奈曼做网站、奈曼网站设计、奈曼网站制作等企业网站建设、网页设计与制作、奈曼企业网站模板建站服务,10多年奈曼做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

一、创建AsyncTask子类

  • 将ImageView的弱引用设置为成员变量,创建构造函数传入ImageView对象。
  • 调用指定大小解析Bitmap方法。
  • 因为是弱引用,所以必须判断引用是否被回收。如果异步任务完成前,用户离开Activity或者设置发生改变,ImageView也可能不存在。
class BitmapWorkerTask extends AsyncTask {
  private final WeakReference imageViewReference;
  private int data = 0;
  public BitmapWorkerTask(ImageView imageView) {
    // Use a WeakReference to ensure the ImageView can be garbage collected
    imageViewReference = new WeakReference(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 bitmapWorkerTaskReference;
  public AsyncDrawable(Resources res, Bitmap bitmap,
      BitmapWorkerTask bitmapWorkerTask) {
    super(res, bitmap);
    bitmapWorkerTaskReference =
      new WeakReference(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加载图片的操作流程
URL链接:http://myzitong.com/article/psiges.html