Android Image Asynchronous loading Android-universal-image-loader

Source: Internet
Author: User

The blog has not been updated for nearly one months, and because of all the things that have been going on in preparation for graduation theses, there has been no time and energy to sink in and keep on learning and organizing something. Just back on track recently, just two days to see the android on the image of the asynchronous loading of open source projects, by the way to record, as the one months to reopen the blog to cook. From today onwards I will resume the blog update, but also hope that we continue to support.

What we're going to introduce today is a very broad picture of GitHub using an asynchronous load library Android-universal-image-loader, which is a very powerful project that I've ever seen, the most complete and best-performing image asynchronous loading solution. Do Android students know, Android loading a lot of pictures, because the system assigned to the image load memory size is limited, so if the load picture is very large, it is easy to report Oom exception, about this exception has a lot of solutions, I do not repeat. The following is a brief introduction to the main features and use of this open source project:

I. SUMMARY of functions

    • Multi-threaded picture loading;
    • Flexibility to change the basic configuration of the Imageloader, including the maximum number of threads, cache mode, picture display options, etc.;
    • Picture asynchronous load caching mechanism, including memory cache (soft reference) and local cache;
    • Monitoring and event processing for loading process;
    • Ability to configure display options for loading images, including picture fillet processing and loading complete display animations;
Second, basic use
    1. Download the jar package Universal-image-loader-1.8.5-with-sources.jar and import the project (this should not be taught)
    2. Configure the manifest file to add network requests and access to external storage because network requests and local caching are required
[HTML]View Plaincopy
  1. < uses-permission android:name="Android.permission.INTERNET" />   
  2. <!--Include Next permission if you want to allow UIL to the cache images on SD card --   
  3. < uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE" />   

3. Implement the Custom MyApplication class and initialize the Imageloader, note the application tag in the manifest to indicate

Android:name =". MyApplication ", otherwise the default application class is used.

[Java]View Plaincopy
  1. Public class myapplication extends application {
  2. @Override   
  3. Public void onCreate () {
  4. Super  . OnCreate ();
  5. //This configuration tuning is custom. You can tune every option, your may tune some of them,
  6. //or you can create the default configuration by   
  7. //Imageloaderconfiguration.createdefault (this);   
  8. //method.   
  9. imageloaderconfiguration config = new Imageloaderconfiguration.builder (Getapplicationcontext ())
  10. . threadpriority (Thread.norm_priority- 2)
  11. . Denycacheimagemultiplesizesinmemory ()
  12. . Disccachefilenamegenerator (new md5filenamegenerator ())
  13. . Tasksprocessingorder (Queueprocessingtype.lifo)
  14. . enableLogging () //Not necessary in common
  15. . build ();
  16. //initialize imageloader with configuration   
  17. Imageloader.getinstance (). init (config);
  18. }
  19. }


4. Configure the picture loading and display options, here are a variety of custom configuration options, you can view the doc documentation to understand, here do not repeat (too much)

[Java]View Plaincopy
  1. displayimageoptions options = New Displayimageoptions.builder ()
  2. . Showstubimage (R.drawable.ic_launcher)
  3. . Showimageforemptyuri (R.drawable.ic_launcher)
  4. . Showimageonfail (R.drawable.ic_launcher)
  5. . cacheinmemory (true)
  6. . Cacheondisc (false)
  7. . Displayer (new roundedbitmapdisplayer )
  8. . build ();

5. Load the picture using Imageloader, here to load the image asynchronously for each item in the ListView, as long as the following method is called in the adapter GetView method to complete the asynchronous list picture loading, The options are the previously defined picture loading and display option, Animatefirstlistener is the first time the picture is loaded for the listener event, with the aim of displaying a fade-in display animation that can add other events

    • In addition, this example also realizes that when the list is suspended during the slide and the user's hands are released, the function of loading will not occur when the list image is loaded asynchronously, so as to ensure the smoothness of loading. The way to do this is to enable the ListView to listen for a listener event pauseonscrolllistener that is contained by Imageloader.
    • If you set Cacheondisc (true), the cached file can be seen in the /sdcard/android/data/[package_name]/cache directory. However, it is recommended to periodically clean up the cache, otherwise the time is long, the SD card will be full, but also can be configured in the Imageloaderconfiguration SD cache policy, there are limits on the number of cache files, there is limited cache file maximum size options.
    • If an Oom exception is encountered during use, it is recommended that the setting ThreadPoolSize in Imageloaderconfiguration is appropriate between 1-5. Used when the displayoption is configured.

Third, the demo effect

The following is the demo home page, which provides three kinds of components to display the multi-graph asynchronous loading way:

The following are the ListView and GridView as well as the Viewpager load display, the loading effect is very smooth:


The above is just a brief introduction of the function and use of imageloader, the implementation of the code can see the original link and download my Project Code view, more information please refer to the Official document: Click to open the link
Project Download: Project source original address: http://www.codesocang.com/jiaocheng/shoujikaifa/8077.html

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.