Android-Universal-Image-Loader(UIL)源码解析

     Android-Universal-Image-Loader(UIL) 是android端开源的异步图片下载、缓存库,UIL旨在提供一个强大的、灵活的和高度可定制的图像加载、缓存和显示工具。它提供了许多配置选项和良好控制图像加载和缓存的过程。

    github地址:https://github.com/nostra13/Android-Universal-Image-Loader 

  注意:作者已不再维护该库(2011年11月-27日--2015年 11月12日

  作为一个优秀的图片加载库,它具有如下的特点:

 

  •    多线程图片加载(支持同步加载和异步加载)
  •    支持自定义ImageLoader配置(包括线程池、下载器、解码器、内存和磁盘缓存、图片显示等)
  •    支持多种图片显示配置
  •    支持内存缓存和SD卡缓存
  •    监听下载过程

该库支持Android2.0以上所有系统

 

设计思路:

支持的图片来源:

用法示例:

ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance

// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view 
//  which implements ImageAware interface)
imageLoader.displayImage(imageUri, imageView);
// Load image, decode it to Bitmap and return Bitmap to callback
imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() {
    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        // Do whatever you want with Bitmap
    }
});
// Load image, decode it to Bitmap and return Bitmap synchronously
Bitmap bmp = imageLoader.loadImageSync(imageUri);
// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view 
//  which implements ImageAware interface)
imageLoader.displayImage(imageUri, imageView, options, 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) {
        ...
    }
}, new ImageLoadingProgressListener() {
    @Override
    public void onProgressUpdate(String imageUri, View view, int current, int total) {
        ...
    }
});
// Load image, decode it to Bitmap and return Bitmap to callback
ImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit to this size
imageLoader.loadImage(imageUri, targetSize, options, new SimpleImageLoadingListener() {
    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        // Do whatever you want with Bitmap
    }
});
// Load image, decode it to Bitmap and return Bitmap synchronously
ImageSize targetSize = new ImageSize(80, 50); // result Bitmap will be fit to this size
Bitmap bmp = imageLoader.loadImageSync(imageUri, targetSize, options);

 

   

当应用打算从网络请求一张图片时,我们期待程序首先从内存中去获取,如果内存中没有那就从存储设备中去获取,如果存储设备中也没有,那就从网络下载这张图片。所以本节我们学习如何在内存中缓存图片。UIL内存缓存的类结构图:

  •        

 

        

1.作为一个缓存工具类,首先其应该具有如下功能:

  

 2.其次,要考虑数据的存储方式和LRUD实现(UIL使用一个Map以弱引用的方式存储外界的缓存对象)

3.既然是内存缓存,我们就需要考虑缓存总容量,这里用图片的强应用计算他的大小

至此,一个内存缓存工具类的要素都已指出。下面我们来观察具体的实现。

前面我们说,缓存的总用量是有限的,那么就需要考虑当缓存满时,先淘汰那些对象,这就涉及缓存策略的问题了。

 

UIL提供了各种策略缓存类

 

FIFO

 

最大位图

LRU 利用了LinkedHashMap的访问特性

Linked内部含有一个private transient Entry header;来记录元素插入的顺序或者是元素被访问的顺序。利用这个线性结构的对象,可以帮助记录entry加入的前后顺序或者记录entry被访问的频率(最少被访问的entry靠前,最近访问的entry靠后)。大致的过程如下:

new LinkedHashMap(10, 0.75, true);
其中前面两个参数就是HashMap构造函数需要的参数,后面的true表明LinkedHashMap按照访问的次序来排序
按照访问的次序来排序的含义:当调用LinkedHashMap的get(key)或者put(key, value)时,碰巧key在map中被包含,那么LinkedHashMap会将key对象的entry放在线性结构的最后。
按照插入顺序来排序的含义:调用get(key), 或者put(key, value)并不会对线性结构产生任何的影响。

   

指定缓存时长策略

指定比较器

 

参考资料:

http://blog.csdn.net/hsuxu/article/details/7454212

http://blog.csdn.net/chunqiuwei/article/details/37662565

源码下载:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值