Android使用AsyncTask加載圖片的操作流程
加載圖片基本操作
一、創(chuàng)建AsyncTask子類
- 將ImageView的弱引用設(shè)置為成員變量,創(chuàng)建構(gòu)造函數(shù)傳入ImageView對象。
- 調(diào)用指定大小解析Bitmap方法。
- 因?yàn)槭侨跻?,所以必須判斷引用是否被回收。如果異步任?wù)完成前,用戶離開Activity或者設(shè)置發(fā)生改變,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);
}
}
}
}
二、創(chuàng)建異步任務(wù)實(shí)例對象,開始執(zhí)行
public void loadBitmap(int resId, ImageView imageView) {
BitmapWorkerTask task = new BitmapWorkerTask(imageView);
task.execute(resId);
}
處理并發(fā)
因?yàn)長istView和GridView復(fù)用子布局,無法保證異步任務(wù)完成時(shí),相關(guān)的布局沒有被回收。也無法保證異步任務(wù)完成時(shí)間的先后順序。所以必須處理并發(fā)事件。
一、創(chuàng)建BitmapDrawable子類
該類專門用來保存Bitmap及其對應(yīng)的異步任務(wù)
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
創(chuàng)建AsyncDrawable并傳入BitmapWorkerTask對象,imageView設(shè)置為該AsyncDrawable再開始異步任務(wù)
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這個(gè)方法用于調(diào)用方法獲取控件對應(yīng)異步任務(wù),判斷是否與當(dāng)前任務(wù)一致
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;
}
這個(gè)getBitmapWorkerTask()方法用于獲取圖片對應(yīng)異步任務(wù)
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;
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
Android編程實(shí)現(xiàn)基于BitMap獲得圖片像素?cái)?shù)據(jù)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)基于BitMap獲得圖片像素?cái)?shù)據(jù)的方法,對比分析了兩種獲取圖片像素的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android開發(fā)使用Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能
這篇文章主要介紹了Android開發(fā)使用Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能,結(jié)合實(shí)例形式分析了Android基于Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
Android 如何攔截用戶頻繁操作(點(diǎn)擊事件)
本文主要介紹了Android 如何攔截用戶頻繁操作,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
詳解Android TabHost的多種實(shí)現(xiàn)方法 附源碼下載
這篇文章主要為大家詳細(xì)介紹了Android TabHost的多種實(shí)現(xiàn)方法 文章中針對每一種實(shí)現(xiàn)方法都附有源碼進(jìn)行下載,感興趣的小伙伴們可以參考一下2016-05-05
Android MVP模式ListView中嵌入checkBox的使用方法
這篇文章主要介紹了Android MVP模式ListView中嵌入checkBox的使用方法,如何在ListView中嵌入checkBox配合使用,感興趣的小伙伴們可以參考一下2016-08-08
解決EditText不顯示光標(biāo)的三種方法(總結(jié))
下面小編就為大家?guī)硪黄鉀QEditText不顯示光標(biāo)的三種方法(總結(jié))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04

