Android-universal-image-loader the use of the asynchronous Load Class library (Hyper-verbose configuration)

Source: Internet
Author: User

This image of the asynchronous loading and caching of the class has been used by many developers, is one of the most common open source libraries, the mainstream application, the random anti-compilation of several fire projects, can see its figure.

But some people do not know how to use this library how to configure, the information found on the Internet may be too little for the people just contact, below I will use the process I know the written down, I hope to help myself and others more in-depth understanding of the use and configuration of the library.

The download path on GitHub is: Https://github.com/nostra13/Android-Universal-Image-Loader, download the latest library file, and import it into the project Lib to use.

The Library and the demo local download Link:

First, Introduction


Android-universal-image-loader is an open source UI component program designed to provide a reusable instrument for asynchronous image loading, caching, and display. So, if you need this feature in your program, try it. Because some classes and methods have been encapsulated. We can use them directly. Instead of having to write again. In fact, writing a program in this area is still more troublesome, to consider many aspects of multi-threading, caching, memory overflow and so on. However, you can also refer to this example to write a better program yourself. Here to introduce you:



Second, the characteristics


Multi-threaded image loading
The possibility of wide tuning of Imageloader configuration (size of the thread pool, HTTP options, memory and disc cache, display image, and others)
The image of the possibility in the cache memory and/or device of the file system (or SD card)
You can "listen" during the loading process
option to customize each displayed image call delimited
Widget support
Android 1.5 + Support
Briefly describe the structure of the project: each picture's load and display tasks are run in a separate thread, unless the picture is cached in memory, and the slice is immediately displayed. If the desired picture is cached locally, they will open a separate thread queue. If there is no correct picture in the cache, the task thread is fetched from the thread pool, so there is no obvious obstacle to quickly displaying the cached picture. (the part that others have borrowed from)

Flow chart:


Iii. Methods of Use

This is an open source Android about download display image of the tool class, in this download package inside the jar file for our import project use, the specific use of the package also contains. Here is an example:

Copy
    1. <uses-permission android:name= />
    2. />

Because it is used, the picture gets to go through the network, and there are cache settings, so these 2 permissions must be there.


Many people want to know how to set up a cached directory, which can be done in the following ways:

Copy
    1. File Cachedir = Storageutils.getowncachedirectory (Getapplicationcontext (), );

This is the directory where you want the cache file: Imageloader/cache

Later in the Imageloaderconfiguration configuration file, set the

Copy
    1. . Disccache ( Unlimiteddisccache (cachedir))

method to set the image cache path for the app.


Let's start by looking at how to use this image to load the library asynchronously:


One.

First, configure the Imageloaderconfiguration class to implement the global Imageloader implementation.

You can choose to initialize the class in application.

Copy
  1. imageloaderconfiguration config = imageloaderconfiguration
  2. , )   
  3. , , Compressformat.jpeg, , )
  4. )  
  5. )
  6. Usingfreqlimitedmemorycache ( * * ))
  7. *  * )    
  8. *  * )    
  9. Md5filenamegenerator ())
  10. )   
  11. Unlimiteddisccache (Cachedir))
  12. Baseimagedownloader (Context, * , * ))

The above configuration to see individual needs to choose, not all are to be configured.

After configuring Imageloaderconfiguration, call the following methods to implement initialization:

Copy
    1. Imageloader.getinstance (). init (config);   

Note: the. Disccachefilenamegenerator () method in the Imageloaderconfiguration configuration is how the cached file is named

The method that can be called inside is 1.new md5filenamegenerator ()//using MD5 to encrypt UIL name

2.new hashcodefilenamegenerator ()//use Hashcode to encrypt UIL name

Two.

When using Imageloader for picture loading, we first instantiate the Imageloader, call the following methods to instantiate, in each layout to instantiate and then use.

Copy
    1. protected Imageloader Imageloader = Imageloader.getinstance ();

After the display of the pictures of the various formats Displayimageoptions settings:

Copy
    1. Displayimageoptions.builder ()
    2. Set picture to display when errors are displayed during image load/decode
    3. true//set whether the downloaded picture is slow to exist in memory true//set whether the downloaded picture is slow to exist in the SD card true whether to consider JPEG image EXIF parameters (rotate, flip)
    4. Set how the image is encoded to display //Set the decoding type of the picture //
    5. . delaybeforeloading (int delayinmillis)//int Delayinmillis set the pre-download delay time for you//set the picture before adding the cache, the bitmap is set //. Preprocessor (bitmapprocessor preprocessor)true//set the image to reset before downloading, reset new)
    6. New))

Follow the configuration you need to set up, if you do not need to do not configure.

Note:

In the above configuration:). Imagescaletype (Imagescaletype imagescaletype) is a way to set how the picture is scaled
Scale type Magescaletype:

Exactly: the target size at which the image will be fully scaled

Exactly_stretched: The picture zooms to the target size completely

In_sample_int: The image will be sampled in multiples of two times

In_sample_power_of_2: The picture will be lowered twice times until the next reduction step to make the image smaller target size

NONE: Picture does not adjust
2). Displayer (Bitmapdisplayer displayer) is the way to set the display of pictures

Display mode Displayer:

Roundedbitmapdisplayer (int roundpixels) to set the fillet picture

Fakebitmapdisplayer () This class didn't do anything.

Fadeinbitmapdisplayer (int durationmillis) time to set the picture fade

Simplebitmapdisplayer () after displaying a picture normally, call it as required

1. Purely for loading a picture of the default configuration

Method:

public void DisplayImage (String uri, ImageView ImageView) {}

Specific implementation:

Copy
    1. Imageloader.getinstance (). DisplayImage (ImageUrl, ImageView);   

2. Load a picture of a custom configuration

Method:

public void DisplayImage (String uri, ImageView ImageView, displayimageoptions options) {}

Concrete implementation: [Java] Copy

    1. Imageloader.getinstance (). DisplayImage (ImageUrl, imageview,options);   


3. Load monitoring when loading the picture

Method:

public void DisplayImage (String uri, Imageaware imageaware, displayimageoptions Options,imageloadinglistener Listener) {}

Imageloadinglistener is used to monitor the download of images.

Specific implementation:

Copy
  1. Imageloader.displayimage (IMAGEURL, ImageView, Options, Imageloadinglistener () {
  2. onloadingstarted () {
  3. onloadingfailed (Failreason failreason) {
  4. Onloadingcomplete (Bitmap loadedimage) {
  5. onloadingcancelled () {
  6. }});


4. Picture loading, with monitoring and loading progress bar case
Call:
public void LoadImage (String uri, ImageSize targetimagesize, displayimageoptions options,
Imageloadinglistener Listener, Imageloadingprogresslistener progresslistener) {}
Specific implementation:

Copy
  1. Imageloader.displayimage (IMAGEURL, ImageView, Options, Imageloadinglistener () {
  2. onloadingstarted () {
  3. onloadingfailed (Failreason failreason) {
  4. Onloadingcomplete (Bitmap loadedimage) {
  5. onloadingcancelled () {
  6. Imageloadingprogresslistener () {
  7. onprogressupdate (String imageuri, view view, current , total ) {
  8. });


Iv. Matters of note
1. The 2 permissions mentioned above must be added or an error will occur
The 2.ImageLoaderConfiguration must be configured and globally initialized for this configuration imageloader.getinstance (). init (config); Otherwise, an error message will also appear
3.ImageLoader determines the width and height of the image according to the ImageView Height,width.
4. If you often have oom (others see it, feel that it is necessary to mention)
① reduces the size of the thread pool in the configuration (. threadpoolsize). Recommendation 1-5;
② use. Bitmapconfig (Bitmap.config.RGB_565) instead of argb_8888;
③ use. Imagescaletype (Imagescaletype.in_sample_int) or Try.imagescaletype (imagescaletype.exactly);
④ avoid using Roundedbitmapdisplayer. He will create a new bitmap object in the argb_8888 format;
⑤ use. MemoryCache (New Weakmemorycache ()), do not use. Cacheinmemory ();

The following people asked how to load local pictures or something, then put the pro-response to add up, thank you for the shortcomings (when used when only thought to use the network pictures, so also did not consider so much).

    1. String Imageuri = ;//From Web
    2. ; From SD Card
    3. ; From content provider
    4. ; From assets
    5. + R.drawable.image; From Drawables (only images, Non-9patch)



Roughly speaking here, there may be some places are not welcome to put forward, I hope you can more easily start, use this library.

Android-universal-image-loader the use of the asynchronous Load Class library (Hyper-verbose configuration)

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.