Android open-source framework Universal-Image-Loader parsing, androidimageloader

Source: Internet
Author: User

Android open-source framework Universal-Image-Loader parsing, androidimageloader

Recently I want to share something with my company. In many projects, the open-source framework of Universal-Image-Loader is used. There is not much process scheduling, there is no memory read control mechanism, and there is no exception handling, learn Together

1. UIL Principle

A. UI: request data. Use a unique Key value to index Bitmap in Memory Cache.

B. Memory Cache: cache search. If the Bitmap corresponding to the Key value can be found, data is returned. Otherwise, run c.

C. Hard Disk Storage: use the file name corresponding to the unique Key value to retrieve the files on the SDCard. If a corresponding file exists, use the BitmapFactory. decode * method to decode Bitmap and return data, and write the data to the cache. If no corresponding file exists, Run d.

D. Download images: Start asynchronous threads and download data from the data source (Web ).

E. If the download is successful, write the data to the hard disk and cache at the same time, and display the Bitmap in the UI.


2. UIL features

Multi-thread image download

Configure ImageLoader at will

Supports level-3 caching

Supports loading file systems, assets, drawable, and other images

Supports image download listening.

...

There are many features. I will not list them here.


3. UIL Parsing

ImageLoaderConfiguration is a global configuration for image caching. It mainly includes Thread class, cache size, disk size, image download and parsing, and log configuration.

ImageLoader is the specific execution class for downloading, caching, and displaying images. It has two specific methods: displayImage (...), loadImage (...), but in the end, their implementations are displayImage (...)

DisplayImageOptions is used to show each Imageloader whether to load the cache to the disk based on the status of the network image (blank, download error, and being downloaded, how to process the downloaded image.


4. ImageLoaderConfiguration Configuration

ImageLoaderConfiguration configuration = ImageLoaderConfiguration. createDefault (this); transform <span style = "font-family:,, Arial;"> ImageLoader. getInstance (). init (configuration); </span>

The createDefault () method is used to create a default ImageLoaderConfifuration.

1. Enable the memory cache. The size of the compressed image is the screen width and height.

2. Enable hard disk cache without compressing Images

3. The default thread pool is 3.

4. Images of different sizes can be cached

5. By default, FIFO is used to process tasks.

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)        .memoryCacheExtraOptions(480, 800) // default = device screen dimensions        .diskCacheExtraOptions(480, 800, null)        .taskExecutor(...)        .taskExecutorForCachedImages(...)        .threadPoolSize(3) // default        .threadPriority(Thread.NORM_PRIORITY - 1) // default        .tasksProcessingOrder(QueueProcessingType.FIFO) // default        .denyCacheImageMultipleSizesInMemory()        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))        .memoryCacheSize(2 * 1024 * 1024)        .memoryCacheSizePercentage(13) // default        .diskCache(new UnlimitedDiscCache(cacheDir)) // default        .diskCacheSize(50 * 1024 * 1024)
If the default configuration does not meet your requirements, you can modify it by yourself.

5. DisplayImageOptions configuration (modify as needed)

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 image status based on network conditions (blank, download error, download in progress)

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

3. Set the download Delay Time

4. cache to memory or hard disk

5. process the image after the download is complete


6. ImageLoader

ImageLoader has two specific methods: loadImage () and displayImage (). The displayImage () method is usually used directly in projects, which is more convenient.


ImageLoader.getInstance().displayImage(imageUrl, mImageView, options);

7. load images from other sources

When loading images from other sources, you only need to change the url

Load the content provider image String contentprividerUrl = "content: // media/external/audio/albumart/13"

Load assets image String assetsUrl = Scheme. ASSETS. wrap ("image.png ");

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


8. UIL Memory Cache Policy

1. Only use strong reference Cache

LruMemoryCache (this class is the default memory cache class of this open-source framework, which caches the strong reference of bitmap. I will analyze this class from the source code below)

2. the cache that combines strong references and weak references has

UsingFreqLimitedMemoryCache (if the total number of cached images exceeds the limit, delete the bitmap with the minimum usage frequency first)

LRULimitedMemoryCache (this is also the lru algorithm used. Unlike LruMemoryCache, it caches weak references of bitmap)

FIFOLimitedMemoryCache (first-in-first-out cache policy. When the specified value is exceeded, the bitmap first added to the cache is deleted)

LargestLimitedMemoryCache (when the cache limit value is exceeded, delete the largest bitmap object first)

LimitedAgeMemoryCache (when the time when bitmap is added to the cache exceeds our set value, delete it)

3. Only use the weak reference Cache

WeakMemoryCache)


9. UIL hard disk cache policy

FileCountLimitedDiscCache (you can set the number of cached images. When the set value is exceeded, delete the files first added to the hard disk) LimitedAgeDiscCache (sets the maximum time for the file to survive. When this value is exceeded, delete the file)

TotalSizeLimitedDiscCache (sets the maximum cached bitmap value. When this value is exceeded, delete the files first added to the hard disk)

UnlimitedDiscCache (this cache class has no restrictions)

Note: The LruMemoryCache is used by UIL default memory cache, And the UnlimitedDiscCache is used by default.


10. How can UIL avoid OOM?

1. Reduce the number of thread pools. It is configured in (. threadPoolSize) in ImageLoaderConfiguration. The default value is 3.

2. Set bitmapConfig to Bitmap. Config. RGB_565 in the DisplayImageOptions option. Because the default value is ARGB_8888, using RGB_565 will consume 2 times less memory than using ARGB_8888.

3. Configure the image memory cache in ImageLoaderConfiguration to memoryCache (new WeakMemoryCache () or not use the memory cache.

4. Set. imageScaleType (ImageScaleType. IN_SAMPLE_INT) or imageScaleType (ImageScaleType. EXACTLY) in the DisplayImageOptions option)

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.