UniversalImageLoader基础,通宵都要看完这个Android关键技术点

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值