图像分割coil-20_Coil Kotlin Android的默认图像加载器

2023-10-27

图像分割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, and https schemes only)

    Uri(仅限android.resourcecontentfilehttphttps方案)

  • 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

您可以在MediumLinkedIn上找到我…

Thanks for reading…

谢谢阅读…

翻译自: https://medium.com/@pavan.careers5208/coil-kotlin-default-image-loader-for-android-556346043004

图像分割coil-20

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

图像分割coil-20_Coil Kotlin Android的默认图像加载器 的相关文章

  • tomcat 中受密码保护的应用程序

    我正在使用 JSP Servlet 开发一个Web应用程序 并且我使用了Tomcat 7 0 33 as a web container 所以我的要求是tomcat中的每个应用程序都会password像受保护的manager applica
  • 从 Powershell 脚本安装 Python

    当以管理员身份从 PowerShell 命令行运行以下命令时 可以在 Windows 11 上成功安装 Python c temp python 3 11 4 amd64 exe quiet InstallAllUsers 0 Instal
  • Python 3:将字符串转换为变量[重复]

    这个问题在这里已经有答案了 我正在从 txt 文件读取文本 并且需要使用我读取的数据之一作为类实例的变量 class Sports def init self players 0 location name self players pla
  • 在我的 Spring Boot 示例中无法打开版本 3 中的 Swagger UI

    我在 Spring Boot 示例中打开 swagger ui 时遇到问题 当我访问 localhost 8080 swagger ui 或 localhost 8080 root api name swagger ui 时出现这种错误 S
  • 使用 python/numpy 重塑数组

    我想重塑以下数组 gt gt gt test array 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44 为了得到 gt gt gt test2 array 11 12 21 22 13 14
  • 使用 AsyncTask 传递值

    我一直在努力解决这个问题 但我已经到了不知道该怎么办的地步 我想做的是使用一个类下载文件并将其解析为字符串 然后将该字符串发送到另一个类来解析 JSON 内容 所有部件都可以单独工作 并且我已经单独测试了所有部件 我只是不知道如何将值发送到
  • Cucumber 0.4.3 (cuke4duke) 与 java + maven gem 问题

    我最近开始为 Cucumber 安装一个示例项目 并尝试使用 maven java 运行它 我遵循了这个指南 http www goodercode com wp using cucumber tests with maven and ja
  • Django 视图中的“请求”是什么

    在 Django 第一个应用程序的 Django 教程中 我们有 from django http import HttpResponse def index request return HttpResponse Hello world
  • Python GTK+ 画布

    我目前正在通过 PyGobject 学习 GTK 需要画布之类的东西 我已经搜索了文档 发现两个小部件似乎可以完成这项工作 GtkDrawingArea 和 GtkLayout 我需要一些基本函数 如 fillrect 或 drawline
  • 如何使用 Python 3 检查目录是否包含文件

    我到处寻找这个答案但找不到 我正在尝试编写一个脚本来搜索特定的子文件夹 然后检查它是否包含任何文件 如果包含 则写出该文件夹的路径 我已经弄清楚了子文件夹搜索部分 但检查文件却难倒了我 我发现了有关如何检查文件夹是否为空的多个建议 并且我尝
  • 找到一个数字所属的一组范围

    我有一个 200k 行的数字范围列表 例如开始位置 停止位置 该列表包括除了非重叠的重叠之外的所有类型的重叠 列表看起来像这样 3 5 10 30 15 25 5 15 25 35 我需要找到给定数字所属的范围 并对 100k 个数字重复该
  • Eclipse 启动时崩溃;退出代码=13

    I am trying to work with Eclipse Helios on my x64 machine Im pretty sure now that this problem could occur with any ecli
  • python 中的“槽包装器”是什么?

    object dict 和其他地方的隐藏方法设置为这样的
  • Python:Goslate 翻译请求返回“503:服务不可用”[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我们不允许提出寻求书籍 工具 软件库等推荐的问题 您可以编辑问题 以便用事实和引文来回答 这个问题似乎不是关于主要由程序员使用的特定编程问
  • 重新分配唯一值 - pandas DataFrame

    我在尝试着assign unique值在pandas df给特定的个人 For the df below Area and Place 会一起弥补unique不同的价值观jobs 这些值将分配给个人 总体目标是使用尽可能少的个人 诀窍在于这
  • Firebase Firestore:获取文档的生成 ID (Python)

    我可以创建一个新文档 带有自动生成的 ID 并存储对其的引用 如下所示 my data key value doc ref db collection u campaigns add my data 我可以像这样访问数据本身 print d
  • pandas.read_csv 将列名移动一倍

    我正在使用位于的 ALL zip 文件here http www fec gov disclosurep PDownload do 我的目标是用它创建一个 pandas DataFrame 但是 如果我跑 data pd read csv
  • pytest找不到模块[重复]

    这个问题在这里已经有答案了 我正在关注pytest 良好实践 https docs pytest org en latest explanation goodpractices html test discovery或者至少我认为我是 但是
  • JSON:TypeError:Decimal('34.3')不是JSON可序列化的[重复]

    这个问题在这里已经有答案了 我正在运行一个 SQL 查询 它返回一个小数列表 当我尝试将其转换为 JSON 时 出现类型错误 查询 res db execute SELECT CAST SUM r SalesVolume 1000 0 AS
  • NLTK:查找单词大小为 2k 的上下文

    我有一个语料库 我有一个词 对于语料库中该单词的每次出现 我想获取一个包含该单词之前的 k 个单词和该单词之后的 k 个单词的列表 我在算法上做得很好 见下文 但我想知道 NLTK 是否提供了一些我错过的功能来满足我的需求 def size

随机推荐