Android asynchronously loads Images

Source: Internet
Author: User

Package cn.com. mzba. Service;

Import java. Io. inputstream;
Import java. Lang. Ref. softreference;
Import java.net. url;
Import java. util. hashmap;

Import Android. Graphics. drawable. drawable;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. widget. imageview; Debt Recovery
/**
* Asynchronous loading and display of images
* @ Author kandanle
*
*/
Public class asyncimageloader {

/**
* Soft reference object: when responding to memory needs, the garbage collector determines whether to clear this object. Soft reference objects are most commonly used for memory-sensitive caching.
*/
Private hashmap <string, softreference <drawable> imagecache;

Public asyncimageloader (){
Imagecache = new hashmap <string, softreference <drawable> ();
}

Public drawable loaddrawable (final string imageurl, final imageview, final imagecallback ){
If (imagecache. containskey (imageurl )){
// Read everyone from the cache
Softreference <drawable> softreference = imagecache. Get (imageurl );
Drawable = softreference. Get ();
If (drawable! = NULL ){
Return drawable;
}
}
Final handler = new handler (){
@ Override
Public void handlemessage (Message MSG ){
Super. handlemessage (MSG );
Imagecallback. imageloaded (drawable) msg. OBJ, imageview, imageurl );
}
};
New thread (){
Public void run (){
Drawable = loadimagefromurl (imageurl );
Imagecache. Put (imageurl, new softreference <drawable> (drawable ));
Message message = handler. obtainmessage (0, drawable );
Handler. sendmessage (Message );
}
}. Start ();

Return NULL;
}

Public static drawable loadimagefromurl (string urlpath ){
URL;
Inputstream is = NULL;
Try {
Url = new URL (urlpath );
Is = (inputstream) URL. getcontent ();
} Catch (exception e ){
E. printstacktrace ();
}
Drawable = drawable. createfromstream (is, "src ");
Return drawable;
}

}

Package cn.com. mzba. Service;

Import Android. Graphics. drawable. drawable;
Import Android. widget. imageview;

Public interface imagecallback {
Public void imageloaded (drawable imagedrawable, imageview, string imageurl );
}

Drawable = asyncimageloader. loaddrawable (weiboinfo. getusericon (), viewusericon, new imagecallback (){
@ Override
Public void imageloaded (drawable imagedrawable, imageview, string imageurl ){
Imageview. setimagedrawable (imagedrawable );
}
});
If (drawable = NULL) {Wind Situation Java-JavaScript mogujie.com women's clothing
Viewusericon. setimageresource (R. drawable. Portrait );
} Else {
Viewusericon. setimagedrawable (drawable );
}

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