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

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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android使用内存缓存图片,可以使用 LruCache 或自定义实现的内存缓存。 1. LruCache LruCache 是 Android 提供的一个可以回收不常用的 Bitmap 对象的缓存类,它的大小是通过构造函数中传入的 maxsize 来指定的。 以下是使用 LruCache 的示例代码: ``` public class ImageCache { private LruCache<String, Bitmap> mMemoryCache; public ImageCache() { int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); int cacheSize = maxMemory / 8; // 可以根据需求进行调整 mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { // 返回 Bitmap 对象的大小,单位为 KB return bitmap.getByteCount() / 1024; } }; } public void addToMemoryCache(String key, Bitmap bitmap) { if (getFromMemoryCache(key) == null) { mMemoryCache.put(key, bitmap); } } public Bitmap getFromMemoryCache(String key) { return mMemoryCache.get(key); } } ``` 2. 自定义实现的内存缓存 自定义实现的内存缓存可以根据需求进行调整和优化,以下是一个简单的示例代码: ``` public class ImageCache { private Map<String, Bitmap> mMemoryCache; public ImageCache() { mMemoryCache = new HashMap<String, Bitmap>(); } public void addToMemoryCache(String key, Bitmap bitmap) { if (getFromMemoryCache(key) == null) { mMemoryCache.put(key, bitmap); } } public Bitmap getFromMemoryCache(String key) { return mMemoryCache.get(key); } } ``` 使用内存缓存时,需要注意内存泄漏的问题,可以在 Activity 或 Fragment 的 onDestroy() 方法中释放缓存

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值