天天看點

開源項目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.

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.

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模式。