Android universal-Image-Loader原理详解


Image-Loader是一个很不错的图片加载框架,只需要简单的配置就能完成缓存,图片下载,显示图片这些工作,Image-Loader的GitHub地址 https://github.com/nostra13/Android-Universal-Image-Loader ,各位可以去这里看到Image-Loader的源码,至于有什么写的不好的地方,还望见谅。

首先看一下Image-Loader的代码结构


框架.png


cache里是缓存相关的类,core里是Image-Loader的核心功能类,utils里是一些常用的工具类。
Image-Loader的用法相信各位都比较清楚:如下:

ImageLoader imageLoader = ImageLoader.getInstance(); //获取实例
imageLoader.init(ImageLoaderConfiguration); //初始化ImageLoader
imageLoader.displayImage(imageUri, imageView); //显示图片
imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
}
});
Bitmap bmp = imageLoader.loadImageSync(imageUri); //异步获取Bitmap

用法特别简单,也非常实用,下面我们来一点一点分析一下,Image-Loader的原理。
当然在这儿必须先把这张图抛出来:


UIL_Flow.png


这张图相信大家都能看得懂,大概意思是需要加载图片的时候,先看Bitmap是否在内存中,如果在内存中就直接用PostprocessBitmap 、 DisplayBitmap去显示图片。如果内存中没有,就去看Disk 中有没有,如果Disk中有就把它拿出来放到内存中去,然后显示图片。如果DIsk中也没有就去下载这个图片然后再把它缓存到Disk中、内存中、显示图片。
这是Image-Loader加载图片的流程,大概的工作原理,具体是怎么实现的呢,下面我们来一点点分析哈
首先打开Imageloader这个类


ImageLoader.png


可以看到有一个ImageLoaderConfiguration,一个ImageLoaderEngine,一个ImageLoadingListener。这 三个东西到底是干嘛的呢?
ImageloaderConfighration是用来配置Imageloader的各个属性的,ImageloaderConfguration采用了Builder构建方法来配置ImageloaderConfiguration的各个参数看下面这张图:


ImageLoaderConfiguration.png


在这个里面有一个defaultDisplayImageOptions方法,这个方法用来设置显示图片的一些操作,比如图片的压缩方式。
这个是Imagloader的总体设计图,最顶层的是Imagloader,Imageloader实现了两个最常用的方法displayImage、loadImage。这两个方法里又需要ImagloaderEngine来实现相关Task的分发处理,然后去显示图片。


overall-design.png


其实ImageLoader所有的东西都在这个流程图中,这个流程图详细解释了显示图片ImageLoader所做的操作。对着代码研读完这个图片你就清楚整个Imagelaoder的原理了贴上代码:

if (TextUtils.isEmpty(uri)) { //uri 为空的时候
engine.cancelDisplayTaskFor(imageAware); //ImageloaderEngine 取消显示图片
listener.onLoadingStarted(uri, imageAware.getWrappedView()); //去加载默认图片
if (options.shouldShowImageForEmptyUri()) { //如果option中设置了空图片就显示空图片
imageAware.setImageDrawable(options.getImageForEmptyUri(configuration.resources));
} else {
imageAware.setImageDrawable(null); //否则就不显示
}
listener.onLoadingComplete(uri, imageAware.getWrappedView(), null); //显示完成!
return;
}
if (targetSize == null) { //如果不要求图片大小的话 就显示所获得的图片的最大值
targetSize = ImageSizeUtils.defineTargetSizeForView(imageAware, configuration.getMaxImageSize());
}
String memoryCacheKey = MemoryCacheUtils.generateKey(uri, targetSize); //从内存中取出缓存中的图片
engine.prepareDisplayTaskFor(imageAware, memoryCacheKey); //开始准备显示图片
listener.onLoadingStarted(uri, imageAware.getWrappedView()); //显示加载中的图片
Bitmap bmp = configuration.memoryCache.get(memoryCacheKey); //去出缓存中的Bitmap
if (bmp != null && !bmp.isRecycled()) { //如果图片在缓存中,并且bitmap没有被回收
L.d(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey); //打印出这个图片的memoryCacheKey
if (options.shouldPostProcess()) { //如果说在DisplayImageOptions设置了postProcess就是是否重新处理bitmap 就开始重新处理并且显示
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()) { //如果是异步显示 就直接run
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.setImageDrawable(null);
}
ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey,
options, listener, progressListener, engine.getLockForUri(uri));
LoadAndDisplayImageTask displayTask = new LoadAndDisplayImageTask(engine, imageLoadingInfo,
defineHandler(options)); //下载并且显示这个图片,这里面包含了下载图片,缓存图片处理bitmap等等操作 具体的可以去看这个LoadAndDisplyImageTask这个runnable里怎么写的,也许下文中会解释一下这个过程
if (options.isSyncLoading()) {
displayTask.run();
} else {
engine.submit(displayTask);
}
}


display-image-flow-chart.png

图片选自http://a.codekk.com/detail/Android/huxian99/Android%20Universal%20Image%20Loader%20%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90


转载  http://www.jianshu.com/p/980d0df2faae




更多相关文章

  1. 关于Android中TextView显示多个空格
  2. GifView——Android显示GIF动画
  3. Android各分辨率定义的图片规格
  4. unity在android显示界面(UnityPlayerActivity)不重复加载且app退出
  5. Android开源框架源码鉴赏:Okhttp
  6. (4.1.10) ImageView图片自适应
  7. Android如何根据当前显示配置匹配资源 layout / drawable
  8. Android--EditText控件属性汇总
  9. android之动画(一)通过AnimationDrawable控制逐帧动画

随机推荐

  1. android 删除SD卡或手机的缓存图像和文件
  2. android 随手记 倒计时
  3. Android中判断QQ、微信是否安装的方法
  4. 关闭android应用程序
  5. Android浏览器如何打开网页
  6. Android(安卓)MaterialDesign中颜色
  7. IntentService 和ResultReceiver
  8. 自定義Dialog去除背景陰影
  9. Android(安卓)利用ClipDrawable 自定义进
  10. Android按键拦截处理最佳实践范例(以Back