Android listview uses multithreading to asynchronously load images and Update views

Source: Internet
Author: User

I. asynchronous imageview loading class

Package COM. busap. netutils; import Java. lang. ref. softreference; import Java. util. hashmap; import Java. util. linkedhashmap; import Java. util. map. entry; import android. graphics. bitmap; import android. OS. handler; import android. util. log; import android. widget. imageview;/*** use multiple threads to asynchronously load images and update the view ** @ author Lizhen **/public final class asynimageloader {private loaderthread thread; // The thread private hashm that loads the image and sends a message to the update Interface AP <string, softreference <bitmap> imagecache; // image object cache, key: urlprivate handler of the image; // handler object public asynimageloader (handler) of the Interface Activity) {imagecache = new hashmap <string, softreference <bitmap> (); this. handler = handler;}/*** display the image in the specified imageview before loading, the image URL is saved in the tag of the view object ** @ Param imageview * view of the image to be displayed * @ Param defaultbitmap * load the default image object to be loaded */Public void loadbitmap (imageview Image View, bitmap defaultbitmap) {// URL of the image. This value may be changed to string url = (string) imageview during image loading. gettag (); If (imagecache. containskey (URL) {// determines whether softreference <bitmap> softreference = imagecache exists in the cache. get (URL); Bitmap bitmap = softreference. get (); If (Bitmap! = NULL) {// if the image object is not empty, you can mount and update the view and return the imageview. setimagebitmap (Bitmap); return;} else {// if it is empty, delete it from the cache (its bitmap object has been recycled and released and needs to be reloaded) log. E ("tag", "cache bitmap is null"); imagecache. remove (URL) ;}} imageview. setimagebitmap (defaultbitmap); // first, a prompt is displayed indicating that the image is being loaded if (thread = NULL) {// The loading thread does not exist and the thread has not started yet, you need to create a thread and start thread = new loaderthread (imageview, URL); thread. start ();} else {// If so, call the thread object to load the thread. load (imageview, URL) ;}/*** Release all bitmap objects in the cache and clear the cache */Public void releasebitmapcache () {If (imagecache! = NULL) {for (Entry <string, softreference <bitmap> entry: imagecache. entryset () {Bitmap bitmap = entry. getvalue (). get (); If (Bitmap! = NULL) {bitmap. recycle (); // release the bitmap object} imagecache. clear () ;}}/*** thread for loading and displaying images */private class loaderthread extends thread {javashashmap <string, imageview> mtaskmap; // The task chain private Boolean miswait of the Image view object to be loaded and displayed; // identifies whether the thread is in the waiting state public loaderthread (imageview, string URL) {mtaskmap = new linkedhashmap <string, imageview> (); mtaskmap. put (URL, imageview);}/*** process the update display of a view ** @ Param imageview */Public void load (imageview, string URL) {mtaskmap. remove (imageview); // It may be available in the task chain. You must delete mtaskmap first. put (URL, imageview); // Add it to the task if (miswait) {// This must be synchronized when the thread is waiting to wake up the thread to process the synchronized (this) of the task to be processed in the task queue {// This must be synchronized when calling the notify () of the object. policy () ;}}@ overridepublic void run () {While (mtaskmap. size ()> 0) {// when there is data in the queue, the thread will always run. once it enters, it must ensure that it will not jump out of the loop miswait = false; final string url = mtaskmap. keyset (). iterator (). next (); Final imageview = mtaskmap. remove (URL); If (imageview. gettag () = URL) {// determine whether the view is reused (once the imageview is reused, its tag value will be changed) Final Bitmap bitmap = myconnection. getbitmapbyurl (URL); // This method should load try {thread from the network or SD card. sleep (1000); // simulate the time when the network loads data} catch (interruptedexception E1) {e1.printstacktrace () ;}// put the loaded image into the cached map imagecache. put (URL, new softreference <bitmap> (Bitmap); If (url = imageview. gettag () {// judge whether the view has reused handler. post (New runnable () {// update the UI @ overridepublic void run () {imageview in the main thread through the message mechanism. setimagebitmap (Bitmap) ;}}}}if (mtaskmap. isempty () {// when there are no tasks to be processed in the task queue, the thread enters the waiting state try {miswait = true; // identifies the thread status, which must be in wait () synchronized (this) {This. wait (); // The guaranteed-performance thread enters the waiting state until a new task is added.} catch (interruptedexception e) {e. printstacktrace ();}}}}}}

Ii. Use

Handler handler;AsynImageLoader mImageAsynLoader;mImageAsynLoader.loadBitmap(discount_icon,defaultBitmap);

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.