Study on Android-Universal-Image-Loader asynchronously loading Image framework Learning

Source: Internet
Author: User

GitHub https://github.com/nostra13/Android-Universal-Image-Loader.
Let's take a look at the usage of the Android-Universal-Image-Loader framework;

1. initialize ImageLoaderConfiguration (global, initialize configuration throughout the application, configure cache, and load threads)

ImageLoaderConfiguration config = new ImageLoaderConfiguration. builder (getApplicationContext ()). threadPriority (Thread. NORM_PRIORITY-2) // sets the priority of the thread. denyCacheImageMultipleSizesInMemory () // when the same Uri obtains images of different sizes and caches them to the memory, only one image is cached. Multiple images of different sizes are cached by default. discCacheFileNameGenerator (new Md5FileNameGenerator () // sets the name of the cached file. discCacheFileCount (60) // maximum number of cached files. tasksProcessingOrder (QueueProcessingType. LIFO) // sets the order of the working queue for image download and display. build (); // Initialize ImageLoader with configurationImageLoader. getInstance (). init (config );


3. Create Image display options: DisplayImageOptions (different options are constructed based on different loaded image display options. The options mainly include display configuration, cache, and display animation during image loading );

DisplayImageOptions; options = new DisplayImageOptions. builder (). showStubImage (R. drawable. ic_launcher) // sets the image displayed during the download process. showImageForEmptyUri (R. drawable. ic_launcher) // sets the image to be displayed when the image Uri is null or incorrect. showImageOnFail (R. drawable. ic_launcher) // sets the image displayed when an error occurs during image loading/decoding. cacheInMemory (true) // whether the memory is saved in the memory. cacheOnDisc (true) // whether the certificate is saved to the SD card. displayer (new RoundedBitmapDisplayer (20 )). build ();

4. Use ImageLoader to display images

// ImageLoaderImageLoader imageLoader = ImageLoader. getInstance (); // The first parameter is uri, the second parameter is the image display imageView, and the third parameter is the image display option just constructed, the fourth parameter is the loading callback method. displayImage has many overload methods, which mediation one of them; imageLoader. displayImage (imageUrls [position], holder. image, options, loadingListener)


To learn more detailed usage, the official demo can be downloaded directly from the link above;

Hey, I started to think that the above usage seems a little more, and it feels a little troublesome, But if you study it carefully, you will find that it has powerful functions, the setting of loading thread priority, image caching, image display, and display animation are all taken into consideration. It also has excellent scalability, users can configure different display effects and loading animations as needed. The overall design is clever. Let's take a look at the powerful design of this open-source framework:

---------------------------------------------- Split line ------------------------------------------------------------------------

Framework features:
1. multi-threaded image loading (thread pool size, HTTP option yarn, thread control, etc );
2. Ability to monitor the loading process;
3. Image Display configuration interface (animations and rounded corners can be expanded );
4. High-speed cache of memory and disk;
5. Strong scalability. R & D personnel only need to implement the interfaces It provides as needed;


The most widely used in the framework is the builder mode and Policy mode. From the perspective of the entire package structure, you can better familiarize yourself with the Framework Design and better expand it to integrate it with your own project;
The figure shows the package structure of the entire open-source framework:


I will not go into details here. The framework is well-named and basically well-known. It consists of several modules:

1. cache module;
Structure of the cache module package:

Which of the following are disc and memZ internal? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys + 31srHudm3vczhuam4 + keys/2 qOsy/zm4bmp upjby87sw8fn4rk/keys + keys/keys /Jo7s8L3A + CjxwPiDV4tH51/keys + MqGiveLC68Sjv + mjujxicj4KIM/CzbzOqr3iwuvEo7/keys + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140403/2014040309150567.png" alt = "\">

The structure is similar to that of the cache module. We mainly focus on the ImageDecoder interface, which has only one method, Bitmap decode (ImageDecodingInfo imageDecodingInfo ), it uses the decoding information provided by imageDecodingInfo for decoding to obtain the bitmap we need. Here we should understand the functions of this module!

The official website only provides us with an implementation class, namely BaseImageDecoder. The implementation details are not recorded here.
3. display module:
Shows the module package structure:

Amount, how familiar the structure is, you only need to look at an interface BitmapDisplayer, there is only one method in the display, that is, the whole module has only one purpose, display pictures, as to how to display, the official website also provides several behavior strategies for us: FadeInBitmapDisplayer (gradient display), RoundedBitmapDisplayer (image rounded corner display), and RoundedVignetteBitmapDisplayer (I have never played it, it means to decorate the rounded corner, )!
Of course, this is just a little bit. How do you display it? Of course, if you are flying, you only need to implement the BitmapDisplayer interface.


4. Download module:
See the figure below:

The structure is no stranger anymore. zookeeper, the core interface of this module is ImageDownloader and image download. This interface defines an internal enumeration Scheme class and has to be overwhelmed by such powerful functions:
Scheme code:

/** Represents supported schemes(protocols) of URI. Provides convenient methods for work with schemes and URIs. */public enum Scheme {HTTP("http"), HTTPS("https"), FILE("file"), CONTENT("content"), ASSETS("assets"), DRAWABLE("drawable"), UNKNOWN("");private String scheme;private String uriPrefix;Scheme(String scheme) {this.scheme = scheme;uriPrefix = scheme + "://";}/** * Defines scheme of incoming URI * * @param uri URI for scheme detection * @return Scheme of incoming URI */public static Scheme ofUri(String uri) {if (uri != null) {for (Scheme s : values()) {if (s.belongsTo(uri)) {return s;}}}return UNKNOWN;}private boolean belongsTo(String uri) {return uri.toLowerCase(Locale.US).startsWith(uriPrefix);}/** Appends scheme to incoming path */public String wrap(String path) {return uriPrefix + path;}/** Removed scheme part ("scheme://") from incoming URI */public String crop(String uri) {if (!belongsTo(uri)) {throw new IllegalArgumentException(String.format("URI [%1$s] doesn't have expected scheme [%2$s]", uri, scheme));}return uri.substring(uriPrefix.length());}}


Scheme defines the supported download image address protocol, including the content, assets, and drawable images can be downloaded as Uris. By adding the protocol header before the assets and drawable addresses, to manage the source of the image in a unified manner, which is really high; and provides various methods to determine whether it is a supported type;

The entire display process is like this: Download --> cache --> decoding --> display, endless.

5. The listener package only provides a variety of callbacks. Here, we can see what this framework can do and whether it can meet our needs. Of course, you are familiar with it, and you can add your own callback as needed;

6. util package and tool package!

The design of the above modules is scalable. We can achieve different policy actions without modifying the overall code to meet our needs. A good framework has both powerful functions and good structure and scalability;

GHOST: the more you see it, the more empty it is. Think about the image download tool you have previously written. The aspect and function you have considered are simply too weak!

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.