One, fast use (make sure the imageloader is initialized only once, so the picture cache is better.) )
Scene: Sets a picture of the specified URI for ImageView.
1, guide package, configure the network, read and write SD card permissions.
2, Initialize:
imageloaderconfiguration config = new Imageloaderconfiguration.builder (context). build ();
ImageLoader.getInstance.init (config);
3, parameter configuration
Displayimageoptions displayimageoptions = new Displayimageoptions.builder ()
. Showstubimage (R.drawable.ic_launcher)
. Showimageonfail (R.drawable.ic_launcher)
. Imagescaletype (imagescaletype.exactly)
. Showimageforemptyuri (R.drawable.ic_launcher). Cacheinmemory (True)
. Cacheondisc (True). Displayer (New Fadeinbitmapdisplayer (300))
. Imagescaletype (imagescaletype.exactly). build ();
4. Set the picture.
Imageloader.displayimage (uri,mimageview,displayimageoptions);
Second, function (translated from GitHub)
Source: Https://github.com/dodola/Android-Universal-Image-Loader
Function: 1, multi-threaded picture loading;
2, as far as possible to reconcile the most extensive picture loading configuration (sub-threading, download, decode, memory & HDD cache, display pictures, and others);
3, listen to the download process as much as possible;
4, as far as possible for various parameters to customize the display image call;
5, support desktop small space
Support Android version 2.0 or above.
Three, the initialization parameter introduction:
Configuration
All options are selectable in the configuration builder. Use the ones you really want to customize.
DON ' T COPY this CODE to YOUR project! This is just example of the all options using.
File Cachedir = storageutils.getcachedirectory (context); Imageloaderconfiguration config = new Imageloaderconfiguration.builder (context). Memorycacheextraoptions (480, +)//default = Device screen dimensions . Disccacheextraoptions (480, Compressformat.jpeg, Asynctask.thread_pool_executor). Taskexecutor . Taskexecutorforcachedimages (Asynctask.thread_pool_executor). ThreadPoolSize (3)//default. Threadpriorit Y (thread.norm_priority-1)//default. Tasksprocessingorder (QUEUEPROCESSINGTYPE.FIFO)//default. Denycache Imagemultiplesizesinmemory (). MemoryCache (New Lrumemorycache (2 * 1024x768)). Memorycachesize (2 * 1024 * 1 024). Disccache (New Unlimiteddisccache (Cachedir))//default. Disccachesize (* 1024x768). Discca Chefilecount (+). Disccachefilenamegenerator (New Hashcodefilenamegenerator ())//default. Imagedownloader (n EW Baseimagedownloader (context))//default.Imagedecoder (New Baseimagedecoder ())//default. Defaultdisplayimageoptions (Displayimageoptions.createsimple ())// Default. enableLogging (). build ();
Display Option
Display options that can be requested by any one of the display threads.
DON ' T COPY this CODE to YOUR project! This is just example of the all options using. 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)
If you want to load the picture from the cache, the arguments for the following two statements are set to true. . Cacheinmemory (FALSE)//default.
. Cacheondisc (FALSE)///default . Preprocessor (...). Postprocessor (...) . Extrafordownloader (...) . Imagescaletype (imagescaletype.in_sample_power_of_2)//default . Bitmapconfig (Bitmap.Config.ARGB_8888)// Default . Decodingoptions (...) . Displayer (New Simplebitmapdisplayer ())//default. Handler (new handler ())//default . Build ();
Four, use cases
Display the URI used by the picture:
String Imageuri = "Http://site.com/image.png"; From webstring Imageuri = "File:///mnt/sdcard/image.png"; From SD cardstring Imageuri = "CONTENT://MEDIA/EXTERNAL/AUDIO/ALBUMART/13"; From content providerstring Imageuri = "Assets://image.png"; From assetsstring Imageuri = "drawable://" + r.drawable.image; From Drawables (only images, Non-9patch)
Note: Use drawable://unless you really need him. Always pay attention to the use of local image loading method: Setimageresource band instead of Imageloader.
Five, useful information
1,imageloader.getinstance (). init (config); Initialize when the app is turned on.
2,<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>SD card cache is required Write permission
3,imageloader determines the width and height of the picture according to the width,height of ImageView.
4, if the oom often occurs
① 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);
④避免使用RoundedBitmapDisplayer.他会创建新的ARGB_8888格式的Bitmap对象;
⑤使用.memoryCache(new WeakMemoryCache()),不要使用.cacheInMemory();
5,内存缓存,sd卡缓存,显示图片,可以使用已经初始化过的实现;
6,为了避免使用list,grid,scroll,你可以使用
Boolean pauseonscroll = false; or Trueboolean pauseonfling = true; or Falsepauseonscrolllistener listener = new Pauseonscrolllistener (Imageloader, Pauseonscroll, pauseonfling); Listview.setonscrolllistener (listener);
Android_universal-image-load use