Coil可以配合Kotlin协程实现图片加载,非常适合在Kotlin/Android项目中使用:

  • 加载性能好
  • 缓存管理(MemCache、DiskCache)、动态采样(Dynamic image sampling)、加载中暂停/终止等功能有助于提高图片加载效率
  • 体积小
    其包体积与Picasso相当,显著低于Glide和Fresco,仅仅只有1500个方法,但是在功能上却不输于其他同类库
  • 简单易用
    配合Kotlin扩展方法等语法优势,API简单易用
  • 技术先进
    基于Coroutine、OkHttp、Okio、AndroidX等先端技术开发,确保了技术上的先进性

引入Coil


build.gradle

dependencies {implementation 'io.coil-kt:coil:$latested_version' //目前最新是1.1.1︙
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="kaleidot725.coilsample"><uses-permission android:name="android.permission.INTERNET" />︙
</manifest>

使用ImageView加载图片


我们在activity_main.xml中声明ImageView,并使用Coil为ImageView加载图片:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><ImageViewandroid:id="@+id/image_view"android:layout_width="300dp"android:layout_height="300dp"android:layout_gravity="center"android:background="#CCCCCC"/><Buttonandroid:id="@+id/reload_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right|bottom"android:layout_margin="16dp"android:text="Reload"/>
</FrameLayout>

普通加载

val url = "https://img-blog.csdnimg.cn/20210124002108308.png"
val imageView = findViewById<ImageView>(R.id.image_view)
val reloadButton = findViewById<Button>(R.id.reload_button)
reloadButton.setOnClickListener {val disposable = imageView.load(url)disposable.dispose()
}
  • 通过扩展方法load加载url
  • 除了String以外,还支持HttpUrl 、Url、 File、 DrawableRes Int 、Drawable、 Bitmap等各种类型的加载
  • load返回Disposable,可以终止加载

crossfade加载

val url = "https://img-blog.csdnimg.cn/20210124002108308.png"
val imageView = findViewById<ImageView>(R.id.image_view)
val reloadButton = findViewById<Button>(R.id.reload_button)
reloadButton.setOnClickListener {imageView.load(url) {crossfade(true)}
}

通过kotlin的尾lambda语法,load(url) {... }内部启动crossfade

crossfade的动画时间

val url = "https://img-blog.csdnimg.cn/20210124002108308.png"
val imageView = findViewById<ImageView>(R.id.image_view)
val reloadButton = findViewById<Button>(R.id.reload_button)
reloadButton.setOnClickListener {imageView.load(url) {crossfade(3000)}
}

placeholder

placeholder预置展位图

val url = "https://img-blog.csdnimg.cn/20210124002108308.png"
val imageView = findViewById<ImageView>(R.id.image_view)
val reloadButton = findViewById<Button>(R.id.reload_button)
reloadButton.setOnClickListener {imageView.load(url) {placeholder(R.drawable.placeholder)crossfade(3000)}
}

error

记载失败时的错误占位图片

val url = "https://notfound.png"
val imageView = findViewById<ImageView>(R.id.image_view)
val reloadButton = findViewById<Button>(R.id.reload_button)
reloadButton.setOnClickListener {imageView.load(url) {error(R.drawable.error)}
}

Transformations图片变换


图片加载时,可以通过transformations对图片进行变换处理
目前支持 BlurCropCircleGrayscaleRouded corners 等四种变换效果

Blur

BlurTransformation: 高斯模糊变换

val url = "https://img-blog.csdnimg.cn/20210124002108308.png"
val imageView = findViewById<ImageView>(R.id.image_view)
val reloadButton = findViewById<Button>(R.id.reload_button)
reloadButton.setOnClickListener {imageView.load(url) {transformations(BlurTransformation(context = applicationContext, radius = 5f, sampling = 5f))}
}

CropCircle

CircleCropTransformation:圆形变换

 val url = "https://img-blog.csdnimg.cn/20210124002108308.png"val imageView = findViewById<ImageView>(R.id.image_view)val reloadButton = findViewById<Button>(R.id.reload_button)reloadButton.setOnClickListener {imageView.load(url) {transformations(CircleCropTransformation())}}

Grayscale

GrayscaleTransformation: 灰度变换

val url = "https://img-blog.csdnimg.cn/20210124002108308.png"
val imageView = findViewById<ImageView>(R.id.image_view)
val reloadButton = findViewById<Button>(R.id.reload_button)
reloadButton.setOnClickListener {imageView.load(url) {transformations(GrayscaleTransformation())}
}

Rouded corners

RoundedCornersTransformation: 圆角矩形变换

val url = "https://img-blog.csdnimg.cn/20210124002108308.png"
val imageView = findViewById<ImageView>(R.id.image_view)
val reloadButton = findViewById<Button>(R.id.reload_button)
reloadButton.setOnClickListener {imageView.load(url) {transformations(RoundedCornersTransformation(topRight = 10f, topLeft = 10f, bottomLeft =  10f, bottomRight =  10f))}

使用ImageLoader加载图片


除了上面介绍的在load(url)时,在尾lambda内进行各种配置以外,还可以通过创建ImageLoader,复用配置。上面介绍的所有配置都可以在ImageLoader中进行,此外,还可以进行Memory CacheBitmap Pooling 等更加多样化的配置:

val imageLoader = ImageLoader(applicationContext) {crossfade(true)placeholder(R.drawable.placeholder)error(R.drawable.error)availableMemoryPercentage(0.1)bitmapPoolPercentage(0.1)
}

如上,我们创建imageLoader实例后,后续可以在load(url)时,通过指定此实例复用上面的配置

val url = "https://notfound.png"
val imageView = findViewById<ImageView>(R.id.image_view)
val reloadButton = findViewById<Button>(R.id.reload_button)
reloadButton.setOnClickListener {imageView.load(url, imageLoader)
}

我们甚至可以通过Coil.setDefaultImageLoader,指定全局的默认ImageLoader,避免每次load时单独指定

  Coil.setDefaultImageLoader(ImageLoader(applicationContext) {crossfade(true)placeholder(R.drawable.placeholder)error(R.drawable.error)availableMemoryPercentage(0.1)bitmapPoolPercentage(0.1)})val url = "https://notfound.png"val imageView = findViewById<ImageView>(R.id.image_view)val reloadButton = findViewById<Button>(R.id.reload_button)reloadButton.setOnClickListener {imageView.load(url)}

最后


Coil配合Kotlin强大的语法特性打造了优秀的使用体验,功能上完全对标竞品毫不逊色。可以预见,随着Kotlin在Andorid开发中比重的日益提升,Coil必将超越竞品成为图片加载库的第一选项。

更多内容参考:
https://coil-kt.github.io/coil/

【Android】Coil:为kotlin而生的图片库相关推荐

  1. 【错误记录】Android 中使用 Kotlin 为 EditText 组件设置文本报错 ( Type mismatch. Required:Editable. Found:String )

    文章目录 一.报错信息 二.解决方案 一.报错信息 在 Android 中使用 Kotlin 开发 , EditText 组件如下 : 布局文件 : <EditTextandroid:id=&q ...

  2. android studio查看字节码,使用Android studio查看Kotlin的字节码教程

    Kotlin是一门JVM语言,它被google大力推广,现如今已经是Android官方推荐的开发语言了.为了更好的学习Kotlin,你必须要从字节码的角度来看待语法特点,这样可以更好的加深自己的理解. ...

  3. kotlin半生对象_如何在Kotlin中使用Actor实现对象池

    kotlin半生对象 by osha1 由osha1 如何在Kotlin中使用Actor实现对象池 (How to implement an Object-Pool with an Actor in ...

  4. 超越Android:探索Kotlin的应用领域

    by Adam Arold 亚当·阿罗德(Adam Arold) 超越Android:探索Kotlin的应用领域 (Going beyond Android: exploring Kotlin's a ...

  5. kotlin半生对象_Kotlin程序| 随播对象特征

    kotlin半生对象 伴侣对象 (Companion object) If you need a function or a property to be tied to a class rather ...

  6. 【翻译】ANDROID KTX – 使用Kotlin进行Android开发

    原文地址:ANDROID KTX – ANDROID DEVELOPMENT WITH KOTLIN [正在翻译中] 介绍 Android KTX is an open source library ...

  7. Android RatingBar使用Kotlin

    In this tutorial, we'll discuss and implement RatingBar in our Android app using Kotlin. 在本教程中,我们将使用 ...

  8. Android Studio和Kotlin入门

    Welcome to the series of tutorials on Android Development with Kotlin. This series is designed with ...

  9. kotlin半生对象_Kotlin单一对象,Kotlin伴侣对象

    kotlin半生对象 In this tutorial, we'll look at how Kotlin deals with Singleton Pattern and static proper ...

最新文章

  1. Java Web之XML基础
  2. Linux入门——一些linux基础
  3. MFC 字符串截取成数组 wcstok
  4. 千山独行-一个人的创业路(连载五)
  5. mysql 字符,索引
  6. vue部分样式无法修改
  7. c语言大作业银行排队叫号系统,C语言银行叫号系统课程设计.doc
  8. adb获取剪贴板内容_Android复制粘贴剪切板内容的一种方法
  9. 你必须掌握的人生定律
  10. Koo叔说Shader-- 熟悉渲染管线
  11. 【Numpy】1. n维数组,dtype,切片,索引
  12. 988-211所有学校
  13. shift+右键,没有“在此处打开命令窗口”选项
  14. 正睿OI DAY12 ks5
  15. 推挽输出、开漏输出和悬空输入等
  16. 使用spool导出数据
  17. scala java混合_Scala和Java混合项目搭建:(Eclipse)
  18. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.view.View andro
  19. APA格式参考文献引用
  20. Go 每日一库之 colly

热门文章

  1. mysql 中 union 的用法
  2. 香水商城平台小程序开发功能有哪些?
  3. 7.关于cdn、页面静态化
  4. Unity面试题F(Yanlz+...+F高薪就业+...+立钻哥哥+...)
  5. 【vs code】shell 语法提示,检查,运行调试
  6. grep用法详解:grep与正则表达式
  7. hideFocus(小技巧)
  8. NIFI Site to Site 安全模式资料学习整合(均来自官网翻译)
  9. 华为荣耀3c语言设置在哪个文件夹,(科普)详解Android系统SD卡各类文件夹名称...
  10. 618如何冲出重围?海尔智家:做好用户的数字化