Android圖片加載框架Coil的詳細(xì)使用總結(jié)
簡(jiǎn)介
Coil 是一個(gè) Android 圖片加載庫(kù),通過(guò) Kotlin 協(xié)程的方式加載圖片。特點(diǎn)如下:
- 更快: Coil 在性能上有很多優(yōu)化,包括內(nèi)存緩存和磁盤(pán)緩存,把縮略圖存保存在內(nèi)存中,循環(huán)利用 bitmap,自動(dòng)暫停和取消圖片網(wǎng)絡(luò)請(qǐng)求等。
- 更輕量級(jí): Coil 只有2000個(gè)方法(前提是你的 APP 里面集成了 OkHttp 和 Coroutines),Coil 和 Picasso 的方法數(shù)差不多,相比 Glide 和 Fresco 要輕量很多。
- 更容易使用: Coil 的 API 充分利用了 Kotlin 語(yǔ)言的新特性,簡(jiǎn)化和減少了很多樣板代碼。
- 更流行: Coil 首選 Kotlin 語(yǔ)言開(kāi)發(fā)并且使用包含 Coroutines, OkHttp, Okio 和 AndroidX Lifecycles 在內(nèi)最流行的開(kāi)源庫(kù)。
Coil 名字的由來(lái):取 Coroutine Image Loader 首字母得來(lái)。
github:https://github.com/coil-kt/coil
文檔:https://coil-kt.github.io/coil/image_loaders/
添加依賴(lài):
implementation("io.coil-kt:coil:1.4.0")
簡(jiǎn)單使用
// URL imageView.load("https://www.example.com/image.jpg") // Resource imageView.load(R.drawable.image) // File imageView.load(File("/path/to/image.jpg")) // And more...
可以使用 lambda 語(yǔ)法輕松配置請(qǐng)求選項(xiàng):
imageView.load("https://www.example.com/image.jpg") { crossfade(true) //漸進(jìn)進(jìn)出 placeholder(R.drawable.image) //加載中占位圖 transformations(CircleCropTransformation()) //圓形圖 error(R.drawable.image) //加載錯(cuò)誤占位圖 }
高斯模糊
//正常圖片 mBinding.image1.load(imageUrl) //高斯模糊-輕微模糊 mBinding.image11.load(imageUrl) { transformations(BlurTransformation(this@MainActivity, 5f, 10f)) scale(Scale.FILL) } //高斯模式-嚴(yán)重模糊 mBinding.image12.load(imageUrl) { transformations(BlurTransformation(this@MainActivity, 20f, 40f)) scale(Scale.FILL) }
效果圖:
圓角
//沒(méi)有圓角 mBinding.image1.load(imageUrl){ transformations(RoundedCornersTransformation()) scale(Scale.FILL) } //圓角一樣 mBinding.image11.load(imageUrl) { transformations(RoundedCornersTransformation(20f)) scale(Scale.FILL) } //圓角不一樣 mBinding.image12.load(imageUrl) { transformations( RoundedCornersTransformation( topLeft = 20f, topRight = 20f, bottomLeft = 50f, bottomRight = 50f ) ) scale(Scale.FILL) }
效果圖:
圓形
布局文件
<ImageView android:id="@+id/image1" android:layout_gravity="center" android:layout_width="150dp" android:layout_height="150dp" />
代碼:
mBinding.image1.load(imageUrl){ transformations(CircleCropTransformation()) scale(Scale.FILL) }
效果圖:
灰色變換 GrayscaleTransformation
簡(jiǎn)單來(lái)說(shuō)就是把彩色圖變成灰色的
mBinding.image1.load(imageUrl) { transformations(GrayscaleTransformation()) }
效果圖:
Gif
添加依賴(lài)
implementation("io.coil-kt:coil-gif:1.4.0")
官方文檔:https://coil-kt.github.io/coil/gifs/
創(chuàng)建 gif ImageLoader 實(shí)例
val imageLoader = ImageLoader.Builder(context) .componentRegistry { if (SDK_INT >= 28) { add(ImageDecoderDecoder(context)) } else { add(GifDecoder()) } } .build() //設(shè)置全局唯一實(shí)例 Coil.setImageLoader(imageLoader)
加載 gif 圖片:
mBinding.image1.load(gifUrl)
效果圖如下:
監(jiān)聽(tīng)下載過(guò)程
mBinding.image1.load(imageUrl) { listener( onStart = { request -> Log.d("coil-", "onStart") }, onError = { request, throwable -> Log.d("coil-", "onError") }, onCancel = { request -> Log.d("coil-", "onCancel") }, onSuccess = { request, metadata -> Log.d("coil-", "onSuccess") } ) }
取消下載
val imageUrl = "https://t7.baidu.com/it/u=433422559,1779762296&fm=193&f=GIF" val disposable = mBinding.image1.load(imageUrl) //取消加載 disposable.dispose()
替換 okhttp 實(shí)例
coil 底層是使用 okhttp 作為網(wǎng)絡(luò)請(qǐng)求工具,可以設(shè)置 okHttpClient 實(shí)例
val okHttpClient = OkHttpClient.Builder() .cache(CoilUtils.createDefaultCache(this)) .build() val imageLoader = ImageLoader.Builder(this).okHttpClient { okHttpClient }.build() Coil.setImageLoader(imageLoader)
自定義
val okHttpClient = OkHttpClient.Builder() .cache(CoilUtils.createDefaultCache(this)) .build() val imageLoader = ImageLoader.Builder(this) .availableMemoryPercentage(0.2) .diskCachePolicy(CachePolicy.ENABLED) //磁盤(pán)緩策略 ENABLED、READ_ONLY、WRITE_ONLY、DISABLED .crossfade(true) //淡入淡出 .crossfade(1000) //淡入淡出時(shí)間 .okHttpClient { //設(shè)置okhttpClient實(shí)例 okHttpClient }.build() Coil.setImageLoader(imageLoader)
availableMemoryPercentage 設(shè)置用于此 ImageLoader 的內(nèi)存緩存和位圖池的可用內(nèi)存百分比,范圍:0-1 , 如果為0 , 則禁用內(nèi)存緩存。
默認(rèn)行為:
memoryCachePolicy 內(nèi)存緩存策略,有4中策略,默認(rèn)為 CachePolicy.ENABLED
diskCachePolicy 磁盤(pán)緩存策略,方式和內(nèi)存策略一直
crossfade 開(kāi)啟淡入淡出
Coil 源碼分析
Coil 是一個(gè)單例類(lèi)
總結(jié)
到此這篇關(guān)于Android圖片加載框架Coil詳細(xì)使用的文章就介紹到這了,更多相關(guān)Android圖片加載框架Coil內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解Android?GLide圖片加載常用幾種方法
- Android 官推 kotlin-first 的圖片加載庫(kù)——Coil的使用入門(mén)
- Android編程圖片加載類(lèi)ImageLoader定義與用法實(shí)例分析
- Android基于Glide v4.x的圖片加載進(jìn)度監(jiān)聽(tīng)
- Android ListView實(shí)現(xiàn)ImageLoader圖片加載的方法
- Android中RecyclerView 滑動(dòng)時(shí)圖片加載的優(yōu)化
- Android圖片加載框架Glide的基本用法介紹
- Android圖片加載利器之Picasso基本用法
- 如何在Android中高效管理圖片加載
相關(guān)文章
Android Studio一直處于Building的兩種解決方法
很多朋友都遇到過(guò)打開(kāi)別人的項(xiàng)目一直處于Building‘XXX’Gradle project info的情況。下面小編給大家?guī)?lái)了Android Studio一直處于Building的解決方法,感興趣的朋友一起看看吧2018-08-08關(guān)于Android Studio封裝SDK的那些事兒
這篇文章主要給大家介紹了關(guān)于Android Studio封裝SDK的那些事兒,文中通過(guò)圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Android實(shí)現(xiàn)圖片異步請(qǐng)求加三級(jí)緩存
這篇文章主要向大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片異步請(qǐng)求加三級(jí)緩存的相關(guān)資料,需要的朋友可以參考下2016-02-02android byte[] 和short[]轉(zhuǎn)換的方法代碼
這篇文章主要介紹了android byte[] 和short[]轉(zhuǎn)換的方法代碼,有需要的朋友可以參考一下2014-01-01Android SQLite數(shù)據(jù)庫(kù)增刪改查操作的案例分析
本篇文章介紹了,在Android中SQLite數(shù)據(jù)庫(kù)增刪改查操作的案例分析,需要的朋友參考下2013-04-04Android 保存Fragment 切換狀態(tài)實(shí)例代碼
本文主要介紹Android Fragment的應(yīng)用,這里給大家用實(shí)例代碼詳細(xì)介紹了Android Fragment 切換狀態(tài),有需要的小伙伴可以參考下2016-07-07Android自定義View實(shí)現(xiàn)彈性小球效果
前段時(shí)間看到一個(gè)功能,是一個(gè)小球沿著固定軌跡彈動(dòng)的效果,那么這篇文章小編給大家分享在Android中如何自定義View來(lái)實(shí)現(xiàn)彈性小球的效果,有需要的可以參考借鑒。2016-09-09