Android Image loading library Picasso source analysis

Source: Internet
Author: User

Picture loading is very important in Android development and a good picture loading library is also ubiquitous. Imageloader, Picasso, Glide, fresco are excellent image loading library. The above mentioned several pictures loading library each has the characteristic. Usage and comparison, there is a lot of online. From the point of view of learning, the individual thinks it is better to start from Picasso. The code is small, and the API is beautiful, it is suitable for us to learn. Today I Picasso the source of the analysis, throw some picture loading technical details for the Garden friends reference. PS: suggest that the garden friends first roughly look at the source code. We need to load the picture 1. Loading speed is 2 faster. 3 Less resource consumption. Loading pictures can not be misplaced Picasso meet the requirements? Load faster 1. Standard strategy, memorycache+diskcache+net. Increases loading speed while guaranteeing traffic. 2.Net part, taking into account the single request loading speed and multi-request concurrency, thus improving the overall load speed. 3.MemoryCache section to improve cache efficiency through LRU policies. Resource consumption is 1 lower. Render the appropriate size picture to reduce memory. 2. Limit the concurrent picture load thread through the line pool, reduce the resource consumption. 3. The threads requesting the same picture are merged to reduce the number of threads. Loading pictures can not be misplaced adapterview will MultiplexingView,picasso ensures that the view shows the correct graph through the map<imageview,action> mechanism. As can be seen, Picasso has satisfied our need for picture loading. Some basic policy caching strategies for Picasso Memorycache+diskcache+net1.memorycache are LRU policies that hold a certain number of processed graphs (such as resize/rotate processing, which can be set directly into view). 2.DiskCache is the local cache of the network picture, the original image is cached, and may need to be processed to be set to view. 3.Net is a picture server, when MemoryCache and DiskCache can not take the picture, the network pull, the cost is highest. Picture dislocation in order to ensure that the picture will not be misplaced, Picasso maintained the Map<imageview,action> each ImageView corresponds to only one Action. If the captured picture action does not match the ImageView, discard it and wait for the correct action to finish. Performance 1. The Picasso thread pool is optimized to set threadcount based on the current device network condition. Under good network conditions, the thread pool holds a lot of threads to ensure that the download speed is fast enough. In poor network conditions (2G networks, etc.), the thread pool reduces the holding thread and ensures that the bandwidth is not blocked by multiple connections. 2.Picasso combines the image URI, resize, transform and other parameters into key to encapsulate the key into the action to make the request. The request thread Hunter the action of the same key, and when the request is complete, the action gets the picture in turn. These are some of the basic strategies of Picasso, may not be able to understand, the next combination Picasso loading imageview picture sceneTo string the process. Process and source code analysis instantiation of Picasso There are two ways 1. Picasso.with (context) This method provides the default way to generate a singleton Picasso object. 2.new Picasso.builder (context). Build () This method provides a way to customize the thread pool, cache, downloader, and so on. Get Requestcreatorpicasso as a picture loading library, the role is to download pictures. After we get the Picasso instance, the normal idea is to call Picasso.load (). Load () has four methods with different parameters, but can be divided into two categories: URI and ResourceID. URIs are also divided into file and net. The return result of load () is the Requestcreator object, Requestcreator is used to configure the load parameters. Requestcreatorrequestcreator has two functions 1. Configure the load parameters. Includes placeholder and error images, loading image size, rotation, center, and other properties. 2. Perform the load. Loaded by invoking the into (object) method. Into methodThe main process combing the following work is left to Hunter to handle Note 1: (imageview,action) is used to ensure that the ImageView with the correct action. Note 2:huntermap holding multiple hunter through key, the same hunter can correspond to multiple actionhunterhunter is a runnable, the function is to get the picture. Hunter execution Process: Executes the hunt () method in the Run () method to try to get the picture, and the result (success, failure, exception) is given to the dispatcher callback. The base class of Hunter is Bitmaphunter, but it is a template class, and the most important decode (request) method is implemented by subclasses. Hunt () methodThe main process is combed as follows: Dispatcherdispatcher is a dispatcher, called by Picasso or Hunter. Picasso or Bitmaphunter can only call the dispatcher** () method. The reason is that it is not certain that the main thread or the hunter thread is calling, so dispatcher all calls are dispatcher forwarded, and the perform** () method is called after forwarding so that the event can be manipulated in the main thread. APIas follows: Dispatchersubmit () and Dispatchercancel () Hunter Add action to invoke Dispatchersubmit (), The Hunter action is called when the Dispatchercancel () Dispatchercomplete () and dispatchererror () loads end. All call the batch method, but the complete operation adds bitmap to the cache for subsequent calls. Batch () acts as a buffer, performing performbatchcomplete () batches every 0.2s. The batch will hunterlist callback to Picasso,picasso for each action of each hunter to result in a callback. Other follow ImageView picture loading, should have a certain understanding of the source code of Picasso. But there are several relatively independent modules are not involved, the park friends directly read the source code can be. Downloader offers two options for URLConnection and okhttp, with preference for okhttp. The main additions are httpcache. Stats is mainly used for data statistics, very independent modules.

Android Image loading library Picasso source analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.