Android asynchronous image loading instance code

Source: Internet
Author: User

The main process of Asynchronously loading images is to determine whether there is an image in the cache. If there is an image in the cache, it will be returned directly. If not, it will be downloaded and cached.

The following is an asynchronous download class:

Copy codeThe Code is as follows :/**
* User: Tom
* Date: 13-5-13
* Time: PM
*/
Public class AsnycImageLoader {

// Define a HashMap to store the cached Image key as String Value as a weak referenced resource file
// Image to facilitate JAVA Collection
Private Map <String, SoftReference <Drawable> imageCache = null;
Public AsnycImageLoader (){
ImageCache = new HashMap <String, SoftReference <Drawable> ();
}

/**
* Loading Images
* <P> imageurl is the URL of the downloaded resource,
* ImageCallback: downloads images online when no related images exist in the cache.
* In multi-thread scenarios, Handler must be used to update the UI by using the callback interface.
* </P>
* @ Param imageUrl
* @ Param callback
* @ Return
*/
Public Drawable loadDrawable (final String imageUrl, final ImageCallback callback ){
// Determine whether a cached image exists in ImageCache
If (imageCache. containsKey (imageUrl )){
SoftReference <Drawable> softReference = imageCache. get (imageUrl );
If (softReference. get ()! = Null ){
Return softReference. get ();
}
}
// Define the Handler of the Operation UI
Final Handler handler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
Callback. imageLoaded (Drawable) msg. obj );
}
};

New Thread (new Runnable (){
@ Override
Public void run (){
Drawable drawable = loadImageFromUrl (imageUrl );
ImageCache. put (imageUrl, new SoftReference <Drawable> (drawable ));
Message message = handler. obtainMessage (0, drawable );
Handler. sendMessage (message );
}
}). Start ();
Return null;
}

// Obtain resources based on the URL
Protected Drawable loadImageFromUrl (String imageUrl ){
Try {
Return Drawable. createFromStream (new URL (imageUrl). openStream (), "src ");
} Catch (Exception e ){
Throw new RuntimeException ();
}
}

// Callback Interface
Public interface ImageCallback {
Public abstract void imageLoaded (Drawable drawable );
}
}

Main Activity:

Copy codeThe Code is as follows :/**
* User: Tom
* Date: 13-5-13
* Time: PM
*/
Public class LoadImage extends Activity {
Private AsnycImageLoader loader = null;

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. loadimages );

Loader = new AsnycImageLoader ();

LoadImage ("http://www.jb51.net/images/icon-partners.png", R. id. image1 );
LoadImage ("http://www.jb51.net/images/icon-dev.png", R. id. image2 );
LoadImage ("http://pic28.jb51.net/20130421/12302174_231210305323_2.jpg", R. id. image3 );

}

Public void loadImage (String url, int id ){
Final ImageView imageView = (ImageView) findViewById (id );
Drawable cacheImage = loader. loadDrawable (url, new AsnycImageLoader. ImageCallback (){
@ Override
Public void imageLoaded (Drawable drawable ){
ImageView. setImageDrawable (drawable );
}
});
If (cacheImage! = Null ){
ImageView. setImageDrawable (cacheImage );
}
}
}

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.