写道
今天晚上一个朋友介绍我看了一篇文章,也是解决android中listview在加载图片错位的问题,看了之后,感觉写的很好,自己也遇到这个问题,但是又不知道从何下手,看到这篇文章后,我的问题得到了解决,同时也感谢作者。
现在饿就把作者的文章转帖上来,给大家共享。
 写道
1、采用线程池



2、内存缓存+文件缓存



3、内存缓存中网上很多是采用SoftReference来防止堆溢出,这儿严格限制只能使用最大JVM内存的1/4



4、对下载的图片进行按比例缩放,以减少内存的消耗



具体的代码里面说明。先放上内存缓存类的代码MemoryCache.java:

public class MemoryCache {

  1. private static final String TAG = "MemoryCache";
  2. // 放入缓存时是个同步操作
  3. // LinkedHashMap构造方法的最后一个参数true代表这个map里的元素将按照最近使用次数由少到多排列,即LRU
  4. // 这样的好处是如果要将缓存中的元素替换,则先遍历出最近最少使用的元素来替换以提高效率
  5. private Map<String, Bitmap> cache = Collections
  6. , 1.5f, true));
  7. // 缓存中图片所占用的字节,初始0,将通过此变量严格控制缓存所占用的堆内存
  8. ;// current allocated size
  9. // 缓存只能占用的最大堆内存
  10. ;// max memory in bytes
  11. public MemoryCache() {
  12. // use 25% of available heap size
  13. );
  14. }
  15. public void setLimit(long new_limit) {
  16. limit = new_limit;
  17. . / 1024. + "MB");
  18. }
  19. public Bitmap get(String id) {
  20. try {
  21. if (!cache.containsKey(id))
  22. return null;
  23. return cache.get(id);
  24. } catch (NullPointerException ex) {
  25. return null;
  26. }
  27. }
  28. public void put(String id, Bitmap bitmap) {
  29. try {
  30. if (cache.containsKey(id))
  31. size -= getSizeInBytes(cache.get(id));
  32. cache.put(id, bitmap);
  33. size += getSizeInBytes(bitmap);
  34. checkSize();
  35. } catch (Throwable th) {
  36. th.printStackTrace();
  37. }
  38. }
  39. /**
  40. * 严格控制堆内存,如果超过将首先替换最近最少使用的那个图片缓存
  41. *
  42. */
  43. private void checkSize() {
  44. Log.i(TAG, "cache size=" + size + " length=" + cache.size());
  45. if (size > limit) {
  46. // 先遍历最近最少使用的元素
  47. Iterator<Entry<String, Bitmap>> iter = cache.entrySet().iterator();
  48. while (iter.hasNext()) {
  49. Entry<String, Bitmap> entry = iter.next();
  50. size -= getSizeInBytes(entry.getValue());
  51. iter.remove();
  52. if (size <= limit)
  53. break;
  54. }
  55. Log.i(TAG, "Clean cache. New size " + cache.size());
  56. }
  57. }
  58. public void clear() {
  59. cache.clear();
  60. }
  61. /**
  62. * 图片占用的内存
  63. *
  64. * @param bitmap
  65. * @return
  66. */
  67. long getSizeInBytes(Bitmap bitmap) {
  68. if (bitmap == null)
  69. ;
  70. return bitmap.getRowBytes() * bitmap.getHeight();
  71. }
  1. 也可以使用SoftReference,代码会简单很多,但是推荐上面的方法。
  2. public class MemoryCache {
  3. private Map<String, SoftReference<Bitmap>> cache = Collections
  4. .synchronizedMap(new HashMap<String, SoftReference<Bitmap>>());
  5. public Bitmap get(String id) {
  6. if (!cache.containsKey(id))
  7. return null;
  8. SoftReference<Bitmap> ref = cache.get(id);
  9. return ref.get();
  10. }
  11. public void put(String id, Bitmap bitmap) {
  12. cache.put(id, new SoftReference<Bitmap>(bitmap));
  13. }
  14. public void clear() {
  15. cache.clear();
  16. }
  17. }
  1. 下面是文件缓存类的代码FileCache.java
  2. public class FileCache {
  3. private File cacheDir;
  4. public FileCache(Context context) {
  5. // 如果有SD卡则在SD卡中建一个LazyList的目录存放缓存的图片
  6. // 没有SD卡就放在系统的缓存目录中
  7. if (android.os.Environment.getExternalStorageState().equals(
  8. android.os.Environment.MEDIA_MOUNTED))
  9. cacheDir = new File(
  10. android.os.Environment.getExternalStorageDirectory(),
  11. "LazyList");
  12. else
  13. cacheDir = context.getCacheDir();
  14. if (!cacheDir.exists())
  15. cacheDir.mkdirs();
  16. }
  17. public File getFile(String url) {
  18. // 将url的hashCode作为缓存的文件名
  19. String filename = String.valueOf(url.hashCode());
  20. // Another possible solution
  21. // String filename = URLEncoder.encode(url);
  22. File f = new File(cacheDir, filename);
  23. return f;
  24. }
  25. public void clear() {
  26. File[] files = cacheDir.listFiles();
  27. if (files == null)
  28. return;
  29. for (File f : files)
  30. f.delete();
  31. }
  32. }

最后最重要的加载图片的类,ImageLoader.java

  1. public class ImageLoader {
  2. MemoryCache memoryCache = new MemoryCache();
  3. FileCache fileCache;
  4. private Map<ImageView, String> imageViews = Collections
  5. .synchronizedMap(new WeakHashMap<ImageView, String>());
  6. // 线程池
  7. ExecutorService executorService;
  8. public ImageLoader(Context context) {
  9. fileCache = new FileCache(context);
  10. );
  11. }
  12. // 当进入listview时默认的图片,可换成你自己的默认图片
  13. final int stub_id = R.drawable.stub;
  14. // 最主要的方法
  15. public void DisplayImage(String url, ImageView imageView) {
  16. imageViews.put(imageView, url);
  17. // 先从内存缓存中查找
  18. Bitmap bitmap = memoryCache.get(url);
  19. if (bitmap != null)
  20. imageView.setImageBitmap(bitmap);
  21. else {
  22. // 若没有的话则开启新线程加载图片
  23. queuePhoto(url, imageView);
  24. imageView.setImageResource(stub_id);
  25. }
  26. }
  27. private void queuePhoto(String url, ImageView imageView) {
  28. PhotoToLoad p = new PhotoToLoad(url, imageView);
  29. executorService.submit(new PhotosLoader(p));
  30. }
  31. private Bitmap getBitmap(String url) {
  32. File f = fileCache.getFile(url);
  33. // 先从文件缓存中查找是否有
  34. Bitmap b = decodeFile(f);
  35. if (b != null)
  36. return b;
  37. // 最后从指定的url中下载图片
  38. try {
  39. Bitmap bitmap = null;
  40. URL imageUrl = new URL(url);
  41. HttpURLConnection conn = (HttpURLConnection) imageUrl
  42. .openConnection();
  43. );
  44. );
  45. conn.setInstanceFollowRedirects(true);
  46. InputStream is = conn.getInputStream();
  47. OutputStream os = new FileOutputStream(f);
  48. CopyStream(is, os);
  49. os.close();
  50. bitmap = decodeFile(f);
  51. return bitmap;
  52. } catch (Exception ex) {
  53. ex.printStackTrace();
  54. return null;
  55. }
  56. }
  57. // decode这个图片并且按比例缩放以减少内存消耗,虚拟机对每张图片的缓存大小也是有限制的
  58. private Bitmap decodeFile(File f) {
  59. try {
  60. // decode image size
  61. BitmapFactory.Options o = new BitmapFactory.Options();
  62. o.inJustDecodeBounds = true;
  63. BitmapFactory.decodeStream(new FileInputStream(f), null, o);
  64. // Find the correct scale value. It should be the power of 2.
  65. ;
  66. int width_tmp = o.outWidth, height_tmp = o.outHeight;
  67. ;
  68. while (true) {
  69. < REQUIRED_SIZE
  70. < REQUIRED_SIZE)
  71. break;
  72. ;
  73. ;
  74. ;
  75. }
  76. // decode with inSampleSize
  77. BitmapFactory.Options o2 = new BitmapFactory.Options();
  78. o2.inSampleSize = scale;
  79. return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
  80. } catch (FileNotFoundException e) {
  81. }
  82. return null;
  83. }
  84. // Task for the queue
  85. private class PhotoToLoad {
  86. public String url;
  87. public ImageView imageView;
  88. public PhotoToLoad(String u, ImageView i) {
  89. url = u;
  90. imageView = i;
  91. }
  92. }
  93. class PhotosLoader implements Runnable {
  94. PhotoToLoad photoToLoad;
  95. PhotosLoader(PhotoToLoad photoToLoad) {
  96. this.photoToLoad = photoToLoad;
  97. }
  98. @Override
  99. public void run() {
  100. if (imageViewReused(photoToLoad))
  101. return;
  102. Bitmap bmp = getBitmap(photoToLoad.url);
  103. memoryCache.put(photoToLoad.url, bmp);
  104. if (imageViewReused(photoToLoad))
  105. return;
  106. BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
  107. // 更新的操作放在UI线程中
  108. Activity a = (Activity) photoToLoad.imageView.getContext();
  109. a.runOnUiThread(bd);
  110. }
  111. }
  112. /**
  113. * 防止图片错位
  114. *
  115. * @param photoToLoad
  116. * @return
  117. */
  118. boolean imageViewReused(PhotoToLoad photoToLoad) {
  119. String tag = imageViews.get(photoToLoad.imageView);
  120. if (tag == null || !tag.equals(photoToLoad.url))
  121. return true;
  122. return false;
  123. }
  124. // 用于在UI线程中更新界面
  125. class BitmapDisplayer implements Runnable {
  126. Bitmap bitmap;
  127. PhotoToLoad photoToLoad;
  128. public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
  129. bitmap = b;
  130. photoToLoad = p;
  131. }
  132. public void run() {
  133. if (imageViewReused(photoToLoad))
  134. return;
  135. if (bitmap != null)
  136. photoToLoad.imageView.setImageBitmap(bitmap);
  137. else
  138. photoToLoad.imageView.setImageResource(stub_id);
  139. }
  140. }
  141. public void clearCache() {
  142. memoryCache.clear();
  143. fileCache.clear();
  144. }
  145. public static void CopyStream(InputStream is, OutputStream os) {
  146. ;
  147. try {
  148. byte[] bytes = new byte[buffer_size];
  149. for (;;) {
  150. , buffer_size);
  151. )
  152. break;
  153. , count);
  154. }
  155. } catch (Exception ex) {
  156. }
  157. }
  158. }

写道

主要流程是先从内存缓存中查找,若没有再开线程,从文件缓存中查找都没有则从指定的url中查找,并对bitmap进行处理,最后通过下面方法对UI进行更新操作。
  1. a.runOnUiThread(...);
  1. 在你的程序中的基本用法:
  2. ImageLoader imageLoader=new ImageLoader(context);
  3. ...
  4. imageLoader.DisplayImage(url, imageView);
  5. 比如你的放在你的ListView的adapter的getView()方法中,当然也适用于GridView。
  1. adapter的代码:
  1. class MyAdapter extends BaseAdapter{
  2. private String urls[];
  3. private Context context;
  4. public int getCount() {
  5. return urls.length;
  6. }
  7. public void setData(String[] urls) {
  8. this.urls = urls;
  9. }
  10. public Object getItem(int position) {
  11. return urls[position];
  12. }
  13. public long getItemId(int position) {
  14. return position;
  15. }
  16. public View getView(int position, View convertView, ViewGroup parent) {
  17. ViewHolder holder = null;
  18. if(convertView == null){
  19. holder = new ViewHolder();
  20. convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.listview_item, null);
  21. holder.imageView = (ImageView) convertView.findViewById(R.id.imageview);
  22. convertView.setTag(holder);
  23. }else{
  24. holder = (ViewHolder) convertView.getTag();
  25. }
  26. System.out.println("开始下载图片 --------------position--------==== " + position);
  27. //把imageLoader传进adapter里面来
  28. imageLoader.displayImage(urls[position], holder.imageView);
  29. return convertView;
  30. }
  31. class ViewHolder{
  32. ImageView imageView;
  33. }
  34. }

写道

最后注意一点:要加权限。网络,sdcard等权限。

android listview 加载图片错乱(错位)的更多相关文章

  1. Android Glide加载图片时转换为圆形、圆角、毛玻璃等图片效果

     Android Glide加载图片时转换为圆形.圆角.毛玻璃等图片效果 附录1简单介绍了Android开源的图片加载框架.在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬 ...

  2. 演化理解 Android 异步加载图片(转)

    演化理解 Android 异步加载图片(转)http://www.cnblogs.com/CJzhang/archive/2011/10/20/2218474.html

  3. 【Android】ListView、RecyclerView异步加载图片引起错位问题

    今天在RecyclerView列表里遇到一个情况,它包含300条数据,每项包含一个图片,发现在首次载入时,由于本地没图,请求网络的时候:快速滑动导致了图片错位.闪烁的问题. 原理的话有一篇已经说的很清 ...

  4. 解决ListView异步加载图片错乱问题 .

    发一个异步图片加载控件.网上也有大把的异步网络加载图片的控件,但是有一个问题,异步加载会造成列表中的图片混乱,因为列表的每一项的View都可能被重用,异步加载的时候多个异步线程引用到了同一个View将 ...

  5. [Android]异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3574131.html  这个可以实现ImageView异步加载 ...

  6. listview加载图片显示

    Adapter:   ---- //adapter的构造方法:   参数1 为url数组: public static String[] mList;// 讲url保村在静态的String[] 中 在 ...

  7. 实例演示Android异步加载图片

    本文给大家演示异步加载图片的分析过程.让大家了解异步加载图片的好处,以及如何更新UI.首先给出main.xml布局文件:简单来说就是 LinearLayout 布局,其下放了2个TextView和5个 ...

  8. 实例演示Android异步加载图片(转)

    本文给大家演示异步加载图片的分析过程.让大家了解异步加载图片的好处,以及如何更新UI.首先给出main.xml布局文件:简单来说就是 LinearLayout 布局,其下放了2个TextView和5个 ...

  9. 演化理解 Android 异步加载图片

    原文:http://www.cnblogs.com/ghj1976/archive/2011/05/06/2038738.html#3018499 在学习"Android异步加载图像小结&q ...

随机推荐

  1. Entity Framework 泛型使用

    因为增删改查是我们常用到的方法,我们不可能每个数据模型都去完成增删改查,这样的办法太笨拙了.我们可以写个父类(包含增删改查),然后让所有的数据模型类继承该父类.那就要求我们的父类必须使用泛型来实现. ...

  2. database first表更新一个表会更新所有的model,包括添加验证代码,解决办法

    因为model类是自动生成的,重新生成后会覆盖自己的修改.一个比较合理做法,就是用 partial class的方式来实现. 比如有一个Model类: Movie.那我们就可以添加一个局部类文件,局部 ...

  3. 56.ISE综合,在chipscope信号列表看不到

    代码写好后,进行逻辑综合,在chipscope上添加被触发的信号时,发现有些在信号列表里看不到,这是因为这些信号没有参与到逻辑电路设计中,产生不想关的电路,综合器会默认优化资源. 还有一种情况是,对于 ...

  4. ASP.NET中Global.asax 文件是什么?

    Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法.你可以使用这个文件实现应用程序安全性以及其它一些任务.下面让我们详细看 ...

  5. spring注解注入失败一个原因

    所有的注解看起来都没有任何问题,最后是由于web-xml配置问题. 由于缺少监听器org.springframework.web.context.ContextLoaderListener, 导致无法 ...

  6. OC学习笔记之属性详解和易错点

    属性的概念在OC1.0中就存在,格式是定义实例变量,然后定义setter和getter方法,用点操作符操作属性 举例,类的接口部分 @interface Father : NSObject { NSI ...

  7. html5+ plus和phoneGap、cordova的比较

    偶遇大神言论,摘录 phonegap出的早,自然用的人多.phonegap自己的定位是混合开发hybrid,用原生+js:HBuilder的定位是纯js搞定一切.5+ 和 phonegap在能力.性能 ...

  8. Careercup - Google面试题 - 5661939564806144

    2014-05-06 01:40 题目链接 原题: Give a N*N matrix, print it out diagonally. Follow up, if it is a M*N matr ...

  9. dp--poj1458最长公共子序列

    很水的一题 输入串a与串b: Dp[i][j]表示a串中1~i与b串中1~j的子串的最长公共子序列. Max{dp[i-1][j], dp[i][j-1]}       (a[i]!=b[j]) Dp ...

  10. Asp.net 导入Excel(服务器不带Office)

    #region 把excel文件转换为DataSet. /// <summary> /// 把excel文件转换为DataSet. /// </summary> /// < ...