This is what I shared on MDCC (with a slight twist), and what I will be doing later in the first release of source parsing.

 

Compare several image caches in terms of overall design and principle so that your friends can see how they implement certain features.

 

The previous article on the benefits of choosing an open source project and how to choose an open source project can be seen: Use and selection of open source projects.

 

One. Four major picture cache basic information



Universal ImageLoader is an early open source image cache used by many early applications.

 

Picasso is widely known for being an open source project from Square and leading JakeWharton.

 

Glide is an open-source project by Google employees that is used in some Google apps and was featured in Google I/O last year, though there is not much information available in the country.

 

Fresco is an image cache that Facebook opened to the public earlier this year. Its main features include: (1) Two in-memory caches, plus a Native cache

 

(2) Support streaming, can be similar to the fuzzy progressive display of pictures on the web page

 

(3) Better support for multi-frame animation pictures, such as Gif, WebP

 

Since Fresco hasn’t released an official version 1.0 and hasn’t had much time to get familiar with the Fresco source code, this comparison doesn’t include Fresco, so we’ll add it later.

 

More image cache library: Android Image cache library

 

2. Basic concepts

Before the comparison, let’s take a look at some general concepts for image caching: (1) RequestManager: a module that generates and manages requests

 

(2) Engine: The Engine part, which is responsible for creating tasks (obtaining data) and scheduling their execution

 

(3) GetDataInterface: data acquisition interface, responsible for obtaining data from various data sources. For example, MemoryCache gets data from the MemoryCache, DiskCache gets data from the local cache, and downloader gets data from the network.

 

(4) Displayer: Resource (picture) display, used to display or operate resources. For example, ImageView, these image caches support not only ImageView, but also other views and the virtual Displayer concept.

 

The Processor is responsible for processing resources, such as rotation, compression, interception, and so on.

 

These concepts may be called differently in different image caches, for example Displayer is called ImageAware in ImageLoader and Target in Picasso and Glide.

 

Third, common advantages

1. The use of simple can be achieved through a code picture acquisition and display.

 

2. Highly configurable, highly adaptive image cache downloaders (retry mechanism), decoders, displays, processors, memory cache, local cache, thread pools, cache algorithms, and most of them can be easily configured.

 

High degree of self-adaptation, the system initializes the cache configuration based on system performance and dynamically adjusts the policy after system information changes. For example, the maximum number of concurrent requests is determined based on the number of CPU cores, the size of memory cache is determined based on the available memory, and the maximum number of concurrent requests is adjusted when the network status changes.

 

3. Multi-level cache has at least two levels of cache to improve picture loading speed.

 

4. Multiple data sources Support multiple data sources, including network, local, resources, and Assets

 

Support for multiple Displayers not only ImageView, but also other views and the virtual Displayer concept.

 

Other minor commonalities include support for animations, support for transform handling, and getting EXIF information.

 

ImageLoader design and advantages

1. Overall design and process

Above is the overall design of ImageLoader. The whole library is divided into ImageLoaderEngine, Cache and ImageDownloader, ImageDecoder, BitmapDisplayer, BitmapProcessor five modules, The Cache is divided into MemoryCache and DiskCache.

 

Simply put, the ImageLoader receives the task of loading and displaying the image and gives it to the ImageLoaderEngine, which distributes the task to a specific thread pool for execution. The task obtains the image from Cache and ImageDownloader, which may be processed by BitmapProcessor and ImageDecoder. Finally, it is converted to a Bitmap and handed to BitmapDisplayer for display in ImageAware.

 

2. The ImageLoader advantages

(1) Support download progress monitoring

 

PauseOnScrollListener interface PauseOnScrollListener interface PauseOnScrollListener interface

 

(3) The default implementation of a variety of memory cache algorithm these image cache can be configured with a cache algorithm, but ImageLoader default implementation of more cache algorithm, such as the Size of the largest first delete, use the least first delete, the least recently used, first delete, the longest time delete, etc..

 

(4) Support local cache file name rule definition

 

Picasso design and strengths

1. Overall design and process

Above is Picasso’s master plan. The whole library is divided into Dispatcher, RequestHandler and Downloader, PicassoDrawable and other modules.

 

The Dispatcher distributes and handles actions, including commit, pause, continue, cancel, network state changes, retry, and so on.

 

Basically, Picasso receives the task of loading and displaying images, creates a Request and sends it to the Dispatcher, who distributes the task to the specific RequestHandler, MemoryCache and Handler are used to retrieve the image. The image is displayed in the Target via PicassoDrawable.

 

Note that Picasso does not have a custom interface for local caching. By default, he uses HTTP local caching. For API 9, okHTTP is used for above and Urlconnection is used for below. So if you want to customize the local cache, you need to redefine the Downloader.

 

2. Picasso was an advantage

(1) Its own statistical monitoring function supports the monitoring of the use of picture cache, including the cache hit ratio, used memory size, saved traffic, etc.

 

(2) Support priority processing tasks with high priority will be selected before each task scheduling, for example, it is applicable when the priority of Banner in App page is higher than that of Icon.

 

(3) Support delay until the image size calculation is finished loading

 

(4) Support flight mode and the number of concurrent threads varies according to the network type. When the mobile phone switches to flight mode or network type changes, the maximum number of concurrent threads in the thread pool will be automatically adjusted. For example, the maximum concurrency of wifi is 4, 4g is 3, and 3g is 2. Here Picasso determines the maximum concurrency based on the network type, not the number of CPU cores.

 

(5) “No” local cache no “local cache, not that there is no local cache, but That Picasso did not implement it himself and handed it over to Square’s other network library okHTTP to implement it, The advantage is that you can Control the expiration time of the image by requesting cache-control and Expired in the Response Header.

 

Glide design and advantages

1. Overall design and process

The general design of Glide is above. The entire library is divided into the RequestManager, Engine(Data acquisition Engine), Fetcher, MemoryCache, DiskLRUCache, Transformation, Encoder, Registry, Target and other modules.

 

Glide receives the task to load and display the resource, creates a Request and passes it to the RequestManager, and the Request starts the Engine to fetch the resource from the data source (via Fetcher). Obtain the Transformation and submit it to Target.

 

Glide relies on open source libraries such as DiskLRUCache and GifDecoder to complete local caching and Gif image decoding.

 

2. The advantages of Glide

Glide is not only an image cache, it supports Gif, WebP, thumbnails. Even Video, so it should be used as a media cache.

 

(2) Support priority processing

 

(3) Consistent with the Activity/Fragment life cycle, trimMemory Glide can maintain a RequestManager for each context, The FragmentTransaction is kept consistent with the Activity/Fragment life cycle and has a corresponding trimMemory interface implementation that can be invoked.

 

(4) Support OKHTTP, Volley Glide by default through UrlConnection to obtain data, can be used with OKHTTP or Volley. Actual ImageLoader and Picasso also support OkHTTP and Volley.

 

(5) Glide memory cache has an active design. When retrieving data from memory cache, it uses remove instead of GET, and puts the cached data into an activeResources map whose value is a soft reference. And count the number of references, judge after the image is loaded, if the reference count is empty, it will be recycled.

 

Image Glide to URL, view_width, view_height, screen resolution as a joint key, the processed picture cached in memory cache, rather than the original picture to save size

 

(3) Consistent with the Activity/Fragment life cycle, supports trimMemory

 

(4) RGB_565 is used as the default image instead of ARGB_888, which is smaller and can also be configured to ARGB_888.

 

Other: Glide can support URL expiration through signature or without the use of local caching

 

Seven, summary



Overall, ImageLoader’s functionality and ease of understanding of agents are average.

 

Although Picasso code is only in a package, there is no strict package distinction, but the code is simple and logical, can be called in-depth understanding in one or two hours.

 

Glide has powerful function, but large amount of code, complex flow. It is recommended to be used when you have a better grasp of the situation, so that it is difficult to solve the problem.

 

More image cache library: Android Image cache library