本文系作者原创,转载请附原文地址,谢谢!
原文地址:http://blog.csdn.net/a774057695/article/details/47150683
/**
重新编辑了一下,发现之前有一堆h5标记。。。。
2015-9-8
*/
前言:
一晃几天没有写东西,这几天一直在做缓存的技术积累,移动端开发者不做移动应用是基本不可能的,经常会被图片什么的虐的死去活来,我想看到我这篇文章的人,对oom,anr已经见怪不怪,快久病成医了,甚至都快单手撸出三五千行的代码解决图片缓存的问题了。但是写本文的意义不在于和大家探讨:异步下载的策略,缓存图片的压缩,Lru算法等等知识,甚至连连缓存检索策略都不愿再去提。因为大家都知道的差不多了。
我想大家真正头疼的事情无非就是两个:
- 搞不定这个问题。
- 写的代码不便于复用。
所以本文是提供支持的,本人编写了类库,但是这个类库的源码公开的事情还是要暂缓,毕竟会牵涉到公司内的项目,而且我计划中的一些功能尚未实现。
该类库提供给大家使用,并注释使用方法。
最近我也不知道是怎么了没事老是干底层的开发。。。。。
正文:
首先你要下载该类库:链接
你可以先建立一个测试应用来测试。
- 新建android项目,sdk>=8
- 将类库拷贝到libs目录下
- configure build path ,add该类库
- 在布局中添加ImageView控件,并在activity中实例化。
- 申明对象:private ImageCache mCache;
- 获取对象实例:mCache = ImageCache.getInstance(getApplicationContext());
- 当前版本中开放的使用方法:
<span style="font-size:18px;">//实例化Imageloader对象 Imageloader imgloader = new Imageloader(getApplicationContext(), mCache); //不要故意搞点不是图片的url来作死 Uri uri = Uri.parse("http://tupian.enterdesk.com/2015/mxy/5/9/6/8.jpg"); /** * the entrance of the lib,provide it with imgUrl like "http(s)://……" and the ImageView httpImage displayed * and then, it will search the cache ,return the drawable or call for Internet if null. * ……. * Besides, the other params are provided to make the app more friendly. * * @author leobert * * @param uri *Uri // * the url of the image you want to load and display, and the image keep the same name in cache * @param iv *ImageView // * the ImageView to display the image * @param defaultImg *int // * like:R.drawable.XXX,in some issues, nothing can be found , e.g: a wrong URL,404 case,IOException……, and now you * need a picture to tell users nothing founds * @param waitingImg *int // TODO * like:R.drawable.XXX, response to users that:"we are trying to loading it" * @param defaultWidth *int // * set the Max width of the ImageView * @param defaultHeight *int // * set the Max height of the ImageView * @param defaultType *ScaleType // * set the ScaleType of the ImageView * */// imgloader.getdarwable(uri, img, R.drawable.ic_launcher, 200, 200, ScaleType.CENTER);</span>
- 当然,你会需要清除缓存,将它做在设置中,使用代码:mCache.clear();
后面会逐渐完善功能。今天就先到这里。
本文系作者原创,转载请附原文地址,谢谢!
原文地址:http://blog.csdn.net/a774057695/article/details/47150683