写道
今天晚上一个朋友介绍我看了一篇文章,也是解决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. [转]ubuntu 14.04 系统设置不见了

    [转]ubuntu 14.04 系统设置不见了 http://blog.sina.com.cn/s/blog_6c9d65a10101i0i7.html 不知道删除什么了,系统设置不见了! 我在终端运 ...

  2. [转]Ubuntu 12.04开机自动挂载Windows分区

    [转]Ubuntu 12.04开机自动挂载Windows分区 http://www.cnblogs.com/A-Song/archive/2013/02/27/2935255.html 系统版本:Ub ...

  3. scrapy 错误

    1. 安装win32时候 Unable to find vcvarsall.bat 解决方法: 1.如果你没有安装vc,去微软下个 VS2008 的免费版就能解决此问题. 2.如果你安装的是VS201 ...

  4. 如何在Quartus II中设置Virtual pin

    为了验证FPGA工程中的某个模块的功能和时序的正确性,常常需要对其单独进行验证,但是这些模块通常都与内部的众多信号相连(如系统总线,中断信号线等),往往一个模块的对外接口引脚会多达几百个,对其单独仿真 ...

  5. transform属性

    transform属性 在OC中,通过transform属性可以修改对象的平移.缩放比例和旋转角度常用的创建transform结构体方法分两大类 (1) 创建“基于控件初始位置”的形变 CGAffin ...

  6. rapid js framework

    allcountjs.com http://mean.io/ https://www.meteor.com/ http://sailsjs.org/#!/ nodejs 博客 http://hexo. ...

  7. android继承Dialog实现自定义对话框

    有时需要自定义对话框,可以使用AlterDialog.Bulider,比如下面的代码片段 new AlertDialog.Builder(self) .setTitle("标题") ...

  8. Differences Between Xcode Project Templates for iOS Apps

    Differences Between Xcode Project Templates for iOS Apps When you create a new iOS app project in Xc ...

  9. Travis-CI的初步了解和测试程序的进一步编写

    一. Travis-CI部分 最近基本都在研究Travis-CI的使用.CI是continue integration(持续集成)的缩写,Travis应该是给我们提供免费服务器的组织.下面介绍一下其使 ...

  10. The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit

    如果你是通过搜索来到本文的,相信你应该是遇到了如下的错误 The code of method _jspService(HttpServletRequest, HttpServletResponse) ...