Android图片缓存库使用经验总结



1、Android-Universal-Image-Loader

可以高度配置的网络图片缓存库,非常灵活,用户量最多

缓存过期实现:

File cacheDir = StorageUtils.getCacheDirectory(context); // or any other folder
MemoryCacheAware<String, Bitmap> memoryCacheCore 
          = new LruMemoryCache(4 * 1024 * 1024); // or any other implementation

MemoryCacheAware<String, Bitmap> memoryCache 
          = new LimitedAgeMemoryCache<String, Bitmap>(memoryCacheCore, 15 * 60);
DiscCacheAware discCache = new LimitedAgeDiscCache(cacheDir, 15 * 60);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        .memoryCache(memoryCache)
        .discCache(discCache)
        ...
        .build();

If you use "limited age" memory cache or disc cache then bitmap or image file will be deleted from cache after timeout (actually they will be deleted during search in cache). Logic is following:

  1. Search bitmap in memory cache
    • needed bitmap is there
      • bitmap was added to cache more than specified time ago
        • delete it from memory cache, go to step 2
      • bitmap was added to cache recently
        • get the bitmap, display it. End.
    • no needed bitmap in cache, go to step 2
  2. Search image file in disc cache
    • needed image is there
      • image was added to cache more than specified time ago
        • delete it from disc cache, go to step 3
      • image was added to cache recently
        • decode image to bitmap, display it. End.
    • no needed image in cache, go to step 3
  3. Download image

Don't forget enable caching (in display options, DisplayImageOptions).

针对同一图片地址,图片会变的情况,平滑替换过期图片的实现:

方法1:重写LimitedAgeDiscCache、LimitedAgeMemoryCache的get方法

方法2:增加判断缓存是否过期的接口,如果过期则通过下载回调监听的方式更新UI。

2、 androidQuery

链式调用,有自定义图片载入效果

3、Picasso

功能单一,没有缓存过期,同androidQuery一样链式调用,载入本地文件速度慢(没有生成thumbnails)。这有一篇老外的对比:http://stackoverflow.com/questions/19995007/local-image-caching-solution-for-android-square-picasso-vs-universal-image-load

4、android-smart-image-view

古董了,功能老掉牙了,不要用了





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值