Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> ListView異步加載圖片實現思路

ListView異步加載圖片實現思路

編輯:Android開發實例

在應用開發中,經常用到ListView去加載數據,加載圖片和文字是比較常見的,文字還好,圖片從網絡請求加載速度比較慢,所以需要把圖片的加載放到另一個線程中去執行,執行完了再更新UI線程。以下列出一個我在項目中使用到的異步加載圖片的解決方案,代碼沒有上全,給出核心部分。

大致思路是這樣
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<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);//獲取文件名
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>(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加載的必要步驟。
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved