Android image caching principle, characteristic contrast _android

Source: Internet
Author: User
Tags webp

This is what I share on the MDCC (slightly changes), but also the source analysis of the first release of the source analysis introduced in the following will slowly do.

Comparing several picture caches from the overall design and principle, it is not possible for their friends to understand their implementation of certain features.

I. Four pictures cache basic information


Universal Imageloader is a very early open source image cache, which was used by many applications in the early days.

Picasso is the Square Open source project, and his leader is Jakewharton, so it is widely known.

Glide, an Open-source project for Google employees, was used by some Google apps and was recommended for last year's Google I/O, but there are few domestic sources.

Fresco is Facebook's open source image cache in the first half of this year, with key features including:
(1) two memory cache plus Native cache make up level three cache

(2) Support streaming, can be similar to the Web page blur progressive display of pictures

(3) for multiple frames animated picture support better, such as Gif, WebP

Since Fresco has not released the official 1.0 version, at the same time has not much time to become familiar with the Fresco source code, the back of the contrast does not include Fresco, later have time to add contrast.

More picture Cache libraries visible: Android Image Cache Library

Ii. Basic Concepts

Before you make a formal comparison, learn a few common concepts of picture caching:
(1) Requestmanager: Request generation and Management module

(2) Engine: Engine section, responsible for creating tasks (getting data) and scheduling execution

(3) Getdatainterface: Data acquisition interface, which is responsible for obtaining data from various data sources.
For example, MemoryCache gets the data from the memory cache, diskcache the data from the local cache, and the downloader gets the data from the network.

(4) Displayer: resource (picture) display for displaying or manipulating resources.
For example, ImageView, these image caches do not only support ImageView, but also support other View and virtual displayer concepts.

(5) Processor resource (picture) processor
Responsible for processing resources, such as rotation, compression, interception and so on.

The names of the above concepts may be different in different image caches, such as Displayer in Imageloader called Imageaware and Target in Picasso and Glide.

Third, common advantages

1. Easy to use
Can be achieved through a code to achieve picture acquisition and display.

2. High configurable degree and high adaptability
The picture caches the downloader (retry mechanism), the decoder, the monitor, the processor, the memory cache, the local cache, the thread pool, the caching algorithm and so on most can easily configure.

The adaptive degree is high, according to the system performance initialization cache configuration, System Information change dynamic adjustment strategy.
For example, according to the CPU core to determine the maximum number of concurrent, according to the available memory to determine the memory cache size, network state changes when the maximum number of concurrent.

3. Multilevel caching
Have at least level two cache, increase picture loading speed.

4. Support Multiple data sources
Support a variety of data sources, network, local, resource, Assets, etc.

5. Support a variety of displayer
Not only supports ImageView, but also supports other View and virtual displayer concepts.

Other minor commonalities include supporting animations, supporting transform processing, capturing 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 Imageloaderengine,cache and imagedownloader,imagedecoder,bitmapdisplayer,bitmapprocessor five modules, of which Cache is divided into MemoryCache and DiskCache two parts.

Simply speaking, Imageloader receives the task of loading and displaying pictures, and gives it to imageloaderengine,imageloaderengine distribution task to the specific thread pool to execute, the task through Cache and Imagedownloader Get the picture, the middle may pass bitmapprocessor and imagedecoder processing, the final conversion to bitmap to Bitmapdisplayer in Imageaware display.

2. Advantages of Imageloader

(1) Support Download Progress monitoring

(2) Can suspend 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 caching algorithms can configure caching algorithms, but imageloader default implementation of a large number of caching algorithms, such as Size maximum first delete, use at least the first deletion, the least recent use, advanced first Delete, the longest time to delete.

(4) Support local cache filename 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 Dispatcher,requesthandler and downloader,picassodrawable modules.

Dispatcher is responsible for distributing and handling Action, including commit, pause, continue, cancel, network state change, retry, and so on.

Simply put, Picasso receives the task of loading and displaying pictures, creates the Request and passes it to the Dispatcher,dispatcher distribution task to the specific RequestHandler, through MemoryCache and Handler ( Data acquisition interface) Gets the picture, and the picture gets successful and is displayed to Target via picassodrawable.

Note that the File system section of Data above, Picasso does not have a custom local cache interface, default to use HTTP local cache, API 9 above the use of okhttp, the following use of URLConnection, so if you need to customize the local cache requires heavy Define Downloader.

2. Advantages of Picasso

(1) with statistical monitoring function
Supports the monitoring of the use of picture caching, including cache hit ratio, used memory size, saved traffic, and so on.

(2) Support priority processing
Priority tasks are selected before each task schedule, such as when the Banner priority in the APP page is higher than Icon.

(3) Support delay to picture size calculation complete loading

(4) Support flight mode, concurrent number of threads depending on the type of network
When mobile phones switch to flight mode or network type conversion, the maximum concurrent number of threads pool is adjusted automatically, for example, WiFi maximum concurrency is 4, 4g is 3,3g 2.
Here Picasso determines the maximum concurrency number, rather than the CPU kernel, based on the network type.

(5) "None" local cache
There is no "local cache, not to say that there is no local cache, but rather Picasso did not implement, to the Square of another network library okhttp to implement, the advantage is that you can request Response Header in the Cache-control and expired Controls the expiration time of a picture.

Vi. 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) module.

Simply speaking, Glide receives the task of loading and displaying resources, creates the Request and gives it to requestmanager,request to start Engine to the data source (through Fetcher), and gets the post transformation processing To Target.

Glide relies on Disklrucache, Gifdecoder and other open source libraries to complete the local cache and Gif image decoding work.

2. Advantages of Glide

(1) Image cache-> Media cache
Glide is not only a picture cache, it supports Gif, WebP, thumbnails. Even video, so it's better to be a media cache.

(2) Support priority processing

(3) Consistent with activity/fragment life cycle, support trimmemory
Glide maintains a requestmanager for each context, which is consistent with the activity/fragment lifecycle through Fragmenttransaction, and has a corresponding Trimmemory interface implementation to invoke.

(4) Support Okhttp, volley
Glide By default URLConnection get data, can cooperate with okhttp or volley use. Actual Imageloader, Picasso also support Okhttp, volley.

(5) Memory friendly
The ①glide memory cache has an active design
When fetching data from the memory cache, it is not like a general implementation with get, but with remove, then put the cached data into a activeresources map with a value of soft reference, count the reference number, judge the picture after loading, and recycle if the reference count is empty.

② Memory Cache smaller picture
Glide to the URL, view_width, view_height, screen resolution, etc. as the joint key, the processed picture is cached in the memory cache, rather than the original image to save size

③ is consistent with activity/fragment lifecycle and supports Trimmemory

④ picture defaults to using default rgb_565 instead of argb_888
Although the sharpness is poor, but the picture is smaller, can also be configured to argb_888.

Other: Glide can expire by signature or not using the local cache support URL

Vii. Summary


Generally speaking, the Imageloader function and the agent is easy to understand the length of the general.

Picasso code, although only under one package, there is no strict packet distinction, but the code simple, logical clear, one or two hours can be called in-depth understanding of the end.

The Glide function is powerful, but the code quantity is big, the flow is complex. In a deeper grasp of the case only recommended use, lest the problem is difficult to solve.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.