当前位置: 首页 > news >正文

解决Android ListView中图片异步加载错位问题(可以直接用,缓存机制也不错的)...

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package com.ljlt.utils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

public enum BitmapManager {  
     INSTANCE;  
   
     private final Map<String, SoftReference<Bitmap>> cache;  
     private final ExecutorService pool;  
     private Map<ImageView, String> imageViews = Collections  
             .synchronizedMap(new WeakHashMap<ImageView, String>());  
     private Bitmap placeholder;  
   
     BitmapManager() {  
         cache = new HashMap<String, SoftReference<Bitmap>>();  
         pool = Executors.newFixedThreadPool(5);  
     }  
   
     public void setPlaceholder(Bitmap bmp) {  
         placeholder = bmp;  
     }  
   
     public Bitmap getBitmapFromCache(String url) {  
         if (cache.containsKey(url)) {  
             return cache.get(url).get();  
         }  
   
         return null;  
     }  
   
     public void queueJob(final String url, final ImageView imageView,  
             final int width, final int height) {  
         /* Create handler in UI thread. */  
         final Handler handler = new Handler() {  
             public void handleMessage(Message msg) {  
                 String tag = imageViews.get(imageView);  
                 if (tag != null && tag.equals(url)) {  
                     if (msg.obj != null) {  
                         imageView.setImageBitmap((Bitmap) msg.obj);  
                     } else {  
                         imageView.setImageBitmap(placeholder);  
                         Log.d(null, "fail " + url);  
                     }  
                 }  
             }  
         };  
   
         pool.submit(new Runnable() {  
             @Override  
             public void run() {  
                 final Bitmap bmp = downloadBitmap(url, width, height);  
                 Message message = Message.obtain();  
                 message.obj = bmp;  
                 Log.d(null, "Item downloaded: " + url);  
   
                 handler.sendMessage(message);  
             }  
         });  
     }  
   
     public void loadBitmap(final String url, final ImageView imageView,  
             final int width, final int height) {  
         imageViews.put(imageView, url);  
         Bitmap bitmap = getBitmapFromCache(url);  
   
         // check in UI thread, so no concurrency issues  
         if (bitmap != null) {  
             Log.d(null, "Item loaded from cache: " + url);  
             imageView.setImageBitmap(bitmap);  
         } else {  
             imageView.setImageBitmap(placeholder);  
             queueJob(url, imageView, width, height);  
         }  
     }  
   
     private Bitmap downloadBitmap(String url, int width, int height) {  
         try {  
             Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(  
                     url).getContent());  
             bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);  
             cache.put(url, new SoftReference<Bitmap>(bitmap));  
             return bitmap;  
         } catch (MalformedURLException e) {  
             e.printStackTrace();  
         } catch (IOException e) {  
             e.printStackTrace();  
         }  
   
         return null;  
     }  
 }



原文地址是:http://www.cnblogs.com/liongname/articles/2345087.html

转载于:https://my.oschina.net/lhjtianji/blog/106487

相关文章:

  • 2013年运唐立淇——水瓶座
  • 帮助你生成响应式布局的CSS模板 - xyCSS
  • mvn添加本地jar包
  • OHStackView
  • C++中的三种继承public,protected ,private
  • oracle错误一览
  • y-combiantor项目列表
  • bootstrap使用总结
  • ciscocatalyst6500引擎内存与flash经典
  • 如何查看IE型号
  • [转]自建CDN防御DDoS:知己知彼,建设持久防线
  • JS中的replace(RegExp, Function)
  • 本机安装wamp后,非本机访问phpmyadmin Forbidden的 解决方法
  • Linux 中用 strace 追踪系统调用和信号值
  • [译]ECMAScript 6:对象字面量中的属性值简写
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • docker python 配置
  • ES6核心特性
  • IndexedDB
  • Laravel深入学习6 - 应用体系结构:解耦事件处理器
  • LeetCode29.两数相除 JavaScript
  • Mysql5.6主从复制
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • nodejs:开发并发布一个nodejs包
  • oldjun 检测网站的经验
  • PHP 小技巧
  • rabbitmq延迟消息示例
  • Vim Clutch | 面向脚踏板编程……
  • 电商搜索引擎的架构设计和性能优化
  • 服务器之间,相同帐号,实现免密钥登录
  • 浮动相关
  • 关于使用markdown的方法(引自CSDN教程)
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • ​Spring Boot 分片上传文件
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (windows2012共享文件夹和防火墙设置
  • (二)构建dubbo分布式平台-平台功能导图
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (九)c52学习之旅-定时器
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (十一)手动添加用户和文件的特殊权限
  • (转) ns2/nam与nam实现相关的文件
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • ***利用Ms05002溢出找“肉鸡
  • @cacheable 是否缓存成功_Spring Cache缓存注解
  • []新浪博客如何插入代码(其他博客应该也可以)
  • [ACM] hdu 1201 18岁生日
  • [ASP.NET MVC]如何定制Numeric属性/字段验证消息
  • [BZOJ 3282] Tree 【LCT】
  • [BZOJ4566][HAOI2016]找相同字符(SAM)