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:
- 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.
- bitmap was added to cache more than specified time ago
- no needed bitmap in cache, go to step 2
- needed bitmap is there
- 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.
- image was added to cache more than specified time ago
- no needed image in cache, go to step 3
- needed image is there
- 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
古董了,功能老掉牙了,不要用了