欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-11-08 14:31:15
加载图片基本操作 一、创建asynctask子类 将imageview的弱引用设置为成员变量,创建构造函数传入imageview对象。 调用指定大小解析b...

加载图片基本操作

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

总结

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