国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

ListView異步加載圖片實現思路

2020-04-11 12:29:59
字體:
來源:轉載
供稿:網友
在應用開發中,經常用到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加載的必要步驟。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 田林县| 奉化市| 荆州市| 襄樊市| 阿图什市| 麻栗坡县| 镇雄县| 德化县| 民和| 天祝| 木里| 綦江县| 宁城县| 胶州市| 九江县| 工布江达县| 太仓市| 赤峰市| 泗洪县| 玉田县| 邮箱| 广丰县| 舒兰市| 郯城县| 蓝山县| 鹿邑县| 福鼎市| 中阳县| 香河县| 库车县| 沐川县| 富阳市| 毕节市| 建宁县| 清水县| 庄河市| 永泰县| 商河县| 洮南市| 灌南县| 临江市|