Android学习——图片异步加载工具类 用到了SoftReference
来源:互联网 发布:mac os x10.12懒人版 编辑:程序博客网 时间:2024/05/16 05:25
来源与网络,具体地址忘记了,写篇博客记录下 ,方便自己以后使用 。 谢谢原作者 。
public class AsyncImageLoader {public Map<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>(); // 固定五个线程来执行任务 private ExecutorService executorService = Executors.newFixedThreadPool(5); private final Handler handler = new Handler(); /** * * @param imageUrl 图像url地址 * @param callback 回调接口 * @return 返回内存中缓存的图像,第一次加载返回null */ public Drawable loadDrawable(final String imageUrl, final ImageCallback callback) { // 如果缓存过就从缓存中取出数据 if (imageCache.containsKey(imageUrl)) { SoftReference<Drawable> softReference = imageCache.get(imageUrl); if (softReference.get() != null) { return softReference.get(); } } // 缓存中没有图像,则从网络上取出数据,并将取出的数据缓存到内存中 executorService.submit(new Runnable() { public void run() { try { final Drawable drawable = loadImageFromUrl(imageUrl); imageCache.put(imageUrl, new SoftReference<Drawable>( drawable)); System.out.println(Thread.currentThread().getId()); handler.post(new Runnable() { public void run() { callback.imageLoaded(drawable); callback.returnImageLoaded(drawable); } }); } catch (Exception e) { throw new RuntimeException(e); } } }); return null; } // 从网络上取数据方法 protected Drawable loadImageFromUrl(String imageUrl) { try { // 测试时,模拟网络延时,实际时这行代码不能有 //SystemClock.sleep(2000); return Drawable.createFromStream(new URL(imageUrl).openStream(), "image.png"); } catch (Exception e) { throw new RuntimeException(e); } } // 对外界开放的回调接口 public interface ImageCallback { // 注意 此方法是用来设置目标对象的图像资源 public void imageLoaded(Drawable imageDrawable); public Drawable returnImageLoaded(Drawable imageDrawable); } }
调用的地方可以这么写
private AsyncImageLoader asyncImageLoader3 = new AsyncImageLoader(); // 引入线程池,并引入内存缓存功能,并对外部调用封装了接口,简化调用过程 private Drawable loadImage4(final String url, final ImageView imageView) { System.out.println("url" + url); // 如果缓存过就会从缓存中取出图像,ImageCallback接口中方法也不会被执行 Drawable cacheImage = asyncImageLoader3.loadDrawable(url, new AsyncImageLoader.ImageCallback() { // 请参见实现:如果第一次加载url时下面方法会执行 @Overridepublic void imageLoaded(Drawable imageDrawable) { imageView.setImageDrawable(imageDrawable); //cacheImage = imageDrawable; }@Overridepublic Drawable returnImageLoaded(Drawable imageDrawable) {// TODO Auto-generated method stubreturn imageDrawable;} }); if (cacheImage != null) { imageView.setImageDrawable(cacheImage); } return cacheImage; }
仅供参考,欢迎纠错。
- Android学习——图片异步加载工具类 用到了SoftReference
- Android优化系列——控件优化(ListView 异步加载图片优化,SoftReference)
- Android性能优化之实现双缓存的图片异步加载工具(LruCache+SoftReference)
- android Listview 软引用SoftReference异步加载图片
- android Listview 软引用SoftReference异步加载图片
- android Listview 软引用SoftReference异步加载图片
- android Listview 软引用SoftReference异步加载图片
- 在android中 软引用SoftReference异步加载网络图片
- Android 图片异步加载的体会,SoftReference已经不再适用
- Android 图片异步加载的体会,SoftReference已经不再适用
- android Listview 软引用SoftReference异步加载图片
- Android性能优化之实现双缓存的图片异步加载工具(LruCache+SoftReference) - 拿来即用
- Android LruCache图片异步加载工具类
- 利用SoftReference实现图片异步加载
- Listview 软引用SoftReference异步加载图片
- 使用新式LruCache取代SoftReference缓存图片,Android异步加载图片
- 图片异步加载工具类
- Android学习笔记————异步图片加载
- 额外域控制升级为主域控制器
- winForm 报表使用
- Linux系统各个目录的作用(中英文对照)
- 为什么使用RTP协议?
- CMFCMenuBar折叠去除
- Android学习——图片异步加载工具类 用到了SoftReference
- PowerDesigner 业务处理模型( BPM )
- android 从assets和res中读取文件(转)
- linux c/c++ 用sscanf或者scanf时数值变量前请不要忘记加&&&&&&&&&&&&&&&&&&&&&
- android中调用.net web service
- vs 皮肤 主题
- intersect 裁剪导出要素类
- 什么是RTP、RTCP和RSVP?
- ios中的block