ListView Asynchronous Load Picture Realization idea _android

Source: Internet
Author: User
Tags stub
In the application development, often use to ListView to load data, loading pictures and text is more common, the text is OK, the picture from the network request loading speed is slow, so need to put the picture loading into another thread to execute, after the implementation of the UI thread update. Here is a solution to the asynchronous loading picture I used in my project, the code is not on the whole, giving the core part.

The general idea is this
1. Use soft references to cache picture bitmap, using the URL of the image as the key to cache lookup;
2. Set level two cache, level is SoftReference, two is the local SD card;
3. If the level two cache is not taken to the picture, it is fetched from the server and added to the cache;
4. Notify UI update via callback interface after loading;

Here are some of the key code that is loaded asynchronously, some of which are not given and can be implemented on your own, such as HttpRequest, a class I wrote myself.
Copy Code code as follows:

public class Asyncimageloader {
Cache for image (Type String are the URL of image,the second parameter is soft reference)
Private hashmap<string, softreference<bitmap>> imagecache = null;
Private activity context;
Public Asyncimageloader (activity context) {
This.context = context;
Imagecache = new hashmap<string, softreference<bitmap>> ();
}
Public Bitmap LoadImage (final imageview imageview,final String imageurl,final imagecallback imagecallback) {
If The cache contains the reference of bitmap then return
if (Imagecache.containskey (ImageURL)) {
softreference<bitmap> bitmapreference = Imagecache.get (ImageURL);
Bitmap Bitmap = Bitmapreference.get ();
if (bitmap!= null) {
return bitmap;
}
}
Second Cache,search Local SD card
else {
String filename = stringutil.namepicture (ImageURL);//Get filename
Boolean isexist = Systemutils.findphotofromsdcard (Constant.info_path, fileName);
if (isexist) {//whether a picture exists on the SD card
Bitmap Bitmap = Systemutils.getphotofromsdcard (Constant.info_path, fileName);
return bitmap;
}
}
Final Handler MyHandler = new Handler () {
@Override
public void Handlemessage (msg)
{
Imagecallback.setimage (ImageView, (Bitmap) msg.obj);
}
};
If the bitmap not exists in cache or SD card,then get it from net
New Thread () {
@Override
public void Run () {
TODO auto-generated Method Stub
Boolean isnetwork = systemutils.checknetwork (context);
if (isnetwork) {
InputStream photostream = Httprequest.getimagestream (ImageURL);//This is a class I wrote myself to get the picture input stream from the server through the URL address
Bitmap Bitmap;
try {
Bitmap = Imagetools.getresizebitmap (photostream, 128, 128);
if (bitmap!= null) {
String fileName = stringutil.namepicture (ImageURL);
Save image to SD card
Systemutils.savephototosdcard (Bitmap, FileName, Constant.info_path);
Put soft reference to cache
Imagecache.put (ImageURL, New softreference<bitmap> (Bitmap));
Send Message to update UI
Message message = Myhandler.obtainmessage (0, bitmap);
Myhandler.sendmessage (message);
}
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}.start ();
return null;
}
/**
* Interface for load image
* @author Ryan
*
*/
public interface imagecallback{
Set image for ImageView through bitmap
public void SetImage (ImageView imageview,bitmap Bitmap);
}
}

In the GetView method of the ListView adapter:
Copy Code code as follows:

Bitmap Bitmap1 = Asyncimageloader.loadimage (Viewholder.imageview1, URL1, New Imagecallback () {
@Override
public void SetImage (ImageView imageview, Bitmap Bitmap) {
TODO auto-generated Method Stub
Imageview.setimagebitmap (bitmap);
}
});
if (bitmap1!= null) {
ViewHolder.imageView1.setImageBitmap (BITMAP1);
}else {
ViewHolder.imageView1.setImageResource (R.DRAWABLE.IMAGE_BG);
}

Where Asyncimageloader is initialized in the adapter construction method to form a cache. Through this mechanism can realize ListView picture asynchronous loading, in the user experience than direct loading to feel much better, that will create interface cotton. Here is to load a picture of the case, if ListView's item in the picture is uncertain, there may be a, two, three, what the way, we can think about, and can discuss together, including implementing ListView scrolling without loading data is also a necessary step in optimizing ListView loading.

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.