Android 图片加载框架Coil使用总结
轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/122040645
本文出自【趙彥軍的博客】
文章目錄
- 簡介
- 簡單使用
- 高斯模糊
- 圓角
- 圓形
- 灰色變換 GrayscaleTransformation
- Gif
- 監聽下載過程
- 取消下載
- 替換 okhttp 實例
- 自定義
- Coil 源碼分析
簡介
Coil 是一個 Android 圖片加載庫,通過 Kotlin 協程的方式加載圖片。特點如下:
- 更快: Coil 在性能上有很多優化,包括內存緩存和磁盤緩存,把縮略圖存保存在內存中,循環利用 bitmap,自動暫停和取消圖片網絡請求等。
- 更輕量級: Coil 只有2000個方法(前提是你的 APP 里面集成了 OkHttp 和 Coroutines),Coil 和 Picasso 的方法數差不多,相比 Glide 和 Fresco 要輕量很多。
- 更容易使用: Coil 的 API 充分利用了 Kotlin 語言的新特性,簡化和減少了很多樣板代碼。
- 更流行: Coil 首選 Kotlin 語言開發并且使用包含 Coroutines, OkHttp, Okio 和 AndroidX Lifecycles 在內最流行的開源庫。
Coil 名字的由來:取 Coroutine Image Loader 首字母得來。
github:https://github.com/coil-kt/coil
文檔:https://coil-kt.github.io/coil/image_loaders/
添加依賴:
implementation("io.coil-kt:coil:1.4.0")簡單使用
// 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 語法輕松配置請求選項:
imageView.load("https://www.example.com/image.jpg") {crossfade(true) //漸進進出placeholder(R.drawable.image) //加載中占位圖transformations(CircleCropTransformation()) //圓形圖error(R.drawable.image) //加載錯誤占位圖 }高斯模糊
//正常圖片 mBinding.image1.load(imageUrl)//高斯模糊-輕微模糊 mBinding.image11.load(imageUrl) {transformations(BlurTransformation(this@MainActivity, 5f, 10f))scale(Scale.FILL) }//高斯模式-嚴重模糊 mBinding.image12.load(imageUrl) {transformations(BlurTransformation(this@MainActivity, 20f, 40f))scale(Scale.FILL)}效果圖:
圓角
//沒有圓角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)}效果圖:
圓形
布局文件
<ImageViewandroid: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
簡單來說就是把彩色圖變成灰色的
mBinding.image1.load(imageUrl) {transformations(GrayscaleTransformation()) }效果圖:
Gif
添加依賴
implementation("io.coil-kt:coil-gif:1.4.0")官方文檔:https://coil-kt.github.io/coil/gifs/
創建 gif ImageLoader 實例
val imageLoader = ImageLoader.Builder(context).componentRegistry {if (SDK_INT >= 28) {add(ImageDecoderDecoder(context))} else {add(GifDecoder())}}.build()//設置全局唯一實例Coil.setImageLoader(imageLoader)加載 gif 圖片:
mBinding.image1.load(gifUrl)效果圖如下:
監聽下載過程
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 實例
coil 底層是使用 okhttp 作為網絡請求工具,可以設置 okHttpClient 實例
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) //磁盤緩策略 ENABLED、READ_ONLY、WRITE_ONLY、DISABLED.crossfade(true) //淡入淡出.crossfade(1000) //淡入淡出時間.okHttpClient { //設置okhttpClient實例okHttpClient}.build()Coil.setImageLoader(imageLoader)-
availableMemoryPercentage 設置用于此 ImageLoader 的內存緩存和位圖池的可用內存百分比,范圍:0-1 , 如果為0 , 則禁用內存緩存。
默認行為:
-
memoryCachePolicy 內存緩存策略,有4中策略,默認為 CachePolicy.ENABLED
-
diskCachePolicy 磁盤緩存策略,方式和內存策略一直
-
crossfade 開啟淡入淡出
Coil 源碼分析
Coil 是一個單例類
總結
以上是生活随笔為你收集整理的Android 图片加载框架Coil使用总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kotlin reduce、fold
- 下一篇: Android使用suspendCanc