UniversalImageLoader是一个开源图片加载库,内部使用了内存缓存、本地缓存、网络下载实现了加载bitmap的功能;内存缓存有LruMemoryCache(默认)等缓存策略。
二、基础用法
// 1.设置加载图片的选项
DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(false)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
.considerExifParams(true)
.build();
// 2.加载图片
ImageLoader.getInstance().loadImage(netUrl, displayImageOptions,
new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) { }
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) { }
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { }
@Override
public void onLoadingCancelled(String imageUri, View view) { }
});
三、源码分析
源码从ImageLoader.getInstance().loadImage()开始分析:
public class ImageLoader {
// 调用入口
public void loadImage(String uri, ImageSize targetImageSize, DisplayImageOptions options,
ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
checkConfiguration();
if (targetImageSize == null) {
targetImageSize = configuration.getMaxImageSize();
}
if (options == null) {
options = configuration.defaultDisplayImageOptions;
}
NonViewAware imageAware = new NonViewAware(uri, targetImageSize, ViewScaleType.CROP);
displayImage(uri, imageAware, options, listener, progressListener);
}
// 加载图片
public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options,
ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
// 1.内存缓存中是否存在,默认为LruMemoryCache
Bitmap bmp = configuration.memoryCache.ge
t(memoryCacheKey);
if (bmp != null && !bmp.isRecycled()) {
// …省略部分代码
listener.onLoadingComplete(uri, imageAware.getWrappedView(), bmp);
} else {
ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,
options, listener, progressListener, engine.getLockForUri(uri));
// 封装成LoadAndDisplayImageTask从本地缓存查找/发送网络请求
LoadAndDisplayImageTask displayTask = new LoadAndDisplayImageTask(engine, imageLoadingInfo,
defineHandler(options));
if (options.isSyncLoading()) {
displayTask.run();
} else {
engine.submit(displayTask);
}
}
}
}
四、内存缓存
内存缓存LruMemoryCache是通过LinkedHashMap实现了最近最少使用算法。
public class LruMemoryCache implements MemoryCache {
private final LinkedHashMap<String, Bitmap> map;
public LruMemoryCache(int maxSize) {
if (maxSize <= 0) {
throw new IllegalArgumentException(“maxSize <= 0”);
}
this.maxSize = maxSize;
this.map = new LinkedHashMap<String, Bitmap>(0, 0.75f, true);
}
@Override
public final Bitmap get(String key) {
if (key == null) {
throw new NullPointerException(“key == null”);
}
synchronized(this) {
return map.get(key);
}
}
/**
- Caches {@code Bitmap} for {@code key}. The Bitmap is moved to the head of the queue.
*/
@Override
public final boolean put(String key, Bitmap value) {
if (key == null || value == null) {
throw new NullPointerException(“key == null || value == null”);
}
synchronized(this) {
size += sizeOf(key, value);
Bitmap previous = map.put(key, value);
ll || value == null");
}
synchronized(this) {
size += sizeOf(key, value);
Bitmap previous = map.put(key, value);