UIL在使用的时候都是一行代码搞定,那么,究竟其中做了多少事情么,还是要从源码中一探究竟。
图片加载方法的重载集
无论你是同步(loadImage)或者异步(loadImageSync)的方式下载图片,还是显示图片(displayImage),最终调用的都是同一个方法:
displayImage(String uri, ImageAware imageAware, DisplayImageOptions options,
ImageSize targetSize, ImageLoadingListener listener, ImageLoadingProgressListener progressListener)
所以,我们应该好好分析一下这个方法!
图片加载的真实过程
按照三级缓存的思想,在检查之后,先内存再硬盘网络。
public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options,
ImageSize targetSize, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
//检查ImageLoader是否已初始化(init(ImageLoaderConfiguration configuration)),若没初始化则抛异常
checkConfiguration();
//ImageAware是对ImageView的装饰模式接口,具体实现一般是ImageViewAware
if (imageAware == null) {
throw new IllegalArgumentException(ERROR_WRONG_ARGUMENTS);
}
//加载监听器,作为图片加载任务的开始、完成、失败、取消的生命周期的回调,若未传入则使用默认的
if (listener == null) {
listener = defaultListener;
}
//图片显示配置,若未传入则使用默认的
if (options == null) {
options = configuration.defaultDisplayImageOptions;
}
//图片的URI为空
if (TextUtils.isEmpty(uri)) {
//引擎中取消此任务
engine.cancelDisplayTaskFor(imageAware);
//任务开始的回调
listener.onLoadingStarted(uri, imageAware.getWrappedView());
//如果设置了URI为空的占位图,则显示空URI占位图
if (options.shouldShowImageForEmptyUri()) {
imageAware.setImageDrawable(options.getImageForEmptyUri(configuration.resources));
} else {
imageAware.setImageDrawable(null);
}
//任务完成的回调
listener.onLoadingComplete(uri, imageAware.getWrappedView(), null);
return;
}
//获取宽高,首先以imageAware为准,其次以配置中的最大宽高(屏幕宽高)为准,
if (targetSize == null) {
targetSize = ImageSizeUtils.defineTargetSizeForView(imageAware, configuration.getMaxImageSize());
}
//根据URI和宽高生成内存中的缓存键
String memoryCacheKey = MemoryCacheUtils.generateKey(uri, targetSize);
//加入ImageEngine的队列
engine.prepareDisplayTaskFor(imageAware, memoryCacheKey);
//开始加载的回调
listener.onLoadingStarted(uri, imageAware.getWrappedView());
//根据缓存键从内存缓存中读取位图
Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);
if (bmp != null && !bmp.isRecycled()) {//目标位图在内存中并且未被回收
L.d(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey);
if (options.shouldPostProcess()) {//需要被处理
ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,
options, listener, progressListener, engine.getLockForUri(uri));
//构造处理并显示任务
ProcessAndDisplayImageTask displayTask = new ProcessAndDisplayImageTask(engine, bmp, imageLoadingInfo,
defineHandler(options));
if (options.isSyncLoading()) {//同步加载
displayTask.run();
} else {//异步加载
engine.submit(displayTask);
}
} else {//无需处理,直接展示图片
options.getDisplayer().display(bmp, imageAware, LoadedFrom.MEMORY_CACHE);
listener.onLoadingComplete(uri, imageAware.getWrappedView(), bmp);
}
} else {//目标位图需从硬盘或者网络获取
if (options.shouldShowImageOnLoading()) {//设置“加载中”占位图
imageAware.setImageDrawable(options.getImageOnLoading(configuration.resources));
} else if (options.isResetViewBeforeLoading()) {//是否将ImageAware的图片重置
imageAware.setImageDrawable(null);
}
ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,
options, listener, progressListener, engine.getLockForUri(uri));
//构造加载并显示任务
LoadAndDisplayImageTask displayTask = new LoadAndDisplayImageTask(engine, imageLoadingInfo,
defineHandler(options));
if (options.isSyncLoading()) {//同步加载
displayTask.run();
} else {//异步加载
engine.submit(displayTask);
}
}
}