Android 图片缓存技术支持

本文系作者原创,转载请附原文地址,谢谢!

原文地址:http://blog.csdn.net/a774057695/article/details/47150683


/**
重新编辑了一下,发现之前有一堆h5标记。。。。
2015-9-8 
*/


前言:

    一晃几天没有写东西,这几天一直在做缓存的技术积累,移动端开发者不做移动应用是基本不可能的,经常会被图片什么的虐的死去活来,我想看到我这篇文章的人,对oom,anr已经见怪不怪,快久病成医了,甚至都快单手撸出三五千行的代码解决图片缓存的问题了。但是写本文的意义不在于和大家探讨:异步下载的策略,缓存图片的压缩,Lru算法等等知识,甚至连连缓存检索策略都不愿再去提。因为大家都知道的差不多了。

    我想大家真正头疼的事情无非就是两个:

  1.     搞不定这个问题。
  2.     写的代码不便于复用。
    所以本文是提供支持的,本人编写了类库,但是这个类库的源码公开的事情还是要暂缓,毕竟会牵涉到公司内的项目,而且我计划中的一些功能尚未实现。
该类库提供给大家使用,并注释使用方法。

    最近我也不知道是怎么了没事老是干底层的开发。。。。。睡觉

正文:

    首先你要下载该类库:链接

    你可以先建立一个测试应用来测试。

  1. 新建android项目,sdk>=8
  2. 将类库拷贝到libs目录下
  3. configure build path ,add该类库
  4.  在布局中添加ImageView控件,并在activity中实例化。
  5. 申明对象:private ImageCache mCache;
  6. 获取对象实例:mCache = ImageCache.getInstance(getApplicationContext());
  7. 当前版本中开放的使用方法:
    <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>
    

  8. 当然,你会需要清除缓存,将它做在设置中,使用代码:mCache.clear();

后面会逐渐完善功能。今天就先到这里。

本文系作者原创,转载请附原文地址,谢谢!

原文地址:http://blog.csdn.net/a774057695/article/details/47150683




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值