Design a simple Android image loading framework

At present, there are too many excellent image loading frameworks for Android, such as volley, Picasso, imageloader, glass and so on. However, as a program ape, it is very important to understand the implementation principle. Only by understanding can we use it better. Therefore, today I will simply design a network loading picture framework. The main thing is to be familiar with the network loading mechanism of pictures.

Generally speaking, an excellent image loader should have the following functions:

Picture compression

Memory cache

Disk cache

Synchronous loading of pictures

Asynchronous loading of pictures

Network pull

Let's introduce it from the above aspects:

1. Image compression (effectively reducing the probability of OOM)

I have introduced the image compression function in the efficient loading of bitmap. I won't talk about it here. Go directly to the code. Here, a class is directly abstracted to complete the image compression function.

2. Memory cache and disk cache

Cache directly selects lrucache and disklrucache to complete memory cache and disk cache.

First initialize it:

After creation, you need to provide methods to add and obtain functions. First, let's look at the memory cache.

Memory caching is relatively simple, while disk caching is much more complex. Lrudiskcache does not directly provide a method to implement it, but to add and read the file system through editor and snapshot.

First, take a look at the editor, which provides commit and abort methods to commit and revoke writes to the file system.

Snapshot, through which the FileInputStream corresponding to the disk cache object can be obtained, but the FileInputStream cannot be compressed conveniently, so the compressed picture is loaded through the filedescriptor, and finally the loaded bitmap is added to the memory cache.

3. Synchronous loading

Synchronous loading requires external calls in sub threads.

It can be seen from the method that the working process follows the following steps:

First, try to read the picture from the memory cache, then try to read the picture from the disk cache, and finally pull it from the network. This method cannot be executed in the main thread. The detection of execution environment is implemented in loadbitmapfromhttp.

4. Asynchronous loading

From the implementation of bindbitmap, the bindbitmap method will try to read the picture from the memory cache. If the reading is successful, the result will be returned directly. Otherwise, the loadbitmap method will be called in the process pool. After the picture is loaded successfully, the picture, the address of the picture and the ImageView to be bound will be encapsulated into a loaderresult object, Then send a message to the main thread through mmainhandler, so that you can set the image for ImageView in the main thread.

Let's take a look at the thread pool and handler used in the bindbitmap method. First, take a look at the thread pool thread_ POOL_ Implementation of executor.

1. Reasons for using thread pool and handler.

First of all, it cannot be implemented with ordinary threads. If ordinary threads are used to load pictures, a large number of threads may be generated with the sliding of the list, which is not conducive to the improvement of efficiency. In the implementation of handler, the looper of the main thread is directly used to construct the handler object, which makes imageloader can be constructed in non main threads. In addition, in order to solve the problem of list dislocation caused by view reuse, before setting an image for ImageView, we will check whether its URL has changed. If it has changed, we will no longer set an image for it, so as to solve the problem of list dislocation.

Summary:

The problem of image loading, especially the loading of a large number of images, has always been a troublesome problem for Android developers. This article only mentioned the most basic solution, which is good for learning.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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