文章目录
- 1 引入依赖
- 2 图片形状
- 2.1 圆形 CircleCrop
- 2.2 旋转 Rotate
- 2.3 圆角 RoundedCorners
- 2.4 自定义圆角 GranularRoundedCorners
- 2.5 监听图片资源加载结果
- 2.5.1 Drawable
- 2.5.2 Bitmap
1 引入依赖
implementation("com.github.bumptech.glide:glide:4.16.0")
2 图片形状
2.1 圆形 CircleCrop
Glide.with(this).load(url).transform(CircleCrop()).into(imgCover)
2.2 旋转 Rotate
Glide.with(this).load(url).transform(Rotate(60)).into(imgCover)
2.3 圆角 RoundedCorners
Glide.with(this).load(url).transform(RoundedCorners(60)).into(imgCover)
2.4 自定义圆角 GranularRoundedCorners
Glide.with(this).load(url).transform(GranularRoundedCorners(0F,60F,60F,0F)).into(imgCover)
2.5 监听图片资源加载结果
2.5.1 Drawable
Glide.with(this).load(url).transform(CircleCrop())
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>,
isFirstResource: Boolean
): Boolean {
Log.d("MainActivity", "onLoadFailed")
return false
}
override fun onResourceReady(
resource: Drawable,
model: Any,
target: Target<Drawable>?,
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
Log.d("MainActivity", "onResourceReady")
return false
}
}).into(imgCover)
2.5.2 Bitmap
Glide.with(this).asBitmap().load(url).transform(CircleCrop())
.listener(object : RequestListener<Bitmap> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Bitmap>,
isFirstResource: Boolean
): Boolean {
Log.d("MainActivity", "onLoadFailed")
return false
}
override fun onResourceReady(
resource: Bitmap,
model: Any,
target: Target<Bitmap>?,
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
Log.d("MainActivity", "onResourceReady")
return false
}
}).into(imgCover)