Android Open source Framework Universal-image-loader is like a picture loader keeper, providing us with a wealth of features:
(1) Multi-threaded loading images (asynchronous or synchronous);
(2) Highly customizable Imageloader configuration (thread pool, picture downloader, decoder, memory and disk cache, display image options, etc.);
(3) Each display image has many custom options (stub picture, cache switch, decode option, bitmap processing and display, etc.);
(4) Support memory and image cache on disk (device's file system and SD card);
(5) Monitor the loading process (including download progress);
Down we explain how to configure the use of Universal-image-loader to load network pictures
1. Download Universal-image-loader
GitHub Downloads:
Https://github.com/nostra13/Android-Universal-Image-Loader
Maven Downloads:
http://central.maven.org/maven2/com/nostra13/universalimageloader/universal-image-loader/
2. Using Universal-image-loader
(1) First associate the Universal-image-loader library file. Then initialize the Imageloader in the application OnCreate
Public class myapplication extends application{ @Override Public void onCreate() {Super. OnCreate (); Initimageloader ( This); } Public Static void Initimageloader(Context context) {//This configuration tuning is custom. You can tune every option, your may tune some of them, //or you can create the default configuration byImageloaderconfiguration.builder config =NewImageloaderconfiguration.builder (context); Config.threadpriority (Thread.norm_priority-2); Config.denycacheimagemultiplesizesinmemory ();//Do not cache multiple-size pictures in memoryConfig.diskcachefilenamegenerator (NewMd5filenamegenerator ());//To ensure that the image name is uniqueConfig.diskcachesize ( -*1024x768*1024x768);//MiB The default size of the memory cache is: 1/8 of the app's available memoryConfig.tasksprocessingorder (QUEUEPROCESSINGTYPE.LIFO); Config.writedebuglogs ();//Remove for release app //Initialize imageloader with configuration.Imageloader.getinstance (). Init (Config.build ());//imageloader.getinstance (). Init (Imageloaderconfiguration.createdefault (this));}}
(2) Configuring application in Androidmanifest
(3) Loading the image in Myimageapadter, initializing the displayimageoptions (for ease of use, we extract it into a class)
/** * Universal-image-loader-master Options Parameters */ Public interface imageloaderoptions {Displayimageoptions options =NewDisplayimageoptions.builder (). showimageonloading (R.drawable.ic_default)//What picture is displayed during the loading of the picture. Showimageforemptyuri (R.drawable.ic_default)if the URL is empty, which image is displayed. Showimageonfail (R.drawable.ic_default)//Load picture failed to show which picture. Cacheinmemory (true)//cache the picture in memory. Cacheondisk (true)//cache the picture on your hard disk. Imagescaletype (imagescaletype.exactly)//The image will be further scaled to the extent of the zoom reference to the width of the ImageView. Bitmapconfig (Bitmap.Config.RGB_565)//This type of rendering mode is also more memory-saving. Considerexifparams (true)//will identify the orientation information of the picture //. Displayer (New Fadeinbitmapdisplayer). Build ();//animation effect that gradually appears. Displayer (NewRoundedbitmapdisplayer ( -). build ();//Fillet effect //Display the options of a large imageDisplayimageoptions pager_options =NewDisplayimageoptions.builder (). showimageonloading (R.drawable.ic_default)//What picture is displayed during the loading of the picture. Showimageforemptyuri (R.drawable.ic_default)if the URL is empty, which image is displayed. Showimageonfail (R.drawable.ic_default)//Load picture failed to show which picture. Cacheinmemory (false)//Do not cache the picture in memory. Cacheondisk (true)//cache the picture on your hard disk. Imagescaletype (imagescaletype.exactly)//The image will be further scaled to the extent of the zoom reference to the width of the ImageView. Bitmapconfig (Bitmap.Config.RGB_565)//This type of rendering mode is also more memory-saving. Considerexifparams (true)//will identify the orientation information of the picture. Displayer (NewFadeinbitmapdisplayer ( -). build ();///animation effects are gradually displayed //. Displayer (New Roundedbitmapdisplayer). Build ();//Fillet effect}
(4) Call the load image in the GetView method
/** * displayImage(uri, imageView, options, listener, progressListener); * 参数解析: * imageUrl : 图片的URL地址 * imageView : 显示图片的ImageView控件 * options : DisplayImageOptions配置信息 * listener : 图片下载情况的监听 * progressListener : 图片下载进度的监听 *///加载图片ImageLoader.getInstance().displayImage(Url.IMAGE, holder.iv_icon, ImageLoaderOptions.options);
–> expansion
Gradually show the effect of animation and rounded corners can not be set at the same time, the solution: we set the fillet effect in (3), and then discard (4) The method to load the picture, and use the following method, to this, we have the cake and eat both.
//加载图片ImageLoader.getInstance().displayImage(Url.IMAGE,holder.iv_icon, options ,new SimpleImageLoadingListener(){ @Override publicvoidonLoadingComplete(String imageUri, View view, Bitmap loadedImage) { super.onLoadingComplete(imageUri, view, loadedImage); 800);//渐渐显示的效果 }});
3. Common problems and solutions in use
(1) The problem that the picture is not displayed on the low-profile mobile phone or some mobile phone when encountering a large horizontal screen image (Width>height)
Workaround: Configure the Imageloaderconfiguration parameter in the app, It is best to set up and configure in application (as in the Myapplication.java above), only once, such as multiple configurations, the system defaults to the first configuration parameter.
imageloaderconfiguration config = new imageloaderconfiguration. Builder(Getapplicationcontext ()). Memorycacheextraoptions(480, -)//Save the maximum length and width of each cached picture. ThreadPoolSize(3)//thread pool size This is actually the default3 . ThreadPriority(Thread. Norm_priority-2)//Set Thread priority. Denycacheimagemultiplesizesinmemory()//When the same URI gets a different size picture, cache to memory only one. By default, multiple different sizes of the same picture are cached. MemoryCache(New Usingfreqlimitedmemorycache (2*1024x768*1024x768)). Memorycachesize(2*1024x768*1024x768)//Set the maximum bytes for the cache. Tasksprocessingorder(Queueprocessingtype. LIFO)//Set up picture download and display of work queue sorting. Defaultdisplayimageoptions(Displayimageoptions. Createsimple ()). Imagedownloader(New Baseimagedownloader (Getapplicationcontext (),5* +, -* +)//ConnectTimeout: Timeout time. Writedebuglogs(). Build();//Start buildingImageloader. GetInstance (). Init(config);//Global initialization of this configuration
According to the above settings and configuration to resolve the screen large image on some phones do not display the problem, it is recommended to set up and configure in application, the disadvantage of the code configuration: use one time to match.
(2) The problem of oom often occurs when a large number of images in a low-profile phone
The number of pictures in the project is large and large, and the crash information collected in the background sees a lot of oom phenomenon.
Workaround:
1) Disable in-memory cache Cacheinmemory (FALSE)
2) Reduce the size of the configured thread pool (. ThreadPoolSize (...)), recommended
3) use. Bitmapconfig (Bitmap.Config.RGB_565) in Display options because rgb_565 mode consumes twice times less memory than argb_8888 mode
4) use. MemoryCache (Newweakmemorycache ()) in configuration or completely disable in-memory caching
5) Use the. Imagescaletype (imagescaletype.exactly) or. Imagescaletype (Imagescaletype.in_sample_int) in the display options.
6) Avoid using Roundedbitmapdisplayer. Called when it uses the ARGB-8888 mode to create a new bitmap object to display
The above conditions, not necessarily all set, may change some of these options to set up can solve the problem, to be flexible to use
4. Related Use Tips
(1) You need to configure the Maximagewidthformemorycache () and Maximageheightformemorycache () parameters only if you need to make the image larger than the size of the current device. For example, when zooming a picture. In other cases, these configurations are not required because the default configuration handles bitmap in the most memory-efficient manner based on the screen size.
(2) Configure the size of the thread pool in settings. A large thread pool allows multiple threads to work at the same time, but it can also significantly affect the speed of the UI thread. However, you can fix this by setting a lower priority: When Imageloader is in use, you can lower its priority so that the UI thread is smoother. The UI thread is often less fluid when using the ListView, so it's best to set ThreadPoolSize () and threadpriority () in your program to optimize your app.
(3) The two parameters of MemoryCache () and memorycachesize () are covered with each other, so one can be used in the imageloaderconfiguration.
(4) The three parameters of Diskcachesize (), DiskCache () and Diskcachefilecount () will cover each other, using only one.
Special Note: Do not use the three parameters of Disccachesize (), Disccache (), and Disccachefilecount () because they are deprecated.
Android Picture loading frame Universal-image-loader magical