Overview: Project Address: Https://github.com/nostra13/Android-Universal-Image-LoaderUIL (universal-image-loader) asynchronous image loading, Caching and display. 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. The same class library (Picasso), although Picasso has a better API, lacks customization. With the UIL Builder, you can configure almost all (the most important of which is that Picasso fails when large images are crawled and cached).
Characteristics:
- Multithreaded load image multithread image loading (async or sync)
- Broad custom configuration wide customization of Imageloader ' s configuration (thread executors, downloader, decoder, memory and disk cache, D Isplay image options, etc.)
- Many customization options for every display image call (stub images, caching switch, decoding options, BITMAP processing and displaying, etc.)
- Image cache images caching in memory and/or on disk (device ' s file system or SD card)
- Loading process monitoring listening loading process (including downloading progress)
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.
Preparing for Work installation: Maven:
<dependency> <groupId>com.nostra13.universalimageloader</groupId> <artifactId> Universal-image-loader</artifactid> <version>1.9.3</version></dependency>
Gradle:
Compile ' com.nostra13.universalimageloader:universal-image-loader:1.9.3 '
Add Network and SD card permissions: Because it is used, the picture gets to go through the network, and there are cache settings, so these 2 permissions must be there.
<uses-permission android:name= "Android.permission.INTERNET"/> <!--Include following permission if you Want to cache images on SD card-- <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE "/>
Pre-configured application or Activity class (before the first usage of Imageloader)
Create global configuration and initialize Imageloader with this config imageloaderconfiguration config = new Image Loaderconfiguration.builder (This) ... . Build (); Imageloader.getinstance (). init (config);
Acceptable URIs Examples
"Http://site.com/image.png"//From the Web "file:///mnt/sdcard/image.png"//From SD card "file:///mnt/sdcard/video.mp4"// From SD card (video thumbnail) "CONTENT://MEDIA/EXTERNAL/IMAGES/MEDIA/13"//from content provider "content://media/ EXTERNAL/VIDEO/MEDIA/13 "//from the content provider (video thumbnail)" Assets://image.png "//From Assets" drawable://"+ R.D RAWABLE.IMG//From Drawables (Non-9patch images)
Note:use drawable://only if you really need it! Always consider the native-to load drawables-imageview.setimageresource (...) instead of using of imageloader.
Example
Imageloader.displayimage (Imageuri, ImageView); Imageloader.loadimage (Imageuri, New Simpleimageloadinglistener () {@ Override public void Onloadingcomplete (String imageuri, view view, Bitmap loadedimage) {//does whatever you want With Bitmap}});//Load image, decode it to Bitmap and return Bitmap synchronouslybitmap BMP = Imageloader.loadimagesy NC (Imageuri);//Load image, decode it to Bitmap and return Bitmap to callbackimagesize targetsize = new ImageSize (80, 50); Result Bitmap'll be fit to this sizeimageloader.loadimage (Imageuri, Targetsize, options, new Simpleimageloadingliste NER () {@Override public void Onloadingcomplete (String imageuri, view view, Bitmap loadedimage) {//Do Whate ver want with Bitmap}});//Load image, decode it to Bitmap and return Bitmap synchronouslyimagesize targetsize = n EW ImageSize (80, 50); Result Bitmap would be fit to this sizebitmap bmp = Imageloader.loadimagesync (Imageuri, targetsize, Options);You can also pass
ImageloadingprogresslistenerMonitor progress.
The configuration imageloaderconfiguration should be a global object for application, and you should configure it only once.
DON ' T COPY this CODE to YOUR project! This was just example of all options using.//see the sample project how to use Imageloader correctly. File Cachedir = storageutils.getcachedirectory (context); Imageloaderconfiguration config = new Imageloaderconfiguration.builder (context). Memorycacheextraoptions (480, +)//default = Device screen dimensions Recommended. Diskcacheextraoptions (480, +, NULL)//. Recommended Diskcacheextraoptions (480, +, NULL). Taskexecutor (...) . Taskexecutorforcachedimages (...) . ThreadPoolSize (3)//default recommended 1-5. ThreadPriority (thread.norm_priority-2)//default. Tasksprocessingord ER (QUEUEPROCESSINGTYPE.FIFO)//default. Denycacheimagemultiplesizesinmemory (). MemoryCache (New LRUMEMORYCAC He (2 * 1024 * 1024))//use it with a strongly referenced cache, but it is recommended to use the weak with the strong reference or use the Weakmemorycache with a fully weakly referenced usingfreqlimitedmemorycache. Memor Ycachesize (2 * 1024x768 * 1024x768). Memorycachesizepercentage (+)//default. DiskCache (New UnlimiteddiSccache (Cachedir))//default. Diskcachesize (* 1024x768). Diskcachefilecount (+). Diskcachefil Enamegenerator (New Hashcodefilenamegenerator ())//default. Imagedownloader (new Baseimagedownloader (context))//DE Fault. Imagedecoder (New Baseimagedecoder ())//default. Defaultdisplayimageoptions (displayimageoptions.creat Esimple ())//default. Writedebuglogs (). build ();
Sample Configuration Cache Directory
File Cachedir = Storageutils.getowncachedirectory (Getapplicationcontext (), "Imageloader/cache");
. DiskCache (New Unlimiteddisccache (Cachedir))//Custom cache path
Configuring the Display Options
DON ' T COPY this CODE to YOUR project! This was just example of all options using.//see the sample project how to use Imageloader correctly. Displayimageoptions options = new Displayimageoptions.builder (). showimageonloading (r.drawable.ic_stub)//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 recommendation. Imagescale Type (imagescaletype.exactly) saves memory. Bitmapconfig (Bitmap.Config.ARGB_8888)//default recommended. Bitmapconfig (BITMAP.CONFIG.R gb_565) saves memory. Decodingoptions (...) . Displayer (New SimplebitmapdisplayeR ())//default//Recommended use of Roundedbitmapdisplayer (displays bitmap with rounded corners) and fadeinbitmapdisplayer (displays Image with "Fade in" animation. Handler (new handler ())//default. Build ();
In the above configuration:
1). Imagescaletype (Imagescaletype imagescaletype) is the 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)
Set rounded picture
Fakebitmapdisplayer () This class didn't do anything.
Fadeinbitmapdisplayer (int durationmillis) time to set the picture fade
Simplebitmapdisplayer () normal display of a picture
The problem of attention
1, the cache is not enabled by default. Can be enabled by configuring Displayimageoptions.
Create default options which is used for every// displayimage (...) call if no options would be passed to this Methoddisplayimageoptions defaultoptions = new Displayimageoptions.builder () ... . Cacheinmemory (True) . Cacheondisk (True) <pre name= "code" class= "java" >boolean Pauseonscroll = false;//or Trueboolean pauseonfling = true; or Falsepauseonscrolllistener listener = new Pauseonscrolllistener (Imageloader, Pauseonscroll, pauseonfling); Listview.setonscrolllistener (listener);
... . Build (); imageloaderconfiguration config = new Imageloaderconfiguration.builder (Getapplicationcontext ()) .... Defaultdisplayimageoptions (defaultoptions) .... Build (); Imageloader.getinstance (). init (config); Do it on application start
Then later, if you want to display Imageimageloader.getinstance (). DisplayImage (ImageUrl, ImageView); Default options would be used
or by using the following method:
Displayimageoptions options = new Displayimageoptions.builder () ... . Cacheinmemory (True) . Cacheondisk (True) ... . Build (); Imageloader.getinstance (). DisplayImage (ImageUrl, ImageView, Options); Incoming options would be used
2, in order to prevent the list (Listview,grid, etc.) rolling slow, you can use Pauseonscrolllistener
Boolean pauseonscroll = false; or Trueboolean pauseonfling = true; or Falsepauseonscrolllistener listener = new Pauseonscrolllistener (Imageloader, Pauseonscroll, pauseonfling); Listview.setonscrolllistener (listener);
A few caveats 1.
the 2 permissions mentioned above must be added or an error will occur
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 ();
Reference: Https://github.com/nostra13/Android-Universal-Image-Loader/wiki
http://blog.csdn.net/vipzjyno1/article/details/23206387
Android-universal-image-loader of asynchronous loading of Android images