在應用程序開發中,ListView通常用于加載數據,其實在開發中非常有必要將圖片加載到另一個線程中,以便在執行后執行和更新UI線程,那么ListView異步加載圖片實現思路大家都清楚嗎?下面就讓武林技術頻道小編帶你一起來了解一下吧!
大致思路是這樣:
1.利用軟引用來緩存圖片Bitmap,用圖片的URL作為緩存查找的Key;
2.設兩級緩存,一級是SoftReference,二級是本地SD卡;
3.如果兩級緩存都沒取到圖片,則從服務器獲取,并加入緩存;
4.加載完后通過回調接口通知UI更新;
以下是異步加載的關鍵代碼,其中一些工具類沒有給出,自己實現就可以,比如HttpRequest是我自己寫的一個類。
復制代碼 代碼如下:
public class AsyncImageLoader {
//Cache for image(Type String is the URL of image,the second parameter is soft reference)
private HashMap> imageCache = null;
private Activity context;
public AsyncImageLoader(Activity context){
this.context = context;
imageCache = new HashMap>();
}
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 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);//獲取文件名
boolean isExist = SystemUtils.findPhotoFromSDCard(Constant.INFO_PATH, fileName);
if (isExist) {//是否在SD卡存在圖片
Bitmap bitmap = SystemUtils.getPhotoFromSDCard(Constant.INFO_PATH, fileName);
return bitmap;
}
}
final Handler myHandler = new Handler(){
@Override
public void handleMessage(Message 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);//這里是我自己寫的一個類,目的是通過URL地址從服務器獲取圖片輸入流
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));
//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);
}
} ,>,>
在ListView的adapter的getView方法中:
復制代碼 代碼如下:
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);
}
其中asyncImageLoader是在adapter的構造方法中初始化的,形成一個緩存。通過這個機制就可以實現ListView的圖片異步加載,在用戶體驗上比直接加載要感覺好很多,那樣會造成界面卡頓。這里是加載一張圖片的情況,如果ListView的item中的圖片是不定的,有可能是一張、兩張、三張,該用什么方式呢,大家可以思考一下,并可以一起討論一下,包括實現ListView滾動時不加載數據也是優化ListView加載的必要步驟。
以上代碼都是武林技術頻道小編為大家搜集的ListView異步加載圖片實現思路,希望對大家的學習有一定的幫助!