開源項目Universal Image Loader for Android
In the previous article, we’ve initialized the ImageLoader with configuration; and now, it is ready for immediate use according to its intended purpose.
在之前的章節,我們已經通過配置初始化了ImageLoader,現在,我們來介紹一下怎麼使用ImageLoader
For this, it has four overloaded methods:
有下麵四個重載的方法
void displayImage(String url, ImageView view)
void displayImage(String url, ImageView view, DisplayImageOptions options)
void displayImage(String url, ImageView view, ImageLoadingListener listener)
void displayImage(String url, ImageView view, DisplayImageOptions options, ImageLoadingListener listener)
The first option.
void displayImage(String url, ImageView view)
Everything is simple. We say, from which URL an image should be downloaded and in which ImageView it should be displayed. The view options (DisplayImageOptions) will be taken from configuration (defaultDisplayImageOptions (...)) in this case.
最簡單的方式,我們隻需要定義要顯示的圖片的URL和要顯示圖片的ImageView。這種情況下,圖片的顯示選項會使用默認的配置
The second option.
void displayImage(String url, ImageView view, DisplayImageOptions options)
We already can define certain options for a specific task. First, I’ll give an example of creating my own options:
我們可以自定義一個顯示選項。下麵是一個例子
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUrl(R.drawable.image_for_empty_url)
.cacheInMemory()
.cacheOnDisc()
.decodingType(DecodingType.MEMORY_SAVING)
.build();
Yes, Builder again. As mentioned in the first article, we can specify using DisplayImageOptions:
如同我們在第一篇文章中提到的,我們可以自定義圖片的顯示選項:
• whether to display the stub image in ImageView, while the real image is downloading, and what image should be displayed;
加載過程中是否顯示圖片的stub,如果顯示的話顯示那個
• whether to display the stub image in ImageView if empty image URL was passed, and what image should be displayed;
URL錯誤的時候,是否顯示一個stub,如果顯示的話顯示那一個
• whether to cache the loaded image in memory;
是否在內存中緩存已加載圖片
• whether to cache the downloaded image on file system.
是否緩存已下載圖片到本地
• to decode the image as quickly as possible (DecodingType.FAST) or as economical for RAM as possible (DecodingType.MEMORY_SAVING).
快速解析圖片或者經濟模式
So, we can pass these options every time by calling displayImage() method or we can specify default options in configuration for initialization; and they will be used in all cases when options weren’t explicitly passed by method calling.
所以,我們可以在每次調用 displayImage() 方法的時候傳入這些參數,或者調用默認的選項來初始化
In addition, you can "listen" the process of image downloading and displaying using the interface ImageLoadingListener:The third option.
除此之外,我們還可以通過接口ImageLoadingListener監聽圖片下載和現實的過程
public interface ImageLoadingListener {
void onLoadingStarted();
void onLoadingFailed();
void onLoadingComplete();
}
And the fourth option is the most powerful one. You can both define options and "listen" to the process.
void displayImage(String url, ImageView view, DisplayImageOptions options, ImageLoadingListener listener)
這是最強大的方法,你既可以定製選項有可以監聽過程。
Tips and tricks
小提示:
1. To perform its functions, the ImageLoader should receive correct parameters. And the point is ImageView rather than image URL. If you create an ImageView object in code (not using LayoutInflater), then pass the current Activity to constructor, and not the application context:
要想實現ImageLoader的功能,你必須傳遞進去正確的參數。關鍵點是ImageView要比URL重要。如果你在代碼中創建了一個ImageView對象,那麼在構造函數中你就要把當前的Activity傳遞進去作為Context,而不是Application作為Context
ImageView imageView = new ImageView(getApplicationContext()); //錯誤
ImageView imageView = new ImageView(MyActivity.this); //正確
ImageView imageView = new ImageView(getActivity()); // 正確 (用於 Fragments)
2. You should configure the maxImageWidthForMemoryCache(...) and maxImageHeightForMemoryCache(...) parameters in configuration only if you want to load in the ImageView images with size larger than size of the device's screen (for example, for subsequent zooming). In all other cases, you don’t need this: these parameters consider the screen size by default for saving memory when working with Bitmaps.
隻有在你需要讓Image的尺寸比當前設備的尺寸大的時候,你才需要配置maxImageWidthForMemoryCache(...)和maxImageHeightForMemoryCache(...)這兩個參數,比如放大圖片的時候。其他情況下,不需要做這些配置,因為默認的配置會根據屏幕尺寸以最節約內存的方式處理Bitmap。
3. Set thread pool size in the configuration wisely: a large pool size (> 10) will allow multiple threads to work simultaneously, which can significantly affect the UI work speed. But it can be fixed by setting a lower priority for threads: the lower priority is the more responsive UI is while ImageLoader work and the longer images are loaded. UI responsiveness is critical to the lists (smooth scrolling), so you should play around with setting of threadPoolSize(...) and threadPriority(...) parameters for selection of the optimal configuration for your application.
在設置中配置線程池的大小是非常明智的。一個大的線程池會允許多條線程同時工作,但是也會顯著的影響到UI線程的速度。但是可以通過設置一個較低的優先級來解決:當ImageLoader在使用的時候,可以降低它的優先級,這樣UI線程會更加流暢。在使用List的時候,UI 線程經常會不太流暢,所以在你的程序中最好設置threadPoolSize(...)和threadPriority(...)這兩個參數來優化你的應用。
4. memoryCacheSize(...) and memoryCache(...) settings overlap each other. Use only one of them for one configuration object.
這兩個參數會互相覆蓋,所以在Configuration中使用一個就好了
5. discCacheSize(...), discCacheFileCount(...) and discCache(...) settings overlap each other, using only one of them for one configuration object.
這三個參數會互相覆蓋,隻使用一個
6. If by using the ImageLoader in an application you always (or almost always) pass into the displayImage(...)method the same loading options (DisplayImageOptions), then a reasonable solution would be setting these options in the ImageLoader configuration as default options (defaultDisplayImageOptions(...) method). Then, you should not indicate these options by calling displayImage(...). If options aren’t explicitly given to the method, then default option will be used for this task.
如果你的程序中使用displayImage()方法時傳入的參數經常是一樣的,那麼一個合理的解決方法是,把這些選項配置在ImageLoader的設置中作為默認的選項(通過調用defaultDisplayImageOptions(...)方法)。之後調用displayImage(...)方法的時候就不必再指定這些選項了,如果這些選項沒有明確的指定給defaultDisplayImageOptions(...)方法,那調用的時候將會調用UIL的默認設置。
7. There is no significant difference between FAST and MEMORY_SAVING decoding types, but it is recommended to use FAST for all kinds of lists (where you want to display many images of small size), and MEMORY_SAVING for galleries (where you want to display images of large size).
在圖片解析的時候使用 FAST 或者 MEMORY_SAVING模式,並不會有明顯的區別。但是如果要在list中顯示的時候,建議使用FAST模式,Gallery中顯示的時候建議使用MEMORY_SAVING模式。
So, I've completed my story about the Universal Image Loader. The project sources are available on GitHub.
最後更新:2017-04-03 08:26:25