Android圖片加載開源框架Universal-Image-Loader解析

1.UIL原理

a.UI:請求數據,使用唯一的Key值索引Memory Cache中的Bitmap。

b.內存緩存:緩存搜索,如果能找到Key值對應的Bitmap,則返回數據。否則執行c

c.硬盤存儲:使用唯一Key值對應的文件名,檢索SDCard上的文件。如果有對應文件,使用BitmapFactory.decode*方法,解碼Bitmap并返回數據,同時將數據寫入緩存。如果沒有對應文件,執行d。

d.下載圖片:啟動異步線程,從數據源下載數據(Web)。

e.若下載成功,將數據同時寫入硬盤和緩存,并將Bitmap顯示在UI中。


2.UIL特征


多線程下載圖片

隨意配置ImageLoader

支持二級緩存

支持加載文件系統,assets,drawable等圖片

支持圖片下載過程的監聽

。。。

特征很多,這里就不一一列舉了


3.UIL解析

ImageLoaderConfiguration是針對圖片緩存的全局配置,主要有線程類、緩存大小、磁盤大小、圖片下載與解析、日志方面的配置。

ImageLoader是具體下載圖片,緩存圖片,顯示圖片的具體執行類,它有兩個具體的方法displayImage(...)、loadImage(...),但是其實最終他們的實現都是displayImage(...)

DisplayImageOptions用于指導每一個Imageloader根據網絡圖片的狀態(空白、下載錯誤、正在下載)顯示對應的圖片,是否將緩存加載到磁盤上,下載完后對圖片進行怎么樣的處理。


4.ImageLoaderConfiguration配置


  ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(this);?                                            <span style="font-family: 微軟雅黑, 宋體, Arial;">ImageLoader.getInstance().init(configuration);</span>


使用createDefault()方法創建了一個默認的ImageLoaderConfifuration

1.開啟內存緩存,壓縮的圖片大小為屏幕的寬和高

2.開啟硬盤緩存,不壓縮圖片

3.默認的線程池為3

4.允許緩存不同尺寸的圖片

5.默認使用FIFO處理任務

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
        .diskCacheExtraOptions(480, 800, null)
        .taskExecutor(...)
        .taskExecutorForCachedImages(...)
        .threadPoolSize(3) // default
        .threadPriority(Thread.NORM_PRIORITY - 1) // default
        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
        .denyCacheImageMultipleSizesInMemory()
        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
        .memoryCacheSize(2 * 1024 * 1024)
        .memoryCacheSizePercentage(13) // default
        .diskCache(new UnlimitedDiscCache(cacheDir)) // default
        .diskCacheSize(50 * 1024 * 1024)
以上對應默認配置,如果默認配置不滿足你的要求,可以自行修改


5.DisplayImageOptions配置(根據需求自行修改


DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageOnLoading(R.drawable.ic) // resource or drawable
        .showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable
        .showImageOnFail(R.drawable.ic_error) // resource or drawable
        .resetViewBeforeLoading(false)  // default
        .delayBeforeLoading(1000)
        .cacheInMemory(false) // default
        .cacheOnDisk(false) // default
        .preProcessor(...)
        .postProcessor(...)
        .extraForDownloader(...)
        .considerExifParams(false) // default
        .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
        .bitmapConfig(Bitmap.Config.ARGB_8888) // default


1.根據網絡情況設置圖片狀態(空白,下載錯誤,正在下載)

2.圖片加載完成后是否重置view

3.設置下載延遲時間

4.是否緩存到內存或者硬盤

5.下載完成后對圖片的處理


6.ImageLoader


ImageLoader有兩個具體的方法,loadImage()和displayImage(),在項目中一般直接使用displayImage()方法,比較方便


ImageLoader.getInstance().displayImage(imageUrl, mImageView, options);

7.加載其他來源的圖片


加載其他來源的圖片時,只需要把url改掉

加載content provider圖片String contentprividerUrl = “content://media/external/audio/albumart/13"

加載assets圖片String assetsUrl = Scheme.ASSETS.wrap("image.png");  

加載drawable圖片String drawableUrl = Scheme.DRAWABLE.wrap("R.drawable.image")


8.UIL內存緩存策略


1. 只使用的是強引用緩存 

LruMemoryCache(這個類就是這個開源框架默認的內存緩存類,緩存的是bitmap的強引用,下面我會從源碼上面分析這個類)

  2.使用強引用和弱引用相結合的緩存有

  UsingFreqLimitedMemoryCache(如果緩存的圖片總量超過限定值,先刪除使用頻率最小的bitmap)

LRULimitedMemoryCache(這個也是使用的lru算法,和LruMemoryCache不同的是,他緩存的是bitmap的弱引用)

FIFOLimitedMemoryCache(先進先出的緩存策略,當超過設定值,先刪除最先加入緩存的bitmap)

LargestLimitedMemoryCache(當超過緩存限定值,先刪除最大的bitmap對象)

LimitedAgeMemoryCache(當 bitmap加入緩存中的時間超過我們設定的值,將其刪除)

  3.只使用弱引用緩存

  WeakMemoryCache(這個類緩存bitmap的總大小沒有限制,唯一不足的地方就是不穩定,緩存的圖片容易被回收掉)


9.UIL硬盤緩存策略


FileCountLimitedDiscCache(可以設定緩存圖片的個數,當超過設定值,刪除掉最先加入到硬盤的文件)LimitedAgeDiscCache(設定文件存活的最長時間,當超過這個值,就刪除該文件)

TotalSizeLimitedDiscCache(設定緩存bitmap的最大值,當超過這個值,刪除最先加入到硬盤的文件)

UnlimitedDiscCache(這個緩存類沒有任何的限制)

注:UIL默認內存緩存使用的LruMemoryCache,默認硬盤緩存使用的是UnlimitedDiscCache



10.UIL如何避免OOM

1.減少線程池的個數,在ImageLoaderConfiguration中的(.threadPoolSize)中配置,默認為3

2.在DisplayImageOptions選項中配置bitmapConfig為Bitmap.Config.RGB_565,因為默認是ARGB_8888, 使用RGB_565會比使用ARGB_8888少消耗2倍的內存

3.在ImageLoaderConfiguration中配置圖片的內存緩存為memoryCache(new WeakMemoryCache()) 或者不使用內存緩存

4.在DisplayImageOptions選項中設置.imageScaleType(ImageScaleType.IN_SAMPLE_INT)或者imageScaleType(ImageScaleType.EXACTLY)

來自:http://blog.csdn.net/elinavampire/article/details/46662213

 本文由用戶 g4yy 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!