Universal Image Loader for Android 使用實例
最簡單的使用示例代碼
- <span style="white-space:pre"> </span>// 1.獲取ImageLoader實例
- ImageLoader imageLoader = ImageLoader.getInstance();
- // 2. 使用默認配置
- ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
- // 3. 初始化ImageLoader
- imageLoader.init(configuration);
- // 4. 顯示圖片時的配置
- displayImageOptions = new DisplayImageOptions.Builder().cacheInMemory().cacheOnDisc()
- .bitmapConfig(Config.RGB_565).build();
- // 5.顯示圖片
- imageLoader.displayImage(uri, imageView, displayImageOptions);
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
以下為參照原文進行的翻譯
1. Caching默認不可用. 啟用需要對DisplayImageOptions進行如下配置:
2. // Create default options which will be usedfor every
3. // displayImage(...) call if no options will be passed to this method
4. DisplayImageOptions defaultOptions= new DisplayImageOptions.Builder()
5. ...
6. .cacheInMemory()
7. .cacheOnDisc()
8. ...
9. .build();
10. ImageLoaderConfiguration config= new ImageLoaderConfiguration.Builder(getApplicationContext())
11. ...
12. .defaultDisplayImageOptions(defaultOptions)
13. ...
14. .build();
15. ImageLoader.getInstance().init(config);// Do it on Application start
16. // Then later, when you want to display image
17. ImageLoader.getInstance().displayImage(imageUrl, imageView); // Default options will be used
or this way:
DisplayImageOptions options= new DisplayImageOptions.Builder()
...
.cacheInMemory()
.cacheOnDisc()
...
.build();
ImageLoader.getInstance().displayImage(imageUrl, imageView, options); // Incoming options will be used
18. 開啟緩存後默認會緩存到外置SD卡如下地址(/sdcard/Android/data/[package_name]/cache).如果外置SD卡不存在,會緩存到手機.緩存到Sd卡需要在Manifest文件中進行如下配置
19. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
20. UIL是如何為ImageView精確定義需要的Bitmap的尺寸?它會搜索如下參數
o 獲取ImageView真實的 width和 height
o 獲取 android:layout_width 和 android:layout_height 參數
o 獲取 android:maxWidth and/or android:maxHeight 參數
o 從configuration (memoryCacheExtraOptions(int,int) option)獲取 maximum width and/or height 參數
o 獲取設備屏幕的 width and/or height
所以如果你知道ImageView的大約最大尺寸,就可以設置如下參數android:layout_width|android:layout_height or android:maxWidth|android:maxHeight 這樣會有助於正確計算當前View所需要的Bitmap尺寸,並節約內存
如果你使用UIL時經常出現 OutOfMemoryError 那你可以嚐試如下方法:
o 減少線程池大小 (.threadPoolSize(...)). 1 - 5 isrecommended.
o 在顯示選項中使用 .bitmapConfig(Bitmap.Config.RGB_565) . RGB_565模式消耗的內存比ARGB_8888模式少兩倍.
o 配置中使用 .memoryCache(newWeakMemoryCache()) 或者完全禁用在內存中緩存(don't call .cacheInMemory()).
o 在顯示選項中使用 .imageScaleType(ImageScaleType.IN_SAMPLE_INT) 或者.imageScaleType(ImageScaleType.EXACTLY).
o 避免使用 RoundedBitmapDisplayer.調用的時候它使用ARGB-8888模式創建了一個新的Bitmap對象來顯示
對於內存緩存模式 (ImageLoaderConfiguration.memoryCache(...))你可以使用已經實現好的方法.
o 緩存隻能使用強引用:
§ LruMemoryCache (Least recently used bitmap is deleted when cache size limit isexceeded緩存大小超過指定值時,刪除最近最少使用的bitmap) - Used by default for API >= 9
o 緩存使用弱引用和強引用:
§ UsingFreqLimitedMemoryCache (Least frequently used bitmap is deleted when cachesize limit is exceeded刪除最少使用bitmap)
§ LRULimitedMemoryCache (Least recently used bitmap is deletedwhen cache size limit is exceeded刪除最近最少使用bitmap) - Used by default for API < 9
§ FIFOLimitedMemoryCache (FIFOrule is used for deletion when cache sizelimit is exceeded先進先出規則刪除bitmap)
§ LargestLimitedMemoryCache (The largest bitmap is deleted when cache sizelimit is exceeded刪除最大的bitmap)
§ LimitedAgeMemoryCache (Decorator. Cached object is deleted when its ageexceeds defined value緩存對象超過定義的時間後刪除)
o 緩存隻能使用弱引用:
§ WeakMemoryCache (Unlimited cache不限製緩存)
21. 本地緩存模式可以使用以下以實現的方法 (ImageLoaderConfiguration.discCache(...)):
o UnlimitedDiscCache (The fastest cache, doesn't limit cache size不限製緩存大小) - Used by default
o TotalSizeLimitedDiscCache (Cache limited by total cache size. If cache size exceedsspecified limit then file with the most oldest last usage date will be deleted設置總緩存大小,超過時刪除最久之前的緩存)
o FileCountLimitedDiscCache (Cache limited by file count. If file count incache directory exceeds specified limit then file with the most oldest lastusage date will be deleted. Use it if your cached files are of about the samesize.設置總緩存文件數量,當到達警戒值時,刪除最久之前的緩存。如果文件的大小都一樣的時候,可以使用該模式)
o LimitedAgeDiscCache (Size-unlimited cache with limited files' lifetime.If age of cached file exceeds defined limit then it will be deleted from cache.不限製緩存大小,但是設置緩存時間,到期後刪除)
NOTE: UnlimitedDiscCache比其他方式快30%以上.
22. To displaybitmap (DisplayImageOptions.displayer(...)) you can usealready prepared implementations:
o RoundedBitmapDisplayer (Displays bitmap with rounded corners)
o FadeInBitmapDisplayer (Displays image with "fade in" animation)
23. To avoid list(grid, ...) scrolling lags you can use PauseOnScrollListener:
24. boolean pauseOnScroll= false; // or true
25. boolean pauseOnFling= true; // or false
26. PauseOnScrollListener listener= new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
27. listView.setOnScrollListener(listener);
最後更新:2017-04-03 20:19:27