首页 > 系统 > Android > 正文

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

2019-10-21 21:33:21
字体:
来源:转载
供稿:网友

加载图片基本操作

一、创建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;}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对VEVB武林网的支持。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表