Android image asynchronous loading class

Source: Internet
Author: User

Keywords: cache pool, loading queue

/*** Title: asynchronous image loading class * Description: * copyright: Copyright (c) 2012 * company: ** @ author tom_liang */package COM. zouba. DD. UI. util; import Java. io. ioexception; import Java. io. inputstream; import Java. io. serializable; import Java. lang. ref. softreference; import malformedurlexception; import URL; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; Im Port android. graphics. drawable. drawable; import android. OS. bundle; import android. OS. handler; import android. OS. message; public class dandanasynchimageloader {public static Boolean isstop = false; // whether to stop asynchronously loading the image thread private static object locker = new object (); // Synchronous lock Private Static thread loadthread = NULL; // asynchronously load the thread object Private Static Map <string, softreference <drawable> imagecache = new hashmap <string, softrefere NCE <drawable> (); // Private Static list of all image caches <asynchimage> taskqueue = new arraylist <asynchimage> (); // asynchronously load the task queue // message processor Private Static final handler = new handler () {public void handlemessage (message) {// after the current task is loaded into the image, execute callback bundle = message. getdata (); asynchimage = (asynchimage) bundle. getserializable ("handled_task"); asynchimage. getcallback (). imageloaded (drawable) Message. OBJ, asynchimage. getimgurl () ;}; static {dandanasynchimageloader. loadthread = new thread () {public void run () {While (! Dandanasynchimageloader. isstop) {synchronized (locker) {If (taskqueue. size () = 0) {try {locker. wait ();} catch (interruptedexception e) {e. printstacktrace () ;}} asynchimage temptask = taskqueue. remove (0); drawable = loadimagefromurl (temptask. getimgurl (); imagecache. put (temptask. getimgurl (), new softreference <drawable> (drawable); message = handler. obtainmessage (0, drawable); bundle = New bundle (); bundle. putserializable ("handled_task", temptask); message. setdata (bundle); handler. sendmessage (Message); locker. notifyall () ;}}}; // start asynchronous thread (network event thread) dandanasynchimageloader. loadthread. start ();}/*** the static method for loading images asynchronously. First, check whether there are previously loaded images in the cache. * If yes, return directly. No, add to asynchronous task queue for loading * @ Param imageurl URL for slice * @ Param imagecallback callback interface * @ return returns the drawable object of asynchronous images */public static drawable loaddrawable (final string I Mageurl, final imagecallback) {If (imagecache. containskey (imageurl) {softreference <drawable> softreference = imagecache. get (imageurl); drawable = softreference. get (); If (drawable! = NULL) {return drawable;} new thread () {public void run () {synchronized (locker) {If (! Taskqueue. contains (imageurl) {taskqueue. add (New asynchimage (imageurl, imagecallback);} locker. policyall ();}}}. start (); return NULL;}/*** network image loading callback interface * @ author zouba **/public interface imagecallback {public void imageloaded (drawable imagedrawable, string imageurl);}/*** description: real network image Loading Method * @ Param URL address of the network image * @ return returns the drawable object of the network image */Private Static drawable loadimagefromurl (string URL) {URL m; inputstream I = NULL; try {M = new URL (URL); I = (inputstream) M. getcontent ();} catch (malformedurlexception E1) {e1.printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} drawable d = drawable. createfromstream (I, "src"); Return D ;} /*************************************** **************************************** * ****** asynchronously load the image's Java Bean, simple encapsulation of asynchronous Image Information ********************************* **************************************** * **********/static class asynchimage implements serializable {Private Static final long serialversionuid = 1l; private string imgurl; private imagecallback callback; Public asynchimage (string imgurl, imagecallback callback) {This. imgurl = imgurl; this. callback = callback;} Public String getimgurl () {return imgurl;} public void setimgurl (string imgurl) {This. imgurl = imgurl;} public imagecallback getcallback () {return callback;} public void setcallback (imagecallback callback) {This. callback = callback ;}}}

Related Article

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: 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.