Android Open Source Framework (ix) Picture loading frame---imageloader

Source: Internet
Author: User

I. INTRODUCTION

Each Android app usually has limited system resources, the amount of memory allocated by Android devices to each app is also capped, and the amount of memory allocated for different device configurations is not the same, with a minimum of 16MB. Pictures take up a lot of memory, especially those that are super clear. So the picture loading is easy to cause the memory overflow, and to solve these problems need a lot of relevant knowledge:
1, multi-threaded download, thread management.
2. Multi-level cache architecture design and strategy, memory cache, disk cache, cache validity processing.
3, image compression, special effects processing, animation processing.
4, the complex network situation download picture strategy, such as weak network and so on.
5, memory management, LRU algorithm, object reference, GC recovery and other optimization.

Universal Imageloader is an early open source image cache that has been used by many applications in the early days.

Universal Imageloader's github address: Https://github.com/nostra13/Android-Universal-Image-Loader
The most recent update was 3 years ago and has stopped updating. Not recommended for use in new projects.

Two. Basic use 1. Add dependency
' com.nostra13.universalimageloader:universal-image-loader:1.9.5 '
2.Application initialization
//Initialize Parametersimageloaderconfiguration config =NewImageloaderconfiguration.builder (context). threadpriority (thread.norm_priority-2)//Thread Priority. Denycacheimagemultiplesizesinmemory ()//when the same URI gets a different size picture, cache to memory, only one is cached. By default, multiple different sizes of the same picture are cached. Disccachefilenamegenerator (NewMd5filenamegenerator ())//use MD5 to save the URI name at the time of the. Tasksprocessingorder (Queueprocessingtype.lifo)//set up Work queue sorting for picture downloads and displays. Writedebuglogs ()//Print Debug Log. Build (); //Global initialization of this configurationImageloader.getinstance (). init (config);
3. Basic use
imageloader.displayimage (constants.images[position],holder.iv,options);PrivateDisplayimageoptions options =NewDisplayimageoptions.builder (). Showstubimage (R.drawable.atguigu_logo)//set up pictures to display during a picture download. Showimageforemptyuri (R.drawable.atguigu_logo)//sets the picture to be displayed when the image URI is empty or wrong. Showimageonfail (R.drawable.atguigu_logo)//Set picture of error display during picture loading or decoding. Cacheinmemory (true)//sets whether the downloaded picture is slow to exist in memory. Cacheondisk (true)//sets whether the downloaded picture is slow to exist on the SD card. Displayer (NewRoundedbitmapdisplayer ( -))//Set rounded picture. build ();//Create a configured Displayimageoption object; 
Three. Expand understanding

  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 the end 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.

The configuration of 1.ImageLoaderConfiguration is mainly global configuration, the main thread class, cache size, disk size, picture download and parsing, log configuration.
Private Staticimageloaderconfiguration config; Config=NewImageloaderconfiguration.builder (context)//start building, picture loading configuration. threadpriority (Thread.norm_priority-2)//Set Thread Priority. ThreadPoolSize (3)//the number of lines Cheng Chinega; reduces the size of the thread pool in the configuration, (. threadpoolsize). Recommended 1-5;. Denycacheimagemultiplesizesinmemory ()//set the loaded picture to have a variety of. Tasksprocessingorder (Queueprocessingtype.lifo)//Picture Load Task order. MemoryCache (NewWeakmemorycache ())//use. MemoryCache (New Weakmemorycache ()), do not use. Cacheinmemory ();. Memorycacheextraoptions (480, -)//that is, the maximum length of each cached file that is saved. Memorycachesizepercentage ( -)//image memory accounts for the application of 60%;. Diskcachefilenamegenerator (NewHashcodefilenamegenerator ())//use Hashcode to encrypt UIL name. Diskcachefilenamegenerator (NewMd5filenamegenerator ())//encrypt the URI name at the time of saving with MD5. Diskcachesize ( -*1024x768*1024x768)//cache setting size of. DiskCache (NewUnlimiteddiskcache (Cachedir))//Custom Cache Path. Diskcachefilecount ( -)//number of cached files. Denycacheimagemultiplesizesinmemory ()//Auto Scale. Imagedownloader (NewBaseimagedownloader (Context,5* +, -* +))//ConnectTimeout (5 s), ReadTimeout (s) time-out. Memorycacheextraoptions (480, -)//sets the maximum width and height of the cached picture, the default is the screen width, the maximum length of each cache file saved. Defaultdisplayimageoptions (Options)//If you need to open the caching mechanism, you need to builde an option, can be displayimageoptions.createsimple (). Writedebuglogs ()//Remove for release app. build (); Build complete (parameters can be configured without setting the whole, as needed)
2. Each of the imageloader.displayimage (...) can use the display Options.
Privatedisplayimageoptions options; Options=NewDisplayimageoptions.builder ()//start building, display the various formats of the picture. resetviewbeforeloading (true)//set whether the picture is reset before downloading, reset. Cacheinmemory (true)//Open Memory Cache. Cacheondisk (true)//turn on the hard disk cache. Displayer (NewRoundedbitmapdisplayer ( -))//is set to rounded, how many radians, and avoid using Roundedbitmapdisplayer. He will create a new bitmap object in the argb_8888 format;. Displayer (NewFadeinbitmapdisplayer ( -))//whether the picture is loaded and the animation time is gradually entered. Displayer (NewSimplebitmapdisplayer ())//display a picture normally. Bitmapconfig (Bitmap.Config.RGB_565)//set the decoding type of the picture; use. Bitmapconfig (Bitmap.config.RGB_565) instead of argb_8888;. Considerexifparams (true)//whether to consider the JPEG image exif parameter (rotate, flip). Imagescaletype (imagescaletype.exactly)//Zoom Level. Imagescaletype (Imagescaletype.in_sample_int)//Both of these configuration scales are recommended. build ();//build complete (parameters can be configured without setting the whole, as needed)Imageloader.getinstance (). init (config);//initialization complete
3.ImageLoader is the specific execution class showing the picture, it has two specific methods DisplayImage LoadImage (...)
Imageloader.getinstance (). DisplayImage (URL, ImageView, options);    

 

Android Open Source Framework (ix) Picture loading frame---imageloader

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.