Abstract: This is fast taxi mobile end architect,Android Open Source project source code parsing CODEKK Initiator Wu Update (@Trinea) on the MDCC share content, from the overall design and principle of several picture cache By comparison, friends who do not use them can also understand their implementation on certain features.
"Csdn Live Report" October 14-16th, "2015 Mobile Developers Conference · China "(Mobile Developer Conference 2015, abbreviated as MDCC 2015) is held at Crowne Plaza Hotel Beijing New Yunnan. The Conference by the world's largest Chinese it community csdn and China's most concerned all-round entrepreneurial Platform innovation Workshop jointly hosted by the "Internet of Everything, mobile first" as the theme, inviting domestic and foreign industry leaders and technical experts on mobile development hotspot, in practice to analyze the technology Scenarios and trends.
Fast Taxi mobile End architect Wu Update (trinea)
This is fast taxi mobile end architect,Android Open Source project source code parsing CODEKK Initiator Wu Update (@Trinea) on the MDCC share content (slightly changed), is also the source of the first phase of the release of the code to be introduced when the source parsing will slowly do. Comparing several image caches in terms of overall design and principle, friends who are useless to them can also understand their implementation on certain features. (ppt>>Android Open Source project selection of image cache )
The benefits of choosing an open source project and how to choose an open source project are visible: Open source project use and selection.
I. Four pictures
CacheBasic information
Universal Imageloader is an early open source image Cache that has been used by many applications in the early days.
Picasso is a Square open source project, and his leader is Jakewharton, so it is widely known.
Glide is an open source project for Google employees and is used by some Google apps to be featured on Google I/O last year, but there is little domestic data.
Fresco is an open source image cache for Facebook in the first half of this year, with key features including:
- Two memory buffers plus Native cache constitute a level three cache ;
- Support streaming, can be similar to the fuzzy progressive display image on the page;
- Support for multi-frame animated pictures is better, such as Gif, WebP.
Given that Fresco has not released a formal version of the 1.0, and has not much time to familiarize with the Fresco source code, the following comparisons do not include Fresco, later time to add a contrast.
Ii. Basic Concepts
Before you make a formal comparison, let's look at some common concepts of image caching :
- Requestmanager: Request generation and Management module;
- Engine: The engines section, which is responsible for creating tasks (acquiring data) and dispatching execution;
- Getdatainterface: Data acquisition Interface, responsible for obtaining data from various data sources. For example, memorycache from the memory cache to obtain data, diskcache from the local cache to obtain data, the downloader from the network to obtain data and so on.
- Displayer: A resource (picture) display for displaying or manipulating resources. For example ImageView, these image caches not only support ImageView, but also support other View and virtual displayer concepts.
- Processor resource (image) processor, responsible for processing resources, such as rotation, compression, interception and so on.
The above concepts may be different in different picture caches , such as Displayer in Imageloader called Imageaware, in Picasso and Glide called Target.
Third, common advantages
1. Easy to use
Can be achieved through a code to achieve picture acquisition and display.
2. High degree of configuration and high adaptability
Picture Cache Downloader (retry mechanism), decoder, monitor, processor, memory cache , local cache , thread pool, cache algorithm, etc. can be easily configured.
High degree of adaptability, according to the system performance initialization cache configuration, System Information changes after the dynamic adjustment strategy.
For example, according to the number of CPU cores to determine the maximum number of concurrent, based on the available memory to determine the size of memory cache , network state changes when the maximum number of concurrency.
3. Multilevel caching
Have at least two levels of cache , increasing the speed of picture loading.
4. Support Multiple data sources
Support multiple data sources, network, local, resource, Assets, etc.
5. Support multiple Displayer
Not only supports ImageView, but also supports other View and virtual displayer concepts.
Other small things in common include support for animations, support for transform processing, access to EXIF information, and more.
Four, imageloader design and advantages
1. Overall design and process
Above is the overall design of the imageloader. The whole library is divided into five modules of Imageloaderengine,cache and Imagedownloader,imagedecoder,bitmapdisplayer,bitmapprocessor, of which the Cache is divided into MemoryCache and DiskCache two parts.
Simply speaking, Imageloader receives the task of loading and displaying the picture and handing it over to the Imageloaderengine,imageloaderengine distribution task to the specific thread pool to execute, the task through the Cache and Imagedownloader Get the picture, the middle may go through bitmapprocessor and imagedecoder processing, the final conversion to bitmap to Bitmapdisplayer in Imageaware display.
2. Imageloader Advantages
(1) Support Download Progress monitoring
(2) You can pause picture loading in View scrolling
The Pauseonscrolllistener interface lets you pause picture loading in View scrolling.
(3) The default implementation of a variety of memory cache algorithm These image caches can be configured cache algorithm, but imageloader default implementation of a large number of cache algorithms, such as the Size of the largest first delete, Use at least first delete, least recently used, advanced first Delete, the longest time first delete, etc.
(4) Support local cache file name rule definition
Five, Picasso design and advantages
1. Overall design and process
Above is the overall design of the Picasso. The whole library is divided into modules such as Dispatcher,requesthandler and downloader,picassodrawable.
Dispatcher is responsible for distributing and handling actions, including commit, pause, resume, cancel, network status changes, retry, and so on.
The simple thing is that Picasso receives the task of loading and displaying the picture, creating the Request and handing it over to the Dispatcher,dispatcher distribution task to the specific RequestHandler, the task through MemoryCache and Handler ( Data acquisition interface) to obtain a picture, the image obtained successfully through the picassodrawable display to Target.
Note that the File system section of Data above, Picasso does not have a custom local cache interface, the default use of HTTP local cache , API 9 or more using Okhttp, the following use URLConnection , so if you need to customize the local cache , you need to redefine Downloader.
2. Picasso Advantages
(1) Self-monitoring function with statistics
Supports monitoring of image cache usage, including cache hit ratio, used memory size, saved traffic, and more.
(2) Support priority processing
Priority tasks are selected before each task is scheduled, such as when the Banner priority in the APP page is higher than the Icon.
(3) Support delay to picture size calculation complete loading
(4) Support flight mode, number of concurrent threads varies by network type
The maximum concurrency of the thread pool is automatically adjusted when the phone switches to airplane mode or network type transformation, such as WiFi maximum concurrency is 4, and 4g is 3,3g 2.
Here Picasso determines the maximum number of concurrent, not CPU cores, based on the network type.
(5) "None" local cache
No "local cache ", not that there is no local cache , but Picasso itself is not implemented, to the Square of another network library okhttp to implement, the advantage is that you can request the Response Header in the Cache-control and Expired control the expiration time of the picture.
Six, Glide design and advantages
1. Overall design and process
Above is the overall design of the Glide. The entire library is divided into Requestmanager (Request manager), Engine (data acquisition engine), Fetcher (Data Collector), MemoryCache (memory cache ), Disklrucache, Transformation (image processing), Encoder (local cache storage), Registry (image type and parser configuration), Target (destination) and other modules.
Simply speaking, Glide receives the task of loading and displaying the resource, creating the Request and handing it over to the requestmanager,request boot Engine to get the resource (via Fetcher), and get to the post transformation processing To Target.
Glide relies on the Disklrucache, Gifdecoder and other open source libraries to complete the local cache and Gif image decoding work.
2. Glide Advantages
(1) Image Cache , media cache
Glide is not just a picture cache , it supports Gif, WebP, thumbnails. Even Video, so it's more like a media cache .
(2) Support priority processing
(3) Consistent with activity/fragment life cycle, support Trimmemory
Glide maintains a requestmanager for each context, is consistent with the activity/fragment life cycle through fragmenttransaction, and has a corresponding Trimmemory interface implementation available for invocation.
(4) Support Okhttp, Volley
Glide data is obtained by default via URLConnection and can be used with okhttp or volley. Actual Imageloader, Picasso also support Okhttp, volley.
(5) memory-friendly
①glide's memory cache has an active design
When fetching data from the memory cache , it does not use get for the general implementation, but instead uses remove to place the cached data in a activeresources map with value as a soft reference and counts the number of references. After the picture is loaded, it is determined that if the reference count is empty, it is recycled.
② Memory Cache for smaller images
Glide as a federated key for URL, viewwidth, viewheight, screen resolution, and so on, the processed picture is cached in the memory cache instead of the original image to save size
③ is consistent with the activity/fragment life cycle, supporting trimmemory
④ images default to use the default RGB565 instead of ARGB888
Although the sharpness is poor, but the picture is smaller, can also be configured to argb_888.
Other: Glide can support URL expiration by signature or not using local cache
Vii. Summary
Generally speaking, the function of Imageloader and the length of the agent are easy to understand.
Although the Picasso code is only in one package, there is no strict package distinction, but the code is simple, logical and clear, one or two hours can be called in-depth understanding of the end.
Glide is powerful, but the code is large and the flow is complicated. In the case of a deeper grasp of the recommended use, lest there is a problem difficult to solve.
"MDCC 2015" open source selection of Android three big picture cache principle, characteristic contrast