您当前的位置: 首页 >  缓存

xiangzhihong8

暂无认证

  • 12浏览

    0关注

    1324博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

picasso图片缓存框架

xiangzhihong8 发布时间:2014-12-05 19:42:47 ,浏览量:12

picasso是Square公司开源的一个Android图形缓存库,地址http://square.github.io/picasso/,可以实现图片下载和缓存功能。

picasso使用简单,如下

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);  
主要有以下一些特性:

  • 在adapter中回收和取消当前的下载;
  • 使用最少的内存完成复杂的图形转换操作;
  • 自动的内存和硬盘缓存;
  • 图形转换操作,如变换大小,旋转等,提供了接口来让用户可以自定义转换操作;
  • 加载载网络或本地资源;
代码分析 Cache,缓存类
Lrucacha,主要是get和set方法,存储的结构采用了LinkedHashMap,这种map内部实现了lru算法(Least Recently Used 近期最少使用算法)。
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. this.map = new LinkedHashMap(0, 0.75f, true);  
最后一个参数的解释:
true if the ordering should be done based on the last access (from least-recently accessed to most-recently accessed), and false if the ordering should be the order in which the entries were inserted.
因为可能会涉及多线程,所以在存取的时候都会加锁。而且每次set操作后都会判断当前缓存区是否已满,如果满了就清掉最少使用的图形。代码如下
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. private void trimToSize(int maxSize) {  
  2.         while (true) {  
  3.             String key;  
  4.             Bitmap value;  
  5.             synchronized (this) {  
  6.                 if (size 
关注
打赏
1482932726
查看更多评论
立即登录/注册

微信扫码登录

0.0604s