當前位置:
首頁 >
android listview 异步加载问题
發布時間:2023/12/19
31
豆豆
生活随笔
收集整理的這篇文章主要介紹了
android listview 异步加载问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
============問題描述============
學做android,自己想模仿QQ空間做一個小demo
listview異步加載圖片的時候遇到了一個問題
異步加載用到了SoftReference?和文件緩存的方法
每次加載圖片的時候,也在內存或緩存中找到了圖片
第一次加載出來后,listview滑動了,同樣也進到了setImageBitmap這一步
可是就是圖片顯示不出來變成空白
下面帖幾張圖和代碼
滑動前
滑動后Image_url?=?new?StringBuffer(AppConstant.DOWNLOAD_IMAGE_URL).append(msg.getOPImageList().get(0).getImageUrl()).toString();ImageView?imageView?=?holder.msgImage;imageView.setTag(Image_url);Bitmap?bitmap?=?Loader.loadBitmap(imageView,?Image_url,new?ImageCallBack()?{@Overridepublic?void?imageLoad(ImageView?imageView,?Bitmap?bitmap)?{if?(imageView.getTag()?!=?null&&?imageView.getTag().equals(Image_url))?{imageView.setImageBitmap(bitmap);}}});Log.e("當前的postion",?""?+?position);if?(bitmap?==?null)?{imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));}?else?{imageView.setImageBitmap(bitmap);}
loaderpublic?class?Loader?{/***?內存圖片軟引用緩沖*/private?static?HashMap<String,?SoftReference<Bitmap>>?imageCache?=?null;public?Loader()?{imageCache?=?new?HashMap<String,?SoftReference<Bitmap>>();}public?static?String?getFileName(String?url)?{return?url.substring(url.lastIndexOf(File.separator)?+?1);}public?static?Bitmap?getLocalResource(String?destDir,?String?imageName)?{Bitmap?bmp?=?null;File?imgeDir?=?new?File(destDir);File?cache?=?null;if?(!imgeDir.exists())?{?//?判斷本地緩存目錄是否存在imgeDir.mkdirs();}?else?{cache?=?new?File(destDir?+?File.separator?+?imageName);?//?判斷該圖片資源是否存在if?(cache.exists())?{?//?如果存在就根據圖片的存儲路徑得到Bitmap對象?bmbmp?=?BitmapFactory.decodeFile(cache.getAbsolutePath());}}return?bmp;}public?static?Bitmap?loadBitmap(final?ImageView?imageView,?final?String?imageURL,final?ImageCallBack?imageCallBack)?{//?在內存緩存中,則返回Bitmap對象if?(imageCache.containsKey(imageURL))?{SoftReference<Bitmap>?reference?=?imageCache.get(imageURL);Bitmap?bitmap?=?reference.get();if?(bitmap?!=?null)?{return?bitmap;}}?else?{/***?加上一個對本地緩存的查找*/String?bitmapName?=?imageURL.substring(imageURL.lastIndexOf("/")?+?1);Bitmap?bitmapTemp?=?null;bitmapTemp?=?getLocalResource(AppConstant.TEST,?bitmapName);if?(bitmapTemp?!=?null)?{return?bitmapTemp;}}final?Handler?handler?=?new?Handler()?{/**?(non-Javadoc)*?*?@see?android.os.Handler#handleMessage(android.os.Message)*/@Overridepublic?void?handleMessage(Message?msg)?{//?TODO?Auto-generated?method?stubimageCallBack.imageLoad(imageView,?(Bitmap)?msg.obj);}};//?如果不在內存緩存中,也不在本地(被jvm回收掉),則開啟線程下載圖片new?Thread()?{/**?(non-Javadoc)*?*?@see?java.lang.Thread#run()*/@Overridepublic?void?run()?{//?TODO?Auto-generated?method?stubBitmap?bitmap?=?null;try?{URL?imageUrl?=?new?URL(imageURL);HttpURLConnection?conn?=??(HttpURLConnection)?imageUrl.openConnection();conn.setConnectTimeout(30000);conn.setReadTimeout(30000);conn.setInstanceFollowRedirects(true);//?InputStream?is?=?conn.getInputStream();InputStream?in?=?conn.getInputStream();BitmapFactory.Options?options?=?new?BitmapFactory.Options();options.inJustDecodeBounds?=?false;options.inSampleSize?=?10;?//?width,hight設為原來的十分一bitmap?=?BitmapFactory.decodeStream(in,?null,?options);}?catch?(MalformedURLException?e1)?{//?TODO?Auto-generated?catch?blocke1.printStackTrace();}?catch?(IOException?e1)?{//?TODO?Auto-generated?catch?blocke1.printStackTrace();}imageCache.put(imageURL,?new?SoftReference<Bitmap>(bitmap));Message?msg?=?handler.obtainMessage(0,?bitmap);handler.sendMessage(msg);File?dir?=?new?File(AppConstant.TEST);if?(!dir.exists())?{dir.mkdirs();}File?bitmapFile?=?new?File(AppConstant.TEST?+?File.separator+?imageURL.substring(imageURL.lastIndexOf("/")?+?1));if?(!bitmapFile.exists())?{try?{bitmapFile.createNewFile();}?catch?(IOException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}}FileOutputStream?fos;try?{fos?=?new?FileOutputStream(bitmapFile);bitmap.compress(Bitmap.CompressFormat.JPEG,?100,?fos);fos.close();}?catch?(FileNotFoundException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}?catch?(IOException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}}}.start();return?null;}/***?回調接口*?*?@author?onerain*?*/public?interface?ImageCallBack?{public?void?imageLoad(ImageView?imageView,?Bitmap?bitmap);}}
坐等大神支招啊
每次都進了setimagebitmap這一步?就是加載不出圖片
============解決方案1============
你用imageLoader這個三方開源庫試試。
============解決方案2============
看一下你imageview控件設的是src還是background。或者試試把bitmap轉成drawable,再setbackground試試
============解決方案3============
看下getView方法?
============解決方案4============
貼下Adapter的代碼看看唄
============解決方案5============
你試試AsyncImage這個第三方開源庫
============解決方案6============
??ImageView?imageViewByTag?=?(ImageView)?listview?
????????????????????????????????.findViewWithTag(imageUrl);?
問題應該在這一句?你這個搜索?只會找到第一個TAG?是imageUrl的控件。?可能并不是你的要更新的控件。
比如??你setTAG了??,?listView?會重用item的,???在?msg.getOPImageList().size()=0?時候?還是有這個tag。??
然后你find的結果就是這個contentView里面的,不是你要更新的那個ImageView。?
============解決方案7============
引用Image_url?=?new?StringBuffer(AppConstant.DOWNLOAD_IMAGE_URL)
.append(msg.getOPImageList().get(0).getImageUrl())
.toString();
ImageView?imageView?=?holder.msgImage;
imageView.setTag(Image_url);
Bitmap?bitmap?=?Loader.loadBitmap(imageView,?Image_url,
new?ImageCallBack()?{
@Override
public?void?imageLoad(ImageView?imageView,?Bitmap?bitmap)?{
if?(imageView.getTag()?!=?null
&&?imageView.getTag().equals(Image_url))?{
imageView.setImageBitmap(bitmap);
}
}
});
Log.e("當前的postion",?""?+?position);
if?(bitmap?==?null)?{
imageView.setImageDrawable(context.getResources().getDrawable(
R.drawable.ic_launcher));
}?else?{
imageView.setImageBitmap(bitmap);
}
這個代碼中??在?ImageCallBack?接口中?吧imageUrl?回傳回來,跟imageView.getTag()?進行比較,應該可以解決問題。試試看。
轉載于:https://www.cnblogs.com/lengyanyue39/p/4091340.html
總結
以上是生活随笔為你收集整理的android listview 异步加载问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ1080 Human Gene F
- 下一篇: 其实,最好的年龄才刚刚开始