Android Open source Framework Universal-image-loader parsing

Source: Internet
Author: User

Recently to share things in the company, many projects use Universal-image-loader this open source framework, there is not too much process scheduling, no memory read control mechanism, no various exception processing, together to learn

1.UIL principle

A. UI: Request data, index the bitmap in memory cache with a unique key value.

B. Memory cache: Cache search, returns data if the bitmap corresponding to the key value can be found. Otherwise, c is executed.

c. Hard disk storage: Retrieves the files on the sdcard using the file name corresponding to the unique key value. if there is a corresponding file, use the Bitmapfactory.decode* method, decode the bitmap and return the data, and write the data to the cache. If there is no corresponding file, execute d.

d. Download the Image: Start an asynchronous thread and download data from the data source (WEB).

E. If the download succeeds, the data is written to both the hard disk and the cache, and the bitmap is displayed in the UI.


2.UIL Features

Multi-Threaded Download pictures

Random Configuration Imageloader

Supports level three cache

Support loading file system, assets,drawable and other pictures

Support for monitoring the image download process

。。。

A lot of features, not listed here


3.UIL parsing

Imageloaderconfiguration is a global configuration for the image cache, with the main thread class, cache size, disk size, image download and parsing, and log configuration.

Imageloader is specific to download pictures, cache pictures, display pictures of the specific execution class, it has two specific methods displayimage (...), LoadImage (...), but in fact, ultimately their implementation is displayimage (...)

Displayimageoptions is used to instruct each imageloader to display the corresponding picture according to the status of the network picture (blank, download error, downloading), whether to load the cache to disk, and how to deal with the picture after downloading.


4.imageloaderconfiguration Configuration

  Imageloaderconfiguration Configuration=imageloaderconfiguration.createdefault (this);?                                            <span style= "font-family: Microsoft Ya-hei, song body, Arial;" >imageloader.getinstance (). Init (configuration);</span>

Use the Createdefault () method to create a default imageloaderconfifuration

1. Open the memory cache, the compressed picture size is the width and height of the screen

2. Open the hard disk cache, do not compress the picture

3. The default thread pool is 3

4. Allow different sizes of images to be cached

5. Use FIFO Processing task by default

imageloaderconfiguration config = new Imageloaderconfiguration.builder (context)        . Memorycacheextraoptions (480, )//default = Device screen dimensions        . Diskcacheextraoptions (480, $, null)        . Taskexecutor (...).        Taskexecutorforcachedimages (...)        . ThreadPoolSize (3)//default        . ThreadPriority (thread.norm_priority-1)//default        . Tasksprocessingorder ( QUEUEPROCESSINGTYPE.FIFO)//default        . Denycacheimagemultiplesizesinmemory ()        . MemoryCache (New Lrumemorycache (2 * 1024x768 * 1024x768))        . Memorycachesize (2 * 1024x768 * 1024x768)        . Memorycachesizepercentage (+)//default< C11/>.diskcache (New Unlimiteddisccache (Cachedir))//default        . Diskcachesize (50 * 1024 * 1024)
The above corresponds to the default configuration, if the default configuration does not meet your requirements, you can modify it yourself

5.displayimageoptions configuration ( self-modification on demand )

Displayimageoptions options = new Displayimageoptions.builder ()        . showimageonloading (r.drawable.ic)//resource or Drawable        . Showimageforemptyuri (r.drawable.ic_empty)//resource or drawable        . Showimageonfail (r.drawable.ic _ERROR)//resource or drawable        . Resetviewbeforeloading (FALSE)  //default        . delaybeforeloading (1000)        . Cacheinmemory (FALSE)///default.        Cacheondisk (FALSE)///default        . Preprocessor (...)        . Postprocessor (...)        . Extrafordownloader (...)        . Considerexifparams (FALSE)//default.        Imagescaletype (imagescaletype.in_sample_power_of_2)//default        . Bitmapconfig (Bitmap.Config.ARGB_8888)//default

1. Set the picture status according to the network situation (blank, download error, downloading)

2. Whether to reset the view after the picture is loaded

3. Set download delay time

4. Whether to cache to memory or hard disk

5. Image processing after download is complete


6.ImageLoader

Imageloader has two specific methods, LoadImage () and DisplayImage (), which are generally used directly in the project DisplayImage () method, which is more convenient


Imageloader.getinstance (). DisplayImage (ImageUrl, Mimageview, Options);

7. Loading pictures from other sources

When loading images from other sources, just change the URL

Load content provider Picture string Contentprividerurl = "CONTENT://MEDIA/EXTERNAL/AUDIO/ALBUMART/13"

Load ASSETS picture String assetsurl = Scheme.ASSETS.wrap ("Image.png");

Load drawable picture String drawableurl = Scheme.DRAWABLE.wrap ("R.drawable.image")


8.UIL Memory Cache Policy

1. Use only strong reference caching

Lrumemorycache (This class is the open source framework default memory cache class, the cache is a strong reference to bitmap, below I will analyze this class from the source)

2. A cache with strong references and weak references

Usingfreqlimitedmemorycache (if the total number of cached pictures exceeds the limit, first delete the bitmap with the least frequency)

Lrulimitedmemorycache (This is also used by the LRU algorithm, and Lrumemorycache is different, he caches the weak references of bitmap)

Fifolimitedmemorycache (FIFO-First cache policy, when the set value is exceeded, first delete the bitmap that is first added to the cache)

Largestlimitedmemorycache (Delete the largest bitmap object first when exceeding the cache limit)

Limitedagememorycache (when the bitmap is added to the cache more than the value we set, delete it)

3. Use only weak reference caching

Weakmemorycache (there is no limit to the total size of this type of cache bitmap, the only thing that is not stable, cached images are easily recycled)


9.UIL Hard Disk Cache policy

Filecountlimiteddisccache (You can set the number of cached pictures, when the set value is exceeded, delete the first file added to the hard disk) Limitedagedisccache (the maximum time to set the file to survive, when the value is exceeded, delete the file)

Totalsizelimiteddisccache (Sets the maximum value of the cache bitmap, when the value is exceeded, delete the file that was first added to the hard disk)

Unlimiteddisccache (there is no limit to this cache class)

Note: UIL default memory cache uses Lrumemorycache, the default hard disk cache is Unlimiteddisccache


10.UIL How to avoid oom

1. Reduce the number of thread pools, configured in Imageloaderconfiguration (. threadpoolsize), default to 3

2. Configure Bitmapconfig as Bitmap.Config.RGB_565 in the displayimageoptions option, because the default is argb_8888, using rgb_565 will consume twice times less memory than using argb_8888

3. The memory cache for the configuration picture in Imageloaderconfiguration is MemoryCache (new Weakmemorycache ()) or the memory cache is not used

4. In the Displayimageoptions option, set the. Imagescaletype (Imagescaletype.in_sample_int) or Imagescaletype ( imagescaletype.exactly)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Open source framework Universal-image-loader parsing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.