图像分割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的默认图像加载器 的相关文章

  • 在Java中使用BufferedWriter写入文件时监视文件大小?

    我正在将一个可能很长的项目列表写入文件 我正在写的项目的长度是可变的 如果生成的文件大小大于10M 则应将其分成多个文件 为了提高性能 我目前使用 BufferedWriter 如下所示 final FileOutputStream fos
  • 在 JavaFX 中拖动未装饰的舞台

    我希望将舞台设置为 未装饰 使其可拖动且可最小化 问题是我找不到这样做的方法 因为我遇到的示例是通过插入到主方法中的方法来实现的 我想通过控制器类中声明的方法来完成此操作 就像我如何使用下面的 WindowClose 方法来完成此操作 这是
  • Java 套接字:可以从一个线程发送并在另一个线程上接收吗?

    这可能是一个非常基本的问题 但我很难找到答案 让一个线程写入 Socket 的输出流 而另一个线程从 Socket 的输入流读取数据 这样可以吗 编辑 这是一个与外部服务器通信的客户端应用程序 我并不是想让两个线程互相交谈 很抱歉含糊不清
  • Java 7 中 Object 和 int 的比较

    最近我偶然发现了一个问题 让我停下来思考 对我来说 下面的代码应该总是会触发错误 但是当我的一位同事问我为什么 Eclipse 没有显示错误时 我无法回答任何问题 class A public static void main String
  • java.lang.IllegalArgumentException:addChild:子名称“/”不唯一

    java lang IllegalArgumentException addChild 子名称 不唯一 通过在 tomcat webapps 文件夹中启用和禁用 saml 单点登录来替换现有 war 文件时遇到此问题 我正在使用 apach
  • Java中无参数的for循环

    我在看别人的代码 发现了这段代码 for 我不是 Java 专家 这行代码在做什么 起初 我认为这会创建一个无限循环 但在该程序员使用的同一个类中 while true 其中 如果我错了 请纠正我 是一个无限循环 这两个相同吗 为什么有人会
  • Python `concurrent.futures`:根据完成顺序迭代 future

    我想要类似的东西executor map 除了当我迭代结果时 我想根据完成的顺序迭代它们 例如首先完成的工作项应该首先出现在迭代中 等等 这样 当且仅当序列中的每个工作项尚未完成时 迭代就会阻塞 我知道如何使用队列自己实现这一点 但我想知道
  • 使用 include 进行 JAXB 剧集编译不起作用

    我有 2 个模式 A B 我在 B 中重用了一些 A 元素 我不使用命名空间 我在用着
  • wsdl 没有服务元素

    我必须使用 WCF Web 服务并获得 WSDL 外部的 因此无法控制 WSDL 在 WSDL 定义中 我没有找到包含服务 端口和地址元素的服务元素 WSDL 中不存在这种情况正常吗 这对于 WCF WSDL 来说很常见吗 我正在尝试使用轴
  • Learning_rate 不是合法参数

    我正在尝试通过实现 GridSearchCV 来测试我的模型 但我似乎无法在 GridSearch 中添加学习率和动量作为参数 每当我尝试通过添加这些代码来执行代码时 我都会收到错误 这是我创建的模型 def define model op
  • 如何使用 Ajax 在 Flask 中发布按钮值而不刷新页面?

    我有一个问题 当我单击 Flask 应用程序中的按钮时 我想避免重新加载 我知道有 Ajax 解决方案 但我想知道如何将我的按钮链接到 ajax 函数以发布按钮值并运行链接到其值的 python 函数 这是我的 html 按钮 div di
  • 如何配置嵌入式 MongoDB 以在 Spring Boot 应用程序中进行集成测试?

    我有一个相当简单的 Spring Boot 应用程序 它公开一个小型 REST API 并从 MongoDB 实例检索数据 对 MongoDB 实例的查询通过基于 Spring Data 的存储库 下面的一些关键代码 Main applic
  • Python中的MariaDB连接器无法连接到远程服务器

    我使用与远程 Mariadb 服务器的连接已有几个月了 今天 无法再通过 macOS 上的 python mariadb 模块和 mariadb 连接器建立连接 基本安装如下 brew install mariadb connector c
  • 在Python中将罗马数字转换为整数

    根据 user2486 所说 这是我当前的代码 def romanMap map M 1000 CM 900 D 500 CD 400 C 100 XC 90 L 50 XL 40 X 10 IX 9 V 5 V 4 I 1 return
  • 用户的完整 UNIX 用户名

    想知道您是否知道是否有一种巧妙的方法可以从 shell 获取完整的用户名 示例 如果我的 UNIX 用户名是 froyo 那么我想获取我的全名 在本例中 如系统中注册的那样 froyo Abhishek Pratap Finger 命令可以
  • spring data jpa 过滤 @OneToMany 中的子项

    我有一个员工测试实体是父实体并且FunGroup信息子实体 这两个实体都是通过employeeId映射 我需要一种方法来过滤掉与搜索条件匹配的子实体 以便结果仅包含父实体和子实体 满足要求 员工测试类 Entity name Employe
  • Django 迁移错误 'TypeError: 序列项 1: 需要一个类似字节的对象,在 mysql-connector-pythoncursor_cent.py 文件上找到 str'

    我正在 Django 项目中使用 mysql connector 来处理 mysql 请求 问题是我正在使用 django admin startproject project 设置一个简单的项目 当我尝试进行简单的管理 py 迁移时 这是
  • 选择合适的IDE

    您会推荐使用以下哪种 IDE 语言来在 Windows 下开发涉及识别手势并与操作系统交互的项目 我将使用 OpenCV 库来执行图像处理任务 之后 我将使用 win32 API 或 NET 框架与操作系统交互 具体取决于您建议的工具 性能
  • Matplotlib 渲染日期、图像的问题

    我在使用 conda forge 的 Matplotlib v 3 1 3 和 python 3 7 时遇到问题 我拥有 Matplotlib 所需的所有依赖项 当我输入这段代码时 它应该可以工作 我得到了泼溅艺术 它基于此 YouTube
  • Chrome + 另一个进程:进程间通信比 HTTP/XHR 请求更快?

    我有一个进程 1 对视频流进行实时图像处理 我需要在 Chrome 中的 HTML 页面中渲染该视频 同一台计算机上的进程 2 在canvas or img or videoHTML5 元素 由于我有 1000x1000 像素 x 3 字节

随机推荐