Detailed explanation of Android volley picture loading function

Gituhb project

I have uploaded the Chinese annotation project of Volley source code to GitHub. Welcome to fork and start

Why write this blog

Originally, the article was maintained on GitHub, but I encountered a problem in the process of analyzing the source code of imageloader. I hope you can help answer it

Volley get web pictures

I wanted to analyze the source code of universal image loader, but I found that volley has realized the function of loading network images In fact, the loading of network pictures is also divided into several steps: 1 Get the URL of the network picture 2. Judge whether the image corresponding to the URL has local cache 3. There is a local cache. Directly use the local cache image and set it to ImageView through asynchronous callback 4. If there is no local cache, first pull it from the network, save it locally, and then set it to ImageView through asynchronous callback

Let's use the volley source code to see if volley implements network image loading according to this step

ImageRequest. java

According to volley's architecture, we first need to construct a network picture request. Volley helped us encapsulate the imagerequest class. Let's take a look at its specific implementation:

Because the volley framework has implemented the local cache of network requests, the main thing imagerequest does is parse the byte stream into a bitmap. In the parsing process, it ensures that only one bitmap is parsed at a time through static variables to prevent oom, and uses scaletype and maxwidth and maxheight set by the user to set the image size In general, the implementation of imagerequest is very simple. I won't explain it too much here The drawbacks of imagerequest are:

1. The user needs to make too many settings, including the maximum size of the picture 2. There is no memory cache for pictures, because volley's cache is disk based, and there is a process of object deserialization

ImageLoader. java

In view of the above two shortcomings, volley provides a more powerful imageloader class Among them, the key is to increase the memory cache Before explaining the source code of imageloader, you need to introduce the usage of imageloader Unlike previous request requests, imageloader does not directly throw new requests to requestqueue for scheduling. Its use method is generally divided into four steps:

• create a requestqueue object

RequestQueue queue = Volley. newRequestQueue(context);

• create an imageloader object

The imageloader constructor receives two parameters, the first is the requestqueue object and the second is the imagecache object (that is, the memory cache class. We won't give the specific implementation first. After explaining the imageloader source code, I will provide an imagecache implementation class using LRU algorithm)

• get an imagelistener object

ImageListener listener = ImageLoader. getImageListener(imageView,R.drawable.default_imgage,R.drawable.Failed_image);

• call the get method of imageloader to load network pictures

imageLoader. get(mImageUrl,listener,maxWidth,maxHeight,scaleType);

With the usage method of imageloader, let's take a look at the source code of imageloader in combination with the usage method:

Major questions

I have two major questions about the source code of imageloader?

• implementation of batchresponse method

I wonder why there is a HashMap in the imageloader class to save the batchedimagerequest collection?

After all, batchresponse is called in the callback of successful execution of a specific imagerequest. The calling code is as follows:

As can be seen from the above code, after the imagerequest is successful, the corresponding batchedimagerequest object has been obtained from minflightrequests The imagecontainers corresponding to the same imagerequest blocked at the same time are all in the mcontainers collection of batchedimagerequest In my opinion, the batchresponse method only needs to traverse the mcontainers collection corresponding to the batchedimagerequest However, in the source code of imageloader, I think it is unnecessary to construct a HashMap object mbatchedresponses to save the batchedimagerequest collection, and then carry out two-layer for loop traversal of the collection in the batchresponse method. It is really very strange. For guidance The weird code is as follows:

I think the code implementation should be:

• using the imagelistener provided by imageloader by default, I think there is a defect, that is, the problem of image flash When setting pictures for listview items, tag judgment needs to be added Because the corresponding ImageView may have been recycled

Custom L1 cache class

First of all, the so-called L1 and L2 cache refer to memory cache and hard disk cache respectively To implement L1 cache, we can use the LRU cache class provided by Android. The example code is as follows:

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