Operation flow of Android using AsyncTask to load pictures

  • 2021-10-25 07:58:26
  • OfStack

Basic operation of loading pictures

1. Create an AsyncTask subclass

Set the weak reference of ImageView as a member variable, and create a constructor to pass in the ImageView object. Invokes the specified size resolution Bitmap method. Because it is a weak reference, you must determine whether the reference is recycled. If the user leaves Activity or the settings change before the asynchronous task completes, ImageView may not exist.

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);
      }
    }
  }
}

2. Create an asynchronous task instance object and start executing


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

Handling concurrency

Because ListView and GridView multiplex sub-layouts, there is no guarantee that the associated layouts will not be recycled when the asynchronous task completes. There is no guarantee of the sequence of asynchronous task completion times. Concurrent events must be handled.

1. Create an BitmapDrawable subclass

This class is dedicated to saving Bitmap and its corresponding asynchronous tasks


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();
  }
}

2. Bind AsyncDrawable

Create an AsyncDrawable and pass in an BitmapWorkerTask object, set imageView to that AsyncDrawable before starting the asynchronous task


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 This method is used to call the method to obtain the asynchronous task corresponding to the control and judge whether it is corresponding to the current task 1


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;
}

This getBitmapWorkerTask () method is used to get the asynchronous task corresponding to the picture


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;
}

Summarize


Related articles: