開源項目Universal Image Loader for Android 說明文檔 (1) 簡介

來源:互聯網
上載者:User

標籤:des   android   style   class   blog   code   



When developing applications for Android, one often facesthe problem of displaying some graphical content from the Internet. So, youshould provide image loading from the Web in an Android app, their processingand displaying with limited memory again and again. And despite the problemhomogeneity, each new project imposes its own specific requirements on thetask.

You may need to organize caching of downloaded images; ifimages are rather large, it is necessary to ensure effective working with thememory to prevent the disastrous mistake OutOfMemoryError . It is also possiblethat an image-stub has to be showed during the image loading; and maybe thesame image should be displayed in different size variations, etc.

As a result, time and resources are wasted for codeadaptation to specific needs. It is this problem what urged me on creating alibrary with open source code - Universal Image Loader for image loading in anAndroid app. Its aim is solution universalization of the above describedproblem in a flexible and configurable tool.

Currently, the library can be used everywhere, where youhave to download and display (and possibly even to cache) an image from theInternet or from the file system of your smartphone. Classic examples forpossibility of ImageLoader using are various lists, tables, galleries, whereyou need to display images from the Web.

上面一大堆話,是說作者為什麼做這個項目

現在這個類庫可以用在任意地方,例如,你需要從網路上下載和顯示圖片,或者從手機本地讀取。ImageLoader可以用的地方包括各種lists,tables,galleries

 

The main features of the ImageLoader for Android are:

主要特性包括:

?asynchronous loading and displaying images from the Internet or the SD-card;

從網路或SD卡非同步載入和顯示圖片

? ability ofcaching loaded images in memory and / or the device‘s file system;

在本地或記憶體中緩衝圖片

? ability tomonitor the loading process by means of "listeners"

通過“listener”監視載入的過程

? effectiveworking with the memory while caching images in the memory;

緩衝圖片至記憶體時,更加高效的工作

? wideopportunities to customize the tool to fit it to your needs.

高度可定製化

 

ImageLoader的可選設定項

What can be configured in the ImageLoader?

全域設定:

? themaximum size of images cached in the memory;

在記憶體中緩衝的圖片最大尺寸

? timeoutfor connection establishing and image loading;

連線逾時時間和圖片載入逾時時間

? themaximum number of simultaneously working threads for images loading;

載入圖片時使用的同時背景工作執行緒數量

? threadspriority during downloading and displaying images;

下載和顯示圖片時的線程優先順序

?implementation of disk cache (you can choose from ready-made implementations orcreate your own);

使用已定義本機快取或自訂

?implementation of cache in the memory (you can choose from ready-madeimplementations or create your own);

使用已定義記憶體緩衝或自訂

? default options of image downloading

圖片下載的預設選項

Image loading options (applied to each individual callImageLoader.displayImage(...)) provide the ability to specify:

圖片載入選項可以進行以下設定:

? whether todisplay the image-stub in the ImageView, while the real image is being loaded(if yes, then you need to specify this "stub");

當真實圖片載入成功以後,是否顯示image-stub。(如果是,需要制定stub)

? Whether tocache the downloaded image in the memory.

在記憶體中是否緩衝已下載圖片

? Whether tocache the downloaded image in the file system.

在本地是否緩衝已下載圖片

? Type ofimage decoding (the fastest or the most economical for the memory).

圖片解碼方式(快速/經濟)

As already mentioned, you can implement your own version ofthe disk cache and the cache in memory. But most likely, you will be quitesatisfied with ready solutions, most of which are caches, limited by someparameter (size, number of files) and having their own logic of self-cleaningby limit excess (FIFO, the oldest object, the largest object, the most seldomused).

如上所述,你可以實現你自己的本機快取和記憶體緩衝方法,但是通常Jar包中已實現的方法已經可以滿足你的需求。例如。。。。。。

 

There are enough configuration options, but this is not thecase as "the main principle of UNIX": "u can configureEVERYTHING. And you WILL configure everything." :) In theImageLoader case, you can customize everything, but it is not necessary at all:??the default configuration is always available and suitable in the most cases.

儘管UIL已經提供了很多設定,但是距離“UNIX的核心精神:你可以配置任何選項,你能夠配置任何選項”仍有差距。在UIL中,你可以自訂任何選項,但這並不是必須的,因為通常已經有了預設設定,而且這些設定在大多數情況下都是適用的。

Implementationpeculiarities

特定需求的實現

Few words about the project structure. Each task for image loadingand displaying (and that is, looking ahead, the call ImageLoader.displayImage(imageView, imageUrl)) is performed in a separate thread, except if the pictureis in cache in the memory - then it is just immediately displayed.here is a separate threads queue where tasks get if theneeded image is cached on the file system. If you do not have the rightimage in the cache, then the task-thread gets in the thread pool. Therefore,there are no obstacles for a fast displaying of cached images.

簡單描述一下這個項目的結構。每一個圖片的載入和顯示任務都運行在獨立的線程中,除非這個圖片緩衝在記憶體中,這種情況片會立即顯示。如果需要的圖片緩衝在本地,他們會開啟一個獨立的線程隊列。如果在緩衝中沒有正確的圖片,任務線程會從線程池中擷取,因此,快速顯示緩衝圖片時不會有明顯的障礙。

he algorithm of the taskprocessing isepresented on the scheme:

處理的規則用進行簡單的描述:

 

 

The main actors of the project can relatively be divided:

UIL的主要功能可以分為以下及部分

? the above mentionedqueue and pool of threads;

上面提到的線程隊列和線程池

? cache in thememory;

緩衝至記憶體

? disk cache;

緩衝至本地

? Image decoder,which decodes image files into Bitmap objects.

圖片解析,將圖片解析為Bitmap對象

The main class ImageLoader manages it all; the maininteraction with the user is performed through it.

最重要的類ImageLoader管理了所有的操作,和使用者的互動也主要通過該類。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.