Android Lazy load image from the Internet

Source: Internet
Author: User

/*deferred load characteristics *lazy load of images in Listview*i am using a ListView to display some images and captions associated with thos E images.*i am getting the images from the Internet. Is there a-to-lazy load the images so while the text displays,*the UI is not locked up and images are displayed as the Y is downloaded?**/Package Com.wilson.android.library;import java.io.IOException; Public classDrawablemanager {PrivateFinal Map<string, drawable>Drawablemap;  PublicDrawablemanager () {Drawablemap=NewHashmap<string, drawable>(); }     Publicdrawable fetchdrawable (String urlstring) {if(Drawablemap.containskey (urlstring)) {returnDrawablemap.Get(urlstring); } log.d ( This. GetClass (). Getsimplename (),"image URL:"+urlstring); Try{InputStream is=fetch (urlstring); Drawable drawable= Drawable.createfromstream ( is,"src"); if(Drawable! =NULL) {drawablemap.put (urlstring, drawable); LOG.D ( This. GetClass (). Getsimplename (),"got a thumbnail drawable:"+ drawable.getbounds () +", "+ drawable.getintrinsicheight () +","+ drawable.getintrinsicwidth () +", "+ drawable.getminimumheight () +","+drawable.getminimumwidth ()); } Else{LOG.W ( This. GetClass (). Getsimplename (),"could not get thumbnail"); }            returndrawable; } Catch(malformedurlexception e) {LOG.E ( This. GetClass (). Getsimplename (),"Fetchdrawable failed", E); return NULL; } Catch(IOException e) {LOG.E ( This. GetClass (). Getsimplename (),"Fetchdrawable failed", E); return NULL; }    }     Public voidfetchdrawableonthread (Final String urlstring, final ImageView ImageView) {if(Drawablemap.containskey (urlstring)) {imageview.setimagedrawable (drawablemap.Get(urlstring)); } Final Handler Handler=NewHandler () {@Override Public voidhandlemessage (Message message) {imageview.setimagedrawable ((drawable) message.obj);        }        }; Thread Thread=NewThread () {@Override Public voidrun () {//Todo:set ImageView to a "pending" imageDrawable drawable =fetchdrawable (urlstring); Message Message= Handler.obtainmessage (1, drawable);            Handler.sendmessage (message);        }        };    Thread.Start (); }    PrivateInputStream Fetch (String urlstring) throws Malformedurlexception, IOException {defaulthttpclient httpClient /c5>=Newdefaulthttpclient (); HttpGet Request=NewHttpGet (urlstring); HttpResponse Response=Httpclient.execute (Request); returnresponse.getentity (). getcontent (); }}

From Http://stackoverflow.com/questions/541966/lazy-load-of-images-in-listview

Android Lazy load image from the Internet

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.