ListView異步加載圖片實現(xiàn)思路
更新時間:2013年04月03日 14:33:02 作者:
用到ListView去加載數(shù)據(jù),加載圖片和文字是比較常見的,文字還好,圖片從網(wǎng)絡(luò)請求加載速度比較慢,以下是一個我在項目中使用到的異步加載圖片的解決方案
在應(yīng)用開發(fā)中,經(jīng)常用到ListView去加載數(shù)據(jù),加載圖片和文字是比較常見的,文字還好,圖片從網(wǎng)絡(luò)請求加載速度比較慢,所以需要把圖片的加載放到另一個線程中去執(zhí)行,執(zhí)行完了再更新UI線程。以下列出一個我在項目中使用到的異步加載圖片的解決方案,代碼沒有上全,給出核心部分。
大致思路是這樣:
1.利用軟引用來緩存圖片Bitmap,用圖片的URL作為緩存查找的Key;
2.設(shè)兩級緩存,一級是SoftReference,二級是本地SD卡;
3.如果兩級緩存都沒取到圖片,則從服務(wù)器獲取,并加入緩存;
4.加載完后通過回調(diào)接口通知UI更新;
以下是異步加載的關(guān)鍵代碼,其中一些工具類沒有給出,自己實現(xiàn)就可以,比如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地址從服務(wù)器獲取圖片輸入流
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的構(gòu)造方法中初始化的,形成一個緩存。通過這個機(jī)制就可以實現(xiàn)ListView的圖片異步加載,在用戶體驗上比直接加載要感覺好很多,那樣會造成界面卡頓。這里是加載一張圖片的情況,如果ListView的item中的圖片是不定的,有可能是一張、兩張、三張,該用什么方式呢,大家可以思考一下,并可以一起討論一下,包括實現(xiàn)ListView滾動時不加載數(shù)據(jù)也是優(yōu)化ListView加載的必要步驟。
大致思路是這樣:
1.利用軟引用來緩存圖片Bitmap,用圖片的URL作為緩存查找的Key;
2.設(shè)兩級緩存,一級是SoftReference,二級是本地SD卡;
3.如果兩級緩存都沒取到圖片,則從服務(wù)器獲取,并加入緩存;
4.加載完后通過回調(diào)接口通知UI更新;
以下是異步加載的關(guān)鍵代碼,其中一些工具類沒有給出,自己實現(xiàn)就可以,比如HttpRequest是我自己寫的一個類。
復(fù)制代碼 代碼如下:
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地址從服務(wù)器獲取圖片輸入流
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方法中:
復(fù)制代碼 代碼如下:
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的構(gòu)造方法中初始化的,形成一個緩存。通過這個機(jī)制就可以實現(xiàn)ListView的圖片異步加載,在用戶體驗上比直接加載要感覺好很多,那樣會造成界面卡頓。這里是加載一張圖片的情況,如果ListView的item中的圖片是不定的,有可能是一張、兩張、三張,該用什么方式呢,大家可以思考一下,并可以一起討論一下,包括實現(xiàn)ListView滾動時不加載數(shù)據(jù)也是優(yōu)化ListView加載的必要步驟。
您可能感興趣的文章:
- ListView異步加載圖片實現(xiàn)思路(優(yōu)化篇)
- 將選擇的圖片顯示在listview中,并顯示filename,path和type的簡單實例
- Android中ListView異步加載圖片錯位、重復(fù)、閃爍問題分析及解決方案
- Android實現(xiàn)ListView異步加載圖片的方法
- 新浪微博第三方登錄界面上下拉伸圖片之第三方開源PullToZoomListViewEx(一)
- 新浪微博第三方登錄界面上下拉伸圖片之第三方開源PullToZoomListViewEx(二)
- Android程序開發(fā)ListView+Json+異步網(wǎng)絡(luò)圖片加載+滾動翻頁的例子(圖片能緩存,圖片不錯亂)
- Android ListView異步加載圖片方法詳解
- C#下listview如何插入圖片
相關(guān)文章
關(guān)于Android發(fā)送短信獲取送達(dá)報告的問題(推薦)
最近公司開發(fā)一個項目,要求app能夠發(fā)送短信并獲取送達(dá)報告。實現(xiàn)代碼非常簡單的,下面小編給大家分享關(guān)于Android發(fā)送短信獲取送達(dá)報告的問題,感興趣的朋友一起看看吧2017-03-03
Android IPC機(jī)制Messenger實例詳解
這篇文章主要介紹了 Android IPC機(jī)制Messenger實例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
Android自定義控件實現(xiàn)滑動開關(guān)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實現(xiàn)滑動開關(guān)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
Android Recyclerview實現(xiàn)水平分頁GridView效果示例
本篇文章主要介紹了Android Recyclerview實現(xiàn)水平分頁GridView效果示例,具有一定的參考價值,有興趣的可以了解一下2017-08-08
深入android Unable to resolve target ''android-XX''詳解
本篇文章是對android Unable to resolve target 'android-XX'錯誤的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android下通過httpClient發(fā)送GET和POST請求的實例代碼
這篇文章介紹了Android下通過httpClient發(fā)送GET和POST請求的實例代碼,有需要的朋友可以參考一下2013-08-08

