Kotlin标配的图片加载库
implementation("io.coil-kt:coil:1.1.1")
// 如果想要显示 Gif、SVG、视频帧等类型的图片,则需要额外引入对应的支持库:
implementation("io.coil-kt:coil-gif:1.0.0")
implementation("io.coil-kt:coil-svg:1.0.0")
implementation("io.coil-kt:coil-video:1.0.0")
//函数式编程思想
imageView.load(imageUrl1); //直接加载图片
imageView.load(R.mipmap.g4) //加载资源素材
直接使用load传入资源链接就好。
imageView.load(imageUrl1){
crossfade(true) //开启淡入淡出
crossfade(3000)
placeholder(R.mipmap.placeholder) //添加占位图
transformations(CircleCropTransformation()) //图片变换 圆形
}
imageView.load(errorUrl){
crossfade(true) //开启淡入淡出
crossfade(3000)
error(R.mipmap.error) //加载失败的情况
transformations(CircleCropTransformation()) //图片变换
}
imageView.load(imageUrl1){
crossfade(true) //开启淡入淡出
crossfade(3000)
placeholder(R.mipmap.placeholder) //添加占位图
transformations(RoundedCornersTransformation(topRight = 30f, topLeft = 30f, bottomLeft = 30f, bottomRight = 30f)) //图片变换 加圆角
}
imageView.load(imageUrl2){
crossfade(true) //开启淡入淡出
crossfade(3000)
//高斯模糊
transformations(BlurTransformation(context = applicationContext, radius = 5f, sampling = 5f))
}
imageView.load(imageUrl2){
crossfade(true) //开启淡入淡出
crossfade(3000)
//灰度
transformations(GrayscaleTransformation())
}
val imageLoader=ImageLoader.Builder(context = this)
.componentRegistry{
if (SDK_INT>28){
add(ImageDecoderDecoder())
}else{
add(GifDecoder())
}
}.build()
Coil.setImageLoader(imageLoader)
imageView.load("https://img-blog.csdnimg.cn/c271ed8cf0f541b08e1192d34e512448.gif")
imageView.load(imageUrl2){
crossfade(true) //开启淡入淡出
crossfade(3000)
//灰度
transformations(GrayscaleTransformation())
listener(
onStart ={ request ->
Log.d("lpf", "onError 开始加载...")
},
onError = { request, throwable ->
Log.d("lpf", "onError 加载失败...")
},
onCancel = { request ->
Log.d("lpf", "onCancel 加载重载中...")
},
onSuccess = { request, metadata ->
Log.d("lpf", "onSuccess 加载成功...")
}
)
}
框架本身提供了:
同时还允许用户继承Transformation接口实现自己的功能。
public interface Transformation {
public abstract fun key(): kotlin.String
public abstract suspend fun transform(pool: coil.bitmap.BitmapPool, input: android.graphics.Bitmap, size: coil.size.Size): android.graphics.Bitmap
}
实现这个接口,并实现key和transform方法就能实现自己的效果
class CustomColorFilterTransformation(
@ColorInt private val color: Int
) : Transformation {
override fun key(): String = "${CustomColorFilterTransformation::class.java.name}-$color"
override suspend fun transform(pool: BitmapPool, input: Bitmap, size: Size): Bitmap {
//获取添加效果图片的配置信息
val width = input.width
val height = input.height
val config = input.config
//从复用池里边获取最合适的Bitmap图片
val output = pool.get(width, height, config)
//创建画布和画笔
val canvas = Canvas(output)
val paint = Paint()
画笔的抗锯齿
paint.isAntiAlias = true
//颜色混合
paint.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)
//绘制到图片上
canvas.drawBitmap(input, 0f, 0f, paint)
return output
}
}
class CustomWatermarkTransformation(
private val waterMarkContent: String,
@ColorInt private val waterMarkColor: Int,
private val waterMarkSize: Float
) : Transformation {
override fun key(): String = "${CustomWatermarkTransformation::class.java.name}-${watermark}-${textColor}-${textSize}"
override suspend fun transform(pool: BitmapPool, input: Bitmap, size: Size): Bitmap {
val width = input.width
val height = input.height
val config = input.config
val output = pool.get(width, height, config)
val canvas = Canvas(output)
val paint = Paint()
paint.isAntiAlias = true
paint.textSize = waterMarkSize
paint.color = waterMarkColor
//先绘制目标bitmap
canvas.drawBitmap(input, 0f, 0f, paint)
//绘制水印
canvas.rotate(40f, width / 2f, height / 2f)
val textWidth = paint.measureText(waterMarkContent)
canvas.drawText(waterMarkContent, (width - textWidth) / 2f, height / 2f, paint)
return output
}
}
waterMarkContent:添加水印的文本内容
waterMarkColor:添加水印的颜色值
waterMarkSize:添加水印的文本内容的字体大小