国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > 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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河池市| 舞钢市| 铜山县| 高要市| 郎溪县| 乐陵市| 元氏县| 丰台区| 八宿县| 沙洋县| 黑龙江省| 甘南县| 镇坪县| 黑河市| 皋兰县| 城固县| 佛坪县| 百色市| 彩票| 福海县| 隆子县| 安乡县| 岳西县| 沁水县| 新宾| 常山县| 上蔡县| 青田县| 宜州市| 宁河县| 仁化县| 通道| 探索| 九台市| 杨浦区| 日照市| 安庆市| 柳州市| 黑龙江省| 湘乡市| 晋城|