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

加载图片基本操作

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

}

总结

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值