1. 程式人生 > 其它 >【Android】Universal Image Loader使用簡介

【Android】Universal Image Loader使用簡介

技術標籤:筆記

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

ImageLoaderConfiguration

是針對圖片快取的全域性配置,主要有執行緒類、快取大小、磁碟大小、圖片下載與解析、日誌方面的配置。

使用說明

//這裡的路徑可以自定義
 File cacheDir = StorageUtils.getCacheDirectory(context);
        ImageLoaderConfiguration config =
new ImageLoaderConfiguration.Builder(context) // 預設等於你的螢幕尺寸,裝置螢幕寬高 .memoryCacheExtraOptions(480, 800) // 在將下載的圖片儲存到你的sd卡之前會重新計算,壓縮。 // 這個屬性不要濫用,只有你在對應的需求時再用,因為他會使你的ImageLoader變的很慢。 .diskCacheExtraOptions(480, 800, null) //用於執行從源獲取圖片任務的 Executor,為configuration中的 taskExecutor,
// 如果為null,則會呼叫DefaultConfigurationFactory.createExecutor(…)根據配置返回一個預設的執行緒池。 .taskExecutor(null) //用於執行從快取獲取圖片任務的 Executor,為configuration中的 taskExecutorForCachedImages, // 如果為null,則會呼叫DefaultConfigurationFactory.createExecutor(…)根據配置返回一個預設的執行緒池。
.taskExecutorForCachedImages(null) // 表示核心池大小(最大併發數) 預設為3 .threadPoolSize(3) // 執行緒優先順序,預設Thread.NORM_PRIORITY - 2 .threadPriority(Thread.NORM_PRIORITY - 2) // 任務程序的順序,預設為:FIFO 先進先出 .tasksProcessingOrder(QueueProcessingType.FIFO) //設定記憶體快取不允許快取一張圖片的多個尺寸,預設允許。 .denyCacheImageMultipleSizesInMemory() //圖片記憶體快取 .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //memoryCacheSize 為 0,則設定該記憶體快取的最大位元組數為 App 最大可用記憶體的 1/8。 .memoryCacheSize(2 * 1024 * 1024) // 建立最大的記憶體快取百分比,預設為 13% .memoryCacheSizePercentage(13) // 硬碟快取路徑,預設為StorageUtils.getCacheDirectory(context) .diskCache(new UnlimitedDiskCache(cacheDir)) //硬碟快取大小 .diskCacheSize(50 * 1024 * 1024) //快取檔案數量 .diskCacheFileCount(100) // 硬碟快取檔名生成器,預設為雜湊檔名生成器 .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // 建立圖片下載器,預設是BaseImageDownloader .imageDownloader(new BaseImageDownloader(context)) // 圖片解碼器,負責將圖片輸入流InputStream轉換為Bitmap物件 .imageDecoder(new BaseImageDecoder(true)) // 圖片顯示的配置項。比如載入前、載入中、載入失敗應該顯示的佔位圖片,圖片是否需要在磁碟快取,是否需要在記憶體快取等。 .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) //是否顯示除錯log資訊 .writeDebugLogs() .build(); ImageLoader.getInstance().init(config);

DisplayImageOptions

用於指導每一個Imageloader根據網路圖片的狀態(空白、下載錯誤、正在下載)顯示對應的圖片,是否將快取載入到磁碟上,下載完後對圖片進行怎麼樣的處理。

從三者的協作關係上看,他們有點像廚房規定、廚師、客戶個人口味之間的關係。ImageLoaderConfiguration就像是廚房裡面的規定,每一個廚師要怎麼著裝,要怎麼保持廚房的乾淨,這是針對每一個廚師都適用的規定,而且不允許個性化改變。ImageLoader就像是具體做菜的廚師,負責具體菜譜的製作。DisplayImageOptions就像每個客戶的偏好,根據客戶是重口味還是清淡,每一個imageLoader根據DisplayImageOptions的要求具體執行。

使用說明

DisplayImageOptions options = new DisplayImageOptions.Builder()
        // 設定圖片在下載期間顯示的圖片
                .showImageOnLoading(R.drawable.ic_stub)
                // 設定圖片Uri為空或是錯誤的時候顯示的圖片
                .showImageForEmptyUri(R.drawable.ic_stub)
                // 設定圖片載入/解碼過程中錯誤時候顯示的圖片
                .showImageOnFail(R.drawable.ic_error)
                // 設定下載的圖片是否快取在記憶體中
                .cacheInMemory(false)
                // 設定下載的圖片是否快取在SD卡中
                .cacheOnDisc(true)
                // 保留圖片檔案頭資訊
                .considerExifParams(true)
                // 設定圖片以如何的編碼方式顯示
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                // 設定圖片的解碼型別
                .bitmapConfig(Bitmap.Config.RGB_565)
                // .decodingOptions(android.graphics.BitmapFactory.Options
                // decodingOptions)//設定圖片的解碼配置
                .considerExifParams(true)
                // 設定圖片下載前的延遲
                .delayBeforeLoading(100)// int
                // delayInMillis為你設定的延遲時間
                // 設定圖片加入快取前,對bitmap進行設定
                // .preProcessor(BitmapProcessor preProcessor)
                .resetViewBeforeLoading(true)// 設定圖片在下載前是否重置,復位
                // .displayer(new RoundedBitmapDisplayer(20))//是否設定為圓角,弧度為多少
                .displayer(new FadeInBitmapDisplayer(100))// 淡入
                .build();