Android asynchronously loads images (1) how to update the interface after downloading images

Source: Internet
Author: User

Android asynchronously loads images (1) how to update the interface after downloading images
Scenario: when loading an image, ImageView first sets a placeHolder, and then starts AsyncTask to load the appropriate image. How can I update the ImageView after the image is loaded successfully? Generally, there are two processing methods: the first is to pass the ImageView to AsyncTask. After the task is executed, the ImageView directly sets the bitmap. The second is that after the Task is executed, store Bitmap in the cache, notify the main thread of the view, and update the data. Passing ImageView to AsyncTask requires that ListView or GridView reuse ImageView. For example, if ImageView4 with pos 4 enables an AsyncTask, you can scroll to the position where pos is 8 and reuse ImageView4, even if AsyncTask is updated, ImageView4 cannot be updated. AsyncTask cannot prevent ImageView memory from being released. For example, when a user leaves this page, the interface should be destroyed. AsyncTask should avoid Memory leakage. After AsyncTask is executed, it should be able to get the ImageView, before setting Bitmap for ImageView, you must determine that the implementation of BitmapWorkerTask must be one-to-one correspondence between ImageView and Drawable and Task, and AsyncTask inherits AsyncTask and references ImageView. In this way, AsyncTask can obtain ImageView to update Bitmap, it does not prevent ImageView from being released. Each time, a placeHolder AsyncDrawable is set, and AsyncTask is weakly referenced in AsyncDrawable. After AsyncTask is executed, you can get Bitma through AsyncDrawable through ImageView get AsyncDrawable PTask: determines whether the task is the same task. If yes, you can determine whether the ImageView, AsyncDrawable, and AsyncTask correspond One to One. data can be updated. In addition, after the AsyncTask is fully executed, it can also be released directly. AsyncDrawable will not prevent AsyncTask from being released. After the Task is executed, the Bitmap will be stored in the cache and the view of the main thread will be notified for data update. Disadvantages: blind refresh, the efficiency is too low, especially the ImageView is very large, and AsyncTask is executed very quickly, so the refresh frequency will be extremely high, which will cause the page to be refreshed continuously, or even stuck to summarize ImageView, BitmapWorkerTask, the AsyncDrawable relationship BitmapWorkerTask holds a WeakReference imageViewReference, which is a weak reference ImageView and is used for asynchronous processing of image loading tasks. AsyncDrawable cleverly references the weak reference WeakReference bitmapWorkerTaskReference, and AsyncDrawable inherits from BitmapDrawable, so that ImageView can be used to weak reference BitmapWorkerTask, ImageView in setImageBitmap (AsyncDrawable. getDrawable can also obtain AsyncDrawable, and then obtain the key code of BitmapWorkerTask protected static class AsyncDrawable extends BitmapDrawable {private final WeakReference <BitmapWorkerTask> bitmapWorkerTaskReference; public AsyncDrawable Res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {super (res, bitmap); priority = new WeakReference <BitmapWorkerTask> (bitmapWorkerTask);} public BitmapWorkerTask timeout () {return bitmapWorkerTaskReference. get () ;}} protected class BitmapWorkerTask extends AsyncTask <Object, Void, BitmapDrawable> {private Object data; private final WeakReference <ImageView> im AgeViewReference; public BitmapWorkerTask (ImageView imageView) {imageViewReference = new WeakReference <ImageView> (imageView);} final AsyncDrawable asyncDrawable = new AsyncDrawable (mResources, map, task); imageView. setImageDrawable (asyncDrawable); protected void onPostExecute (BitmapDrawable value) {final ImageView imageView = getAttachedImageView (); if (value! = Null & imageView! = Null) {imageView. setImageDrawable (value); afterGetBitmap (imageView, value );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.