Android bitmap loading optimization and cache related introduction

1、 Efficient loading of bitmap

Bitmapfactory provides four methods: decodefile, decoderesource, decodestream and decodebyte array, which are used to load a bitmap object from the file system, resource, input stream and byte array respectively.

Loading bitmap efficiently is very simple, that is, bitmapfactory.options is used to load the required size pictures. Bitmapfactory.options can load the reduced picture according to a certain sampling rate and display the reduced picture in ImageView.

Pictures can be loaded efficiently through the sampling rate, and the sampling rate can be obtained in the following ways:

After the above four steps, the loaded image is the final scaled image. Of course, it may not be scaled.

The code implementation is as follows:

The actual use can be like the following. If you load a picture size of 100 * 100, you can load the picture efficiently as follows:

2、 Cache policy in Android

At present, the commonly used algorithm is LRU, that is, the least recently used algorithm. When the cache is full, the least recently used cache objects will be eliminated first

2.1 LruCache

Lrucache is a generic class. Its internal implementation mechanism is that LinkedHashMap stores external cache objects by strong reference, and provides get() and put() to access cache objects. When the cache is full, remove the earlier cache objects and add new ones. Lrucache is thread safe.

2.2 DiskLriCache

Disklrucache is used to implement storage device caching, that is, disk caching.

2.2.1 creation of disklrucache

Since it is not part of the Android SDK, it cannot be created through the construction method. The open () method is provided for its own creation

Typical disklrucache creation process

The third parameter represents the data corresponding to a single node, which is generally set to 1.

2.2.2 adding the cache of disklrucache is completed through editor, which represents the editing object of a cache object. Disklrucache does not allow editing of one cache object at the same time.

2.2.3 cache lookup of disklrucache

The cache lookup process also needs to convert the URL into a key, get a snapshot object through disklrucache's get(), and then get the cached file input stream through this object, and get the file input stream to get the bitmap object. In order to avoid oom during loading, the original image is generally not loaded directly. In the previous introduction, bitmapfactory.options is used to load a scaled image, but there is a problem with the scaling of FileInputStream in that method. The reason is that FileInputStream is an orderly file stream, and the two calls to decodestream affect the location attribute of the file stream, resulting in null in the second decodestream. To solve this problem, you can get the corresponding file descriptor through the file stream, and then load a scaled image through the bitmapfactory.decodefiledescriptor method.

3、 Implementation of imageloader

It has the functions of synchronous loading, asynchronous loading, image compression, memory cache, disk cache and network pull.

3.1 picture compression function

As mentioned earlier.

3.2 implementation of memory cache and disk cache

Select lrucache and disklrucache to complete memory cache and disk cache respectively

3.3 interface design of synchronous loading and asynchronous loading

About synchronous loading: it can be seen from the implementation of loadbitmap that its working process follows the following steps: first try to read pictures from the memory cache, then read pictures from the disk cache, and finally try to pull pictures from the network. In addition, this method can not be invoked in the main thread, otherwise it will throw an exception. Because loading pictures is a time-consuming operation.

About asynchronous loading: as can be seen from bindbitmap, binfbitmap will first try to read the results from the memory cache. If it succeeds, it will return directly. Otherwise, it will call loadbitmap () from the thread pool. After loading is successful, the picture, picture address and ImageView to be bound will be encapsulated into a loaderresult object, and a message will be sent to the main thread through mmainhandler, In this way, you can set the image for ImageView in the main thread. Asynchronous loading of images is a very useful function. Many times, the caller does not want to load images synchronously in a separate thread and set the images to the required ImageView. Therefore, imageloader needs to load images in its internal thread and set the images to the required ImageView.

The source code of imageloader can be downloaded here to view the implementation of imageloader

4、 Use of imageloader

The core is imageadapter. The core method of getview () is as follows:

For the above code imageadapter, there is no need to know the complex process of loading images in imageloader.

Optimization list stuck phenomenon:

summary

The above is the whole content of this article. I hope the content of this article can bring some help to Android developers. If you have any questions, you can leave a message.

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
分享
二维码
< <上一篇
下一篇>>