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

源碼下載:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章