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 );
}
}
}