PrefaceCoil is a very young image loading library. It only released version 1.0.0 on October 22, 2020, but it has been promoted by Android officials. We talked about it in a blog post called Android Developers Backstage. The reason for the promotion is simple: on the one hand, this library is really well made, and on the other hand, this library is written entirely in Kotlin and uses a lot of Kotlin features, especially coroutines. So Google says it will not give up Java, but in fact we all know it. Coil's name comes from the first letters of Coroutine Image Loader. It can be seen that images are loaded through Kotlin coroutines. The features are as follows:
From the features of Coil, we can see that it is a very suitable image loading library for personal apps, especially apps developed in pure Kotlin. Moreover, Coil uses a lot of new features and coroutines of Kotlin, which is of great value for us to learn Kotlin. Compared with glide and fresco, which have very complex structures and amazing amounts of code, Coil has only about 2,000 methods, so it is also very suitable for source code research and learning. Basic UseCoil can be downloaded from mavenCentral():
Coil adds a lot of extension functions to ImageView, so we can load images with just one line of code:
At the same time, we can also use lambda syntax to easily configure image loading:
Commonly used APIsImageLoaderImageLoader is the steward of image loading in Coil, responsible for handling cache, data acquisition, image decoding, request management, Bitmap cache pool, memory management, etc. It is generally recommended to create only one ImageLoader and share it in the App, so that the performance is optimal. This is because each ImageLoader has its own memory cache and Bitmap cache pool. We can create and configure ImageLoader through the constructor.
At the same time, since ImageLoader is an interface, it means that we can test it very conveniently. For example, we can inject a fake ImageLoader to return the same drawable every time.
ImageRequestImageRequest provides all the necessary information for ImageLoader to load images, and we can also use custom Target for processing.
ImageRequest is created based on the Builder mode, which includes various configuration items for loading images. Here we focus on the most commonly used configuration items. DisposableDisposable is the return value after calling the load() method, which is mainly used to cancel image loading:
Image TransformationImage transformation is a very common function in image loading libraries. Coil abstracts it into a Transformation interface. You can see that there is a BitmapPool parameter in the transform() method. This is because a Bitmap is often required when implementing graphic transformation. In this case, it can be directly obtained from the BitmapPool, thereby reusing the existing Bitmap.
Coil mainly provides the following image transformation effects: Functional expansionCoil not only provides many necessary functions, but also reserves many extension points for developers to customize. Coil's image loading mainly includes four main modules: InterceptorsCoil's Interceptor undoubtedly draws on the design ideas of okhttp, which greatly facilitates subsequent function expansion. For example, we can add a custom cache layer to Coil:
Mappers and FetchersWhen calling load() externally, the passed String parameter may point to a local resource file or a network image. Mappers and Fetchers can be used together to distinguish resource types. For example:
StringMapper will convert the incoming String into the corresponding Uri.
ResourceUriFetcher will determine whether the scheme type of Uri is android.resource. If it is, it represents a local resource file, while HttpUriFetcher will determine whether the scheme of Uri is http or https. If it is, it represents a network image.
DecodersAndroid supports many image formats, but there are also many formats that it does not support (for example: Gif, SVG, video frames, etc.), so Coil provides a corresponding extension library. ① Gif (GifDecoder supports all API levels, but is slower, ImageDecoderDecoder loads faster, but is only available on API 28 and higher)
② SVG (If the requested MIME type is image/svg+xml, all SVGs will be automatically detected and decoded)
③ Video frame (only supports File and Uri)
|
<<: WeChat dedicated input method is here
>>: There are really no new features to add, so Android 12 has to add a "Recycle Bin" feature
Bird statue The bird is the most obvious shape in...
On September 7, the reporter learned from the Xis...
Are there some "special" people around ...
Apple's latest AirPods have received widespre...
The World Cup is not only a brand carnival for gl...
Starting from September this year, the Ministry o...
Today, Apple released a quick security response u...
"Time is money, but money can't buy time...
1. Ideas for event planning 1. Five elements of e...
Source: Jiangsu Huaian Fire Department...
Previously we talked about some core work ideas o...
On December 4, 2016, Himalaya FM's first &quo...
Yesterday morning, Apple released a new iOS 14.6 ...
For people who wear glasses, it is easy for glass...