Picasso source code analysis of Android image loading tool
After seeing this, I believe you are familiar with the use of Picasso. This blog will start with the basic usage and gradually understand its design principle.
Picasso's code volume is very small among many open source frameworks. There are only 35 class files, but although the sparrow is small, it has all kinds of internal organs. All right, let's follow my footsteps and go.
Basic Usage
With (this) method
Very classic singleton mode, double check lock
One more thing to say here is that there are five implementation methods of singleton mode, namely lazy, hungry, double check lock, internal static class and enumeration. The two implementation methods of double check lock and internal static class are used most. The main advantage is that the program execution efficiency is high and it is suitable for multi-threaded operation. Next, let's look at the implementation of builder
It is also a very classic design pattern, builder pattern or buider pattern. The biggest feature is chain call, which makes the caller's code logic concise and extensible.
The advantage of reading excellent framework source code is to learn the design ideas inside and finally be able to use them in our own projects
After the analysis of the with method, we get an object of Picasso
Load (imageurl) method
There are many load overload methods, but they are relatively simple, that is, a requestcreator object is created
It is also a builder mode. A request. Builder object is obtained and assigned to the data variable.
Into (ImageView) method
This method is relatively complex. Try to describe the comments clearly. Look at the code
Explain 1 createrequest
Detailed explanation 2 enqueueandsubmit
From the name, we can see that the action is added to a queue. After several conversion processes, it runs from the Picasso class to the dispatcher class. As we mentioned above, it is a scheduler. Let's go to the dispatcher to see the implementation logic
Again, after several turns, the final implementation code is as follows
After submitting to the thread pool, wait for the thread pool to schedule. Once there is an idle thread, the run method of bitmaphunter will be executed
At this point, we have obtained the result. It is a result object, which is distributed through the dispatcher and enters the dispatcher class. The final execution method is as follows
After several turns, we finally returned to Picasso's class
The above code is easy to understand. What we pass in is a list composed of multiple bitmaphunter objects. Here, make a traversal and call the complete method. At this time, we have returned to the main line, and the picture will be displayed soon
Next, enter the deliveraction method
At this point, we enter the complete method in imageviewaction. We mentioned the role of imageviewaction class above, which is used to process the final download results. How exciting! The picture will be displayed soon ~~~
It's too hard to write source code analysis. I've described it as clearly as possible. If you don't understand anything, you can communicate with me ~ ~ ~
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.