天天看點

android Universal Image Loader for Android 說明文檔 (1)

all manipulations are held by the imageloader class. it is a singletone, so to get a single instance of the class, you should call

the getinstance() method. before using imageloader to its intended

purpose (to display images), you should initialize its configuration - imageloaderconfiguration using the init

(...) method. well, then you can use all variations of the displayimage(...) method with a clear conscience.

所有的操作都由imageloader控制。該類使用單例設計模式,是以如果要擷取該類的實力,需要調用getinstance()方法。在使用imageloader顯示圖檔之前,你首先要初始化它的配置,調用imageloaderconfiguration的init()方法,然後你就可以實作各種的顯示了。

in general, the easiest option of using imageloader (with the default configuration) is shown below:

通常情況下,imageloader最簡單的使用方法如下(使用預設的配置選項):

imageview imageview = ... // view, where the image will be displayed

imageloader imageloader = imageloader.getinstance();

imageloader.init(imageloaderconfiguration.createdefault(context));

imageloader.displayimage(imageurl, imageview);

 now, let’s consider the full functionality.

現在讓我們讨論下他的全部功能:

 as you already know, you first need to initialize the imageloader using the configuration object. as the imageloader is a singleton, then it should be initialized only once for application launching. i would recommend doing it in an overloaded application.oncreate(). a

reinitializing of an already initialized imageloader will have no effect.

so, we create a configuration, it is an object of the imageloaderconfiguration class. we create it using thebuilder:

 就像你已經知道的,首先,你需要使用imageloaderconfiguration對象來初始化imageloader。由于imageloader是單例,是以在程式開始的時候隻需要初始化一次就好了。建議你在activity的oncreate()方法中初始化。如果一個imageloader已經初始化過,再次初始化不會有任何效果。下面我們通過imageloaderconfiguration.builder建立一個設定

file cachedir = storageutils.getcachedirectory(context,

"universalimageloader/cache");

imageloaderconfiguration config = new

imageloaderconfiguration.builder(getapplicationcontext())

.maximagewidthformemorycache(800)

.maximageheightformemorycache(480)

.httpconnecttimeout(5000)

.httpreadtimeout(20000)

.threadpoolsize(5)

.threadpriority(thread.min_priority + 3)

.denycacheimagemultiplesizesinmemory()

.memorycache(new usingfreqlimitedcache(2000000)) // you can pass your own memory cache implementation

.disccache(new unlimiteddisccache(cachedir)) // you can pass your own disc cache implementation

.defaultdisplayimageoptions(displayimageoptions.createsimple())

.build();

let’s consider each option:

下面上我們來讨論一下每個選項:   

• maximagewidthformemorycache() and maximageheightformemorycache() is

used for decoding images into bitmap objects. in order not to store a full-sized image in the memory, it is reduced to a size determined from the values of imageview parameters, where the image is loaded: maxwidth and maxheight (first

stage),layout_width and layout_height (second

stage). if these parameters are not defined (values fill_parentand wrap_content are

considered as uncertain), then dimensions specified by settings maximagewidthformemorycache() and maximageheightformemorycache() are

taken. the size of the original image is reduced by 2 times (recommended for fast decoding), till the width or height becomes less than the specified values;

•用于将圖檔解碼為bitmap對象。為了避免将原圖存到記憶體中,系統會根據imageview的參數來縮小圖檔的尺寸,這些參數包括maxwidth 、maxheight 、layout_width 、layout_height .如果這些參數沒有指定,尺寸将會根據maximagewidthformemorycache和maximageheightformemorycache指定。原始圖檔的尺寸将會被縮減兩次,知道寬和高比指定的值小。

o default values - size of the device’s screen.

o 預設值 - 裝置螢幕的尺寸

• httpconnecttimeout() sets the maximum waiting time (in milliseconds) for establishing an http connection;

• 設定建立http連接配接的最大逾時時間

o default value - 5 seconds

o 預設值 - 5秒

• httpreadtimeout() sets the maximum time (in milliseconds) for loading an image from the web;

• 設定從網絡上加載圖檔的最大逾時時間

o default value - 30 seconds

o 預設值 - 30秒

• threadpoolsize() sets size of the thread pool. each task on image loading and displaying is performed in a separate thread, and

those threads, in which the image uploading from the web occurs, get to the pool. thus, the pool size determines the number of threads running simultaneously. setting of a large pool size can significantly reduce the speed of the ui, for example, list scrolling

could slow down.

• 設定線程池的大小。每一個加載和顯示圖檔的任務都運作在獨立的線程中,(囧,不知道怎麼翻譯)是以,線程池的大小決定了可以同時運作的線程數,如果設定的過大,将會降低ui線程的反應速度,比如list滑動時可能會卡頓。

o default value - 5

o 預設值 - 5

• threadpriority() sets priority of all threads in the system (from 1 to 10), in which tasks are performed;

•設定目前線程的優先級(1 -- 10)

o default value - 4

o 預設值 - 4

• calling denycacheimagemultiplesizesinmemory() imposes a ban on storing different sizes of the same image in the memory. as full-size

images are stored in the disk cache, and when loading into memory, they are reduced to the size of imageview, in which they should be displayed, then there are cases when the same image has to be displayed first in a small view, and then in a big one. at the

same time, two bitmaps of different sizes representing the same image will be stored in the memory. this is the default behavior.  

• 調用該方法會禁止在記憶體中緩存同一張圖檔的多個尺寸。當把本地圖檔加載到記憶體中時,首先會把圖檔縮減至要顯示的imageview的大小,是以可能會出現一種狀況,就是會首先顯示一張圖的小圖,然後再顯示這張圖的大圖。這種情況下,同一張圖檔的兩種尺寸的bitmap會被存儲在記憶體中,這是預設的操作

the denycacheimagemultiplesizesinmemory() instruction ensures deletion of the previous size of the loaded image from cache in the

memory.

該方法會確定删除已加載圖檔緩存在記憶體中的其他尺寸的緩存。

• using memorycache(), you

can specify the implementation of cache in the memory. you can use ready-made solutions (they all are realizations of limited size-cache; where by exceeding cache size, an object is removed from it by a certain algorithm):

•  調用該方法,你可以指定在記憶體中緩存的實作。你可以使用如下這些已有的解決方案

o fifolimitedcache (the object is removed on the basis of first-in-first-out)

o 按照fifo規則清理記憶體

o largestlimitedcache (the largest-sized object is removed)

o 移除最大的緩存

o usingagelimitedcache (the object with the oldest date of access is removed)

o 按照通路時間,移除最久之前的緩存

o usingfreqlimitedcache (the most rarely used object is removed)

o 按照使用頻率,移除最少使用的緩存

alternatively, you can implement your own version of the cache by implementing the interface memorycacheaware<string, bitmap>;

當然,你也通過實作接口 memorycacheaware<string,

bitmap>來實作一個自定義的清除緩存的方法,

o default value - usingfreqlimitedcache with memory limit to 2 mb

o 預設值 -usingfreqlimitedcache  ,緩存大小2mb

memorycachesize() sets the maximum cache size in the memory. in this case, the default cache is used -usingfreqlimitedcache.

設定記憶體中緩存的大小。這種情況下,預設的緩存方法是用usingfreqlimitedcache

o default value - 2 mb

o 預設值 - 2mb

• using disccache(), you can define cash implementation in the file system. you can use ready-made solutions (where the files matching

certain urls are named as hash codes of these urls):

•disccache()設定本地緩存。你可以使用以下已實作的方法

o unlimiteddisccache (usual cache, no restrictions)

o 不限制緩存大小

o filecountlimiteddisccache (cache with limited size)

o 限制緩存的檔案數量(樓主可能把注釋寫反了)

o totalsizelimiteddisccache (cache with limited files number)

o 限制緩存檔案的總大小

alternatively, you can define your own cache implementation by disccacheaware interface.

當然你可以實作接口disccacheaware 來自定義緩存的實作方法

o default value - unlimiteddisccache

o 預設值 - unlimiteddisccache

• disccachesize(int) specifies the maximum cache size in the file system. in this case, the totalsizelimiteddisccache is used.

• 指定在本地的最大緩存大小。在這種情況下,totalsizelimiteddisccache

會被使用

• disccachefilecount(int) specifies the maximum number of files in the disk cache. in this case, the filecountlimiteddisccache

is used.

• 指定在本地緩存的檔案數量。在這種情況下,filecountlimiteddisccache 會被使用

• using defaultdisplayimageoptions(), you

can set image displaying options, which will be used for all calls of the displayimage(...) method, where custom options were not passed.

•使用defaultdisplayimageoptions你可以設定圖檔的顯示選項,這些選項會在調用displayimage(...)的時候被使用。

 i’ll discuss these options in details below.

在下面我會讨論這些選項 

we can construct a configuration object ourselves or trust a developer (i.e. me) and use the default configuration:

我們可以自定義配置選項,也可以trust me,使用我提供的預設選項:

imageloaderconfiguration config  =

          imageloaderconfiguration.createdefault(context);

thus, the configuration is created. now, the imageloader can be initialized with it:

這樣,一個設定就建立了,之後,你就可以用它來初始化imageloader

imageloader.getinstance().init(config);

that's all, the imageloader is ready to use. i'll tell you about this in the next article.

這時候imageloader就可以使用了。

繼續閱讀