图像分割coil-20
介绍 (Introduction)
A well-developed language has a number of supporting libraries that make the life of a developer comfortable. For example, in Android in the section of image loading into ImageViews, we have a huge set of libraries. But the popular libraries that are mostly used for image loading are Glide, Picasso, Universal Image Loader, etc. As Kotlin language is emerging it has introduced it’s own image loading library named COIL. The COIL is an acronym for Coroutine Image Loader, it is an image loading library backed by Kotlin Coroutines. However, we need not have knowledge of Coroutines to use this library. In this article let’s explore its features and basic usage.
完善的语言具有许多支持库,使开发人员的生活舒适。 例如,在Android的将图像加载到ImageViews中的部分中,我们有大量的库 。 但是最流行的图像加载库是Glide,Picasso,Universal Image Loader等。随着Kotlin语言的出现,它引入了自己的图像加载库COIL 。 COIL是C oroutine I mage L oader的首字母缩写,它是由Kotlin Coroutines支持的图像加载库。 但是,我们不需要了解协程就可以使用此库。 在本文中,我们探讨其功能和基本用法。
长处 (Strengths)
As every library has it’s own strengths, Coil has its own set of features to differentiate from others. It is fast, modern, and lightweight library that uses all the latest things available in the market. It’s impressive because of the following strengths
由于每个图书馆都有自己的长处,因此Coil具有自己的一套特色以区别于其他图书馆。 它是快速,现代且轻量级的库,它使用了市场上所有最新的东西。 由于以下优点,给人留下深刻的印象
Fast: Coil performs a number of optimizations including memory and disk caching, downsampling the image in memory, re-using Bitmaps, automatically pausing/cancelling requests, and more.
快速 :Coil执行了许多优化,包括内存和磁盘缓存,对内存中的图像进行下采样,重新使用位图,自动暂停/取消请求等等。
Lightweight: Coil adds ~2000 methods to your APK (for apps that already use OkHttp and Coroutines), which is comparable to Picasso and significantly less than Glide and Fresco.
轻量级 :Coil为您的APK添加了约2000种方法(对于已经使用OkHttp和Coroutines的应用程序),与Picasso相当,远小于Glide和Fresco。
Easy to use: Coil’s API leverages Kotlin’s language features for simplicity and minimal boilerplate. This can be experienced in the following part of the article.
易于使用 :Coil的API利用Kotlin的语言功能来简化和最小化样板。 在本文的以下部分中可以体会到这一点。
Modern: Coil is Kotlin-first and uses modern libraries including Coroutines, OkHttp, Okio, and AndroidX Lifecycles.
现代的 :Coil是Kotlin的第一名,并使用包括Coroutines,OkHttp,Okio和AndroidX Lifecycles在内的现代库。
Bitmap Pooling: Similar to Glide and Fresco, Coil supports bitmap pooling. Bitmap pooling is a technique to re-use Bitmap objects once they are no longer in use (i.e. when a View is detached, a Fragment’s view is destroyed, etc.). This can significantly improve memory performance (especially on pre-Oreo devices), however, it creates several API limitations.
位图池 :类似于Glide和Fresco,Coil支持位图池。 位图池是一种不再使用位图对象(即,当视图分离,片段的视图被破坏等)时重新使用位图对象的技术。 这可以显着提高内存性能 (尤其是在Oreo之前的设备上),但是,它会造成一些API限制。
Avoid annotation processing, which can often slow down build speeds. Coil depends on Kotlin's extension functions instead.
避免使用注释处理 ,因为注释处理通常会降低构建速度。 线圈取决于Kotlin的扩展功能。
Image Sampling: Suppose if we have an image that is 500x500 on disk, but you only need to load it into memory at 100x100 to be displayed in a view. Coil will load the image into memory, but what happens now if you need the image at 500x500? There’s still more “quality” to read from disk, but the image is already loaded into memory at 100x100. Ideally, we would use the 100x100 image as a placeholder while we read the image from disk at 500x500. This is exactly what Coil does and Coil handles this process automatically for all BitmapDrawables. Paired with
crossfade(true)
, this can create a visual effect where the image detail appears to fade in, similar to a progressive JPEG.图像采样 :假设磁盘上有一个500x500的图像,但是您只需要以100x100的速率将其加载到内存中即可在视图中显示。 Coil会将图像加载到内存中,但是如果您需要500x500的图像会怎样呢? 从磁盘读取还有更多的“质量”,但是图像已经以100x100的速率加载到内存中。 理想情况下,当我们从磁盘以500x500读取图像时,我们将100x100图像用作占位符 。 这正是Coil所做的,并且Coil为所有BitmapDrawables自动处理此过程 。 与
crossfade(true)
配对,可以创建视觉效果,其中图像细节似乎会淡入,类似于渐进式JPEG 。
Here’s what it looks like in the sample app:
这是示例应用程序中的外观:
Images in the list have intentionally been loaded with very low detail and the crossfade is slowed down to highlight the visual effect. 列表中的图像被故意加载了非常低的细节,并且淡入淡出的速度变慢以突出视觉效果。It’s evident that with the above features Coil is the good-to-go choice for Android developers who have started working with Kotlin.
很明显,有了上述功能,对于开始使用Kotlin的Android开发人员而言,Coil是不错的选择。
让我们开始编码 (Let’s get to coding)
要求 (Requirements)
Before we start to use coil in our applications there is a prerequisite set of requirements to be satisfied
在我们开始在应用中使用线圈之前,必须满足一组先决条件
- Android X Android X
- Min SDK 14+ 最低SDK 14+
- Compile SDK: 29+ 编译SDK:29+
- Java 8+ Java 8+
添加依赖 (Adding dependency)
The first step after qualifying the minimum requirement criteria is to add a Coil library dependency to the application’s build.gradle file:
满足最低要求标准之后的第一步是将Coil库依赖项添加到应用程序的build.gradle文件中:
implementation("io.coil-kt:coil:0.11.0")
The coil is available on mavenCentral()
.
线圈在mavenCentral()
上可用。
载入图像 (Loading Images)
To load an image into an ImageView, use the load
extension function:
要将图像加载到ImageView中,请使用load
扩展功能:
// URL
imageView.load(// Resource
imageView.load(R.drawable.image)// File
imageView.load(File("/path/to/image.jpg"))// And more...
向图像添加变换 (Add Transformations to the Image)
In Coil, adding image transformations is relatively simple. The transformations available are:
在Coil中,添加图像变换相对简单。 可用的转换为:
CircleCropTransformation — Crops and centres the image into a circle.
CircleCropTransformation —裁剪并将图像居中成一个圆圈。
RoundedCornersTransformation — Crops the image to fit the target’s dimensions and rounds the corners of the image.
RoundedCornersTransformation —裁剪图像以适合目标尺寸,并圆角化图像。
BlurTransformation —Applies a Gaussian Blur.
BlurTransformation —应用高斯模糊。
GrayscaleTransformation — Shades the image into grayscale.
GrayscaleTransformation —将图像着色为灰度。
Requests can be configured with an optional trailing lambda:
可以使用可选的尾随lambda配置请求:
imageView.load(
crossfade(true)
placeholder(R.drawable.place_holder_image)
transformations(CircleCropTransformation())
}
图像加载器 (Image Loaders)
imageView.load uses the singleton ImageLoader to execute a LoadRequest. The singleton ImageLoader can be accessed using:
imageView.load使用单ImageLoader的执行的loadRequest。 可以使用以下方式访问单例ImageLoader:
val imageLoader = Coil.imageLoader(context)
Optionally, you can create your own ImageLoader instance(s) and inject them with dependency injection:
(可选)您可以创建自己的ImageLoader实例,并通过依赖项注入将它们注入:
val imageLoader = ImageLoader(context)
If you do not want the singleton ImageLoader, depend onio.coil-kt:coil-base
.
如果您不希望使用单例ImageLoader,请依赖io.coil-kt:coil-base
。
要求 (Requests)
To load an image into a custom target, execute a LoadRequest:
要将图像加载到自定义目标中,请执行LoadRequest :
val request = LoadRequest.Builder(context)
.data("
.target { drawable ->
// Handle the result.
}
.build()
imageLoader.execute(request)
To get an image imperatively, execute a GetRequest:
要强制获取图像,请执行GetRequest :
val request = GetRequest.Builder(context)
.data("
.build()
val drawable = imageLoader.execute(request).drawable
支持的数据类型 (Supported Data Types)
The base data types that are supported by all ImageLoader instances are:
所有ImageLoader实例支持的基本数据类型是:
- String (mapped to a Uri) 字符串(映射到Uri)
- HttpUrl HttpUrl
Uri (
android.resource
,content
,file
,http
, andhttps
schemes only)Uri(仅限
android.resource
,content
,file
,http
和https
方案)- File 文件
- @DrawableRes Int @DrawableRes Int
- Drawable 可抽出
- Bitmap 位图
预装 (Preloading)
To preload, an image into memory, execute a LoadRequest without a Target:
要将图像预加载到内存中,请执行不带Target的LoadRequest :
val request = LoadRequest.Builder(context)
.data("https://www.example.com/image.jpg")
// Optional, but setting a ViewSizeResolver will conserve memory by limiting the size the image should be preloaded into memory at.
.size(ViewSizeResolver(imageView))
.build()
imageLoader.execute(request)
To preload a network image only into the disk cache, disable the memory cache for the request:
要将网络映像仅预加载到磁盘缓存中,请为请求禁用内存缓存:
val request = LoadRequest.Builder(context)
.data("https://www.example.com/image.jpg")
.memoryCachePolicy(CachePolicy.DISABLED)
.build()
imageLoader.execute(request)
取消要求 (Cancelling Requests)
LoadRequests will be automatically cancelled if the associated View is detached, the associated Lifecycle is destroyed, or another request is started on the same View.
如果关联的视图已分离,关联的生命周期被破坏或在同一View上启动了另一个请求,则LoadRequests将自动取消。
Furthermore, each LoadRequest returns a RequestDisposable, which can be used to check if a request is in progress or dispose of the request (effectively cancelling it and freeing its associated resources):
此外,每个LoadRequest返回一个RequestDisposable ,可用于检查请求是否正在进行或正在处理该请求(有效地取消请求并释放其关联资源):
val disposable = imageView.load("https://www.example.com/image.jpg")// Cancel the request.disposable.dispose()
GetRequests will only be cancelled if the coroutine context's job is cancelled.
仅当协程上下文的作业被取消时, GetRequests才会被取消。
R8 / Proguard (R8 / Proguard)
The coil is fully compatible with R8 out of the box and doesn’t require adding any extra rules.
线圈与R8完全兼容开箱即用,并且不需要添加任何额外的规则。
If you use Proguard, you may need to add rules for Coroutines, OkHttp and Okio.
如果您使用Proguard的,你可能需要添加规则协同程序 , OkHttp和奥基奥 。
摘要 (Summary)
So this is basic info of Coil, so if you like it and want to explore more or want to give it a try check out the following links
因此,这是Coil的基本信息,因此,如果您喜欢它,并且想探索更多或想尝试一下,请查看以下链接
Coil Documentation
线圈文件
Coil GitHub Repo
Coil GitHub存储库
Please let me know your suggestions and comments.
请让我知道您的建议和意见。
You can find me on Medium and LinkedIn …
您可以在Medium和LinkedIn上找到我…
Thanks for reading…
谢谢阅读…
翻译自: https://medium.com/@pavan.careers5208/coil-kotlin-default-image-loader-for-android-556346043004
图像分割coil-20