溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android使用AsyncTask加載圖片的操作流程

發(fā)布時(shí)間:2020-09-29 06:41:53 來源:腳本之家 閱讀:260 作者:timshinlee 欄目:移動(dòng)開發(fā)

加載圖片基本操作

一、創(chuàng)建AsyncTask子類

  • 將ImageView的弱引用設(shè)置為成員變量,創(chuàng)建構(gòu)造函數(shù)傳入ImageView對(duì)象。
  • 調(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í)例對(duì)象,開始執(zhí)行

public void loadBitmap(int resId, ImageView imageView) {
  BitmapWorkerTask task = new BitmapWorkerTask(imageView);
  task.execute(resId);
}

處理并發(fā)

因?yàn)長(zhǎng)istView和GridView復(fù)用子布局,無(wú)法保證異步任務(wù)完成時(shí),相關(guān)的布局沒有被回收。也無(wú)法保證異步任務(wù)完成時(shí)間的先后順序。所以必須處理并發(fā)事件。

一、創(chuàng)建BitmapDrawable子類

該類專門用來保存Bitmap及其對(duì)應(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對(duì)象,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)用方法獲取控件對(duì)應(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()方法用于獲取圖片對(duì)應(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é)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)億速云的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI