Android universal imageloader cache images

Project introduction:

The most troublesome thing on Android is to obtain pictures, display and recycle from the network. Any problem in any link may be directly oom. This project may help you. The purpose of universal image loader for Android is to realize asynchronous network image loading, caching and display, and support multi-threaded asynchronous loading. It originally came from Fedor Vlasov's project, and since then, it has undergone large-scale reconstruction and improvement.

Characteristic enumeration:

Images can be downloaded through multiple threads. Images can come from the network, file system, project folder, assets and drawable medium. It supports arbitrary configuration of imageloader, such as thread pool, Image Downloader, memory cache strategy, hard disk cache strategy, image display options and other configurations. It supports memory cache of images, The file system cache or SD card cache supports the monitoring of the picture download process. The bitmap is cropped according to the size of the control (ImageView) to reduce the excessive memory occupied by the bitmap. It is better to control the picture loading process, such as pausing the picture loading and restarting the picture loading. It is generally used in listview and GridView, and pausing the picture loading during the sliding process, Loading pictures when sliding stops provides loading pictures on slower networks

Use process:

Create a default imageloader, and all operations are controlled by imageloader. This class uses the singleton design pattern, so if you want to obtain the strength of this class, you need to call the getInstance () method. Before using imageloader to display images, you must first initialize its configuration, call the init () method of imageloaderconfiguration, and then you can realize various displays.

Customize the configuration of imageloader. As you already know, first, you need to use the imageloaderconfiguration object to initialize the imageloader. Since imageloader is a singleton, it only needs to be initialized once at the beginning of the program. It is recommended that you initialize in the oncreate () method of the activity. If an imageloader has been initialized, re initialization will not have any effect. Let's create a setting through imageloaderconfiguration. Builder

Get imageloader

Use process:

(1) Whether the image operation participates in the cache and image effect configuration operation

Displayimageoptions the following are all the default configuration parameters. You can customize the configuration according to your needs

(2) The picture loading listener is here. You can set the animation or progress bar during loading

(3) You can add pictures to ImageView with simple settings

For local pictures, add "file: / /" before their absolute address. The network picture is written directly to the path.

Because my bag is the latest one, it may be different from the old version. I see some netizens say:

Cache Cleanup:

The cleaning of the cache can be determined according to the needs. It can be cleaned up in the lifecycle function ondestroy of each activity, or it can be set separately for the user to clean up by himself.

Girdview, listview loading pictures:

I believe most people use GridView and listview to display a large number of pictures. When we quickly slide GridView and listview, we want to stop loading pictures. When GridView and listview stop sliding, we load the pictures of the current interface. Of course, this framework also provides this function and is very simple to use, It provides the pauseonscolllistener class to control the listview. The GridView stops loading pictures during sliding. This class uses the proxy mode

The first parameter is our image loading object imageloader. The second parameter controls whether to pause loading images during sliding. If you need to pause, just pass true. The third parameter controls whether to load images when sliding the interface

OutOfMemoryError:

Although this framework has a good caching mechanism to effectively avoid the generation of oom. Generally, the probability of generating oom is relatively small, but it can not guarantee that outofmemoryerror will never occur. This framework makes a simple catch for outofmemoryerror to ensure that our program will not crash when encountering oom. However, if we use this framework, oom often occurs, How should we improve it?

Reduce the number of threads in the thread pool and configure them in (. Threadpoolsize) in imageloaderconfiguration. It is recommended to configure 1-5

In the displayimageoptions option, configure bitmapconfig to bitmap.config.rgb_ 565, because the default is ARGB_ 8888, using RGB_ 565 will be better than using ARGB_ 8888 consumes 2x less memory

Configure the memory cache of pictures in imageloaderconfiguration as memorycache (newweakmemorycache()) or do not use memory cache

Set. Imagescaletype (imagescaletype. In_sample_int) or imagescaletype (imagescaletype. Actual) in the displayimageoptions option

Through the above, I believe you have a good understanding of the use of the universal image loader framework. When using the framework, we try to use the displayimage() method to load pictures. Loadimage() is to call back the picture object to the onloadingcomplete() method of the imageloadinglistener interface. We need to manually set it to the ImageView, displayimage() In the method, weak references are used for the ImageView object to facilitate the garbage collector to recycle the ImageView object. If we want to load a fixed size image, we need to pass an ImageSize object using the loadimage() method, and the displayimage() method will be based on the measured value of the ImageView object or Android: Layout_ width and android:layout_ The value set by height, or the value set by Android: maxwidth and / or Android: maxheight

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>