Reprint Please specify http://write.blog.csdn.net/postedit?ref=toolbar
Click to download the required jar file
Project Description:
Android The most annoying thing is to get pictures from the network, display, recycling, any link has problems can be directly OOM , this project may help you. Universal image Loader for Android is designed to enable asynchronous loading, caching and display of Web images, and supports multi-threaded asynchronous load. It was originally derived from the project of Fedor Vlasov , and has since been reconstructed and improved on a large scale.
Feature enumeration:
- Multi-threaded download pictures, images can be from the network, file system, project folder assets and drawable medium
- Supports arbitrary configuration of imageloader, such as thread pool, picture downloader, memory cache policy, hard disk cache policy, picture display options, and other configuration
- Memory cache for image support, file system cache or SD card cache
- Support for monitoring the image download process
- Bitmap is clipped based on the size of the control (ImageView), reducing bitmap memory consumption
- Better control of the loading process of the picture, such as pause picture loading, restart loading pictures, generally used in the Listview,gridview, the sliding process to pause loading pictures, stop the slide when to load the picture
- Provides loading of pictures on a slower network
Use procedure:
Create the default Imageloader, and all operations are controlled by Imageloader . This class uses a singleton design pattern, so if you want to get the power of that class, you need to call the GetInstance () method. Before using Imageloader to display a picture, you first initialize its configuration, call Imageloaderconfiguration's init () method, and then you can implement various displays.
Create the default imageloader configuration parameter imageloaderconfiguration config = imageloaderconfiguration . Createdefault ( this); Initialize imageloader with configuration.
Custom Configuration Imageloader, as you already know, first, you need to initialize Imageloader with the Imageloaderconfiguration object. Since Imageloader is a singleton, it is only necessary to initialize it once at the beginning of the program. It is recommended that you initialize in the activity's OnCreate () method. If a imageloader has already been initialized, initializing again will have no effect. Below we create a setting through Imageloaderconfiguration.builder
File Cachedir =storageutils.getowncachedirectory (This, "Imageloader/cache"); imageloaderconfigurationconfig = new Imageloaderconfiguration. Builder (This). Memorycacheextraoptions (480, +)//maxwidth, max height, which is the maximum length of each cache file saved. Threadpoolsi Ze (3)//Line Cheng Chinega load. ThreadPriority (thread.norm_priority-2). Denycacheimagemultiplesizesinmemory () . memorycache (New Usingfreqlimitedmemorycache * 1024x768)//You can pass your own memory cache implementation/your It can be implemented in its own memory cache. Memorycachesize (2 * 1024x768 * 1024x768). Disccachesize (* 1024x768). DISCC Achefilenamegenerator (Newmd5filenamegenerator ())//The URI name to be saved is encrypted with MD5. Tasksprocessingorder (Queueprocessingtype . LIFO). Disccachefilecount (100)//number of files cached. Disccache (new Unlimiteddisccache (Cachedir))//Custom cache path . Defaultdisplayimageoptions (Displayimageoptions.createsimple ()). Imagedownloader (New BaseimagedownloaDer (this,5 *, +))//ConnectTimeout (5 s), ReadTimeout (s) time-out. Writedebuglogs ()//Remove for Releaseapp. Build ();//Start building imageloader.getinstance (). init (config);
Get Imageloader
Imageloader Imageloader Imageloader = Imageloader.getinstance ();
Use procedure:
(1) Whether the image operation participates in the cache and the image effect configuration operation
Displayimageoptions options = new Displayimageoptions.builder () . showimageonloading (r.drawable.ic_stub) // The picture when the picture is loaded . Showimageforemptyuri (R.drawable.ic_empty) //The default picture when there is no picture resource . Showimageonfail (r.drawable.ic_ Error) //load failed picture . Cacheinmemory (True) //Enable memory caching . Cacheondisk (True) //Enable external memory caching . Considerexifparams (TRUE) //enable EXIF and JPEG image formats . Displayer (new Roundedbitmapdisplayer) // Set the display style here is the rounded rectangle . Build ();
Displayimageoptions The following are all default configuration parameters can be customized according to the requirements of the configuration
private int imageresonloading = 0; private int imageresforemptyuri = 0; private int imageresonfail = 0; Private drawable imageonloading = null; Private drawable Imageforemptyuri = null; Private drawable imageonfail = null; Private Boolean resetviewbeforeloading = false; Private Boolean cacheinmemory = false; Private Boolean cacheondisk = false; Private Imagescaletype imagescaletype = imagescaletype.in_sample_power_of_2; Private Options Decodingoptions = new options (); private int delaybeforeloading = 0; Private Boolean considerexifparams = false; Private Object extrafordownloader = null; Private bitmapprocessor preprocessor = null; Private Bitmapprocessor postprocessor = null; Private Bitmapdisplayer Displayer = Defaultconfigurationfactory.createbitmapdisplayer (); Private Handler Handler = null; Private Boolean issyncloading = false;
(2) Picture loading listener here, you can set the animation or the progress bar at the time of loading and stuff like this.
Imageloadinglistener Animatefirstlistener = new Animatefirstdisplaylistener ();p rivate static class Animatefirstdisplaylistener extends Simpleimageloadinglistener { static final list<string> displayedimages = Collections.synchronizedlist (New linkedlist<string> ()); @Override public void Onloadingcomplete (String imageuri, view view, Bitmap loadedimage) { if (loadedimage! = null { ImageView ImageView = (ImageView) view; Boolean firstdisplay =!displayedimages.contains (Imageuri); if (firstdisplay) { fadeinbitmapdisplayer.animate (ImageView, n); Displayedimages.add (Imageuri);}}}
(3) Simple setup can add a picture to ImageView
Imageloader.displayimage (IMAGEURL, ImageView, Options, Animatefirstlistener);
Cache cleanup:
The cache cleanup can be based on demand, and can be ondestroy in the life cycle function of each activity or individually set up to let users clean up themselves.
@Override public void OnDestroy () { Super.ondestroy (); Imageloader.clearmemorycache (); Imageloader.cleardiskcache (); }
Girdview,listview Loading Pictures:
I believe most people use Gridview,listview to display a lot of pictures, and when we slide Gridview,listview quickly, we want to stop the loading of the image, and load the picture of the current screen when the Gridview,listview stops sliding. , this framework of course also provides this function, it is also very simple to use, it provides pauseonscrolllistener this class to control the Listview,gridview sliding process stop to load the picture, the class uses the proxy mode
Listview.setonscrolllistener (New Pauseonscrolllistener (Imageloader, Pauseonscroll, pauseonfling)); Gridview.setonscrolllistener (New Pauseonscrolllistener (Imageloader, Pauseonscroll, pauseonfling));
The first parameter is our picture loading object Imageloader, the second is to control whether to suspend loading the picture during the sliding process, if need to pause to pass true on the line, the third parameter controls the fierce sliding interface when the picture is loaded
OutOfMemoryError:
Although this framework has a good caching mechanism, effectively avoid the production of oom, the general situation of the probability of generating oom is relatively small, but does not guarantee that outofmemoryerror never happen, this framework for OutOfMemoryError do a simple catch, Make sure our program encounters Oom without being crash, but how do we improve if we use this framework often to get oom?
Reduce the number of threads in the thread pool, configured in Imageloaderconfiguration (. threadpoolsize), recommended configuration 1-5
Configure Bitmapconfig as Bitmap.Config.RGB_565 in the displayimageoptions option, because the default is argb_8888, using rgb_565 consumes twice times less memory than using argb_8888
The memory cache for the configuration picture in Imageloaderconfiguration is MemoryCache (Newweakmemorycache ()) or the memory cache is not used
Set the. Imagescaletype (Imagescaletype.in_sample_int) or Imagescaletype (ImageScaleType.EXACTLY) in the displayimageoptions option.
Through these, I believe that the use of Universal-image-loader framework has been very well understood, we use the framework when possible to use the DisplayImage () method to load the picture, LoadImage () is to callback the picture object to the Imageloadinglistener interface of the Onloadingcomplete () method, we need to manually set to ImageView above, DisplayImage () method, Using the weak references for the ImageView object, it is convenient for the garbage collector to reclaim the ImageView object, and if we want to load a fixed-size picture, the LoadImage () method needs to pass a ImageSize object. The DisplayImage () method is based on the measured value of the ImageView object, or the value set by Android:layout_width and Android:layout_height, or Android:maxwidth and/ Or android:maxheight the set value to crop the picture