我是靠谱客的博主 怡然钻石,最近开发中收集的这篇文章主要介绍Glide无法使用任何Transform/Gilde使用Transform不生效问题说明以及Gilde加载任意角圆角实现,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
可能是由于下面的情况导致
1.官方不允许同时使用两个Transform,例如
apply(RequestOptions.circleCropTransform()).apply(RequestOptions.centerCropTransform())
只会生效一个
2.ImageView设置了scaleType ,如果是图片本身需要裁剪的话,所有的变换后的效果可能就被裁剪掉了
附上一个既能裁剪 又能 设置圆角的Transform(kotlin)
原理是先使用官方的centerCrop得到转换后的图片在进行圆角裁减
package com.free.vpn.shoora.main.widget
import android.graphics.*
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.CenterCrop
class GlideRoundedCornersTransform(private val mRadius: Float, private val mCornerType: CornerType) :
CenterCrop() {
enum class CornerType {
ALL, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT, TOP, BOTTOM, LEFT, RIGHT, TOP_LEFT_BOTTOM_RIGHT, TOP_RIGHT_BOTTOM_LEFT, TOP_LEFT_TOP_RIGHT_BOTTOM_RIGHT, TOP_RIGHT_BOTTOM_RIGHT_BOTTOM_LEFT, DEFAULT
}
override fun transform(
pool: BitmapPool,
toTransform: Bitmap,
outWidth: Int,
outHeight: Int
): Bitmap {
return roundCrop(pool, super.transform(pool, toTransform, outWidth, outHeight))!!
}
private fun roundCrop(pool: BitmapPool, source: Bitmap?): Bitmap? {
if (source == null) {
return null
}
val width = source.width
val height = source.height
var result: Bitmap? =
pool[source.width, source.height, Bitmap.Config.ARGB_8888]
if (result == null) {
result =
Bitmap.createBitmap(source.width, source.height, Bitmap.Config.ARGB_8888)
}
val canvas = Canvas(result!!)
val paint = Paint()
paint.shader = BitmapShader(
source,
Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP
)
paint.isAntiAlias = true
val path = Path()
drawRoundRect(canvas, paint, path, width, height)
return result
}
private fun drawRoundRect(
canvas: Canvas,
paint: Paint,
path: Path,
width: Int,
height: Int
) {
val rids: FloatArray
when (mCornerType) {
CornerType.ALL -> {
rids = floatArrayOf(
mRadius,
mRadius,
mRadius,
mRadius,
mRadius,
mRadius,
mRadius,
mRadius
)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.TOP_LEFT -> {
rids = floatArrayOf(mRadius, mRadius, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.TOP_RIGHT -> {
rids = floatArrayOf(0.0f, 0.0f, mRadius, mRadius, 0.0f, 0.0f, 0.0f, 0.0f)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.BOTTOM_RIGHT -> {
rids = floatArrayOf(0.0f, 0.0f, 0.0f, 0.0f, mRadius, mRadius, 0.0f, 0.0f)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.BOTTOM_LEFT -> {
rids = floatArrayOf(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, mRadius, mRadius)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.TOP -> {
rids = floatArrayOf(mRadius, mRadius, mRadius, mRadius, 0.0f, 0.0f, 0.0f, 0.0f)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.BOTTOM -> {
rids = floatArrayOf(0.0f, 0.0f, 0.0f, 0.0f, mRadius, mRadius, mRadius, mRadius)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.LEFT -> {
rids = floatArrayOf(mRadius, mRadius, 0.0f, 0.0f, 0.0f, 0.0f, mRadius, mRadius)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.RIGHT -> {
rids = floatArrayOf(0.0f, 0.0f, mRadius, mRadius, mRadius, mRadius, 0.0f, 0.0f)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.TOP_LEFT_BOTTOM_RIGHT -> {
rids = floatArrayOf(mRadius, mRadius, 0.0f, 0.0f, mRadius, mRadius, 0.0f, 0.0f)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.TOP_RIGHT_BOTTOM_LEFT -> {
rids = floatArrayOf(0.0f, 0.0f, mRadius, mRadius, 0.0f, 0.0f, mRadius, mRadius)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.TOP_LEFT_TOP_RIGHT_BOTTOM_RIGHT -> {
rids =
floatArrayOf(mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, 0.0f, 0.0f)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.TOP_RIGHT_BOTTOM_RIGHT_BOTTOM_LEFT -> {
rids =
floatArrayOf(0.0f, 0.0f, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius)
drawPath(rids, canvas, paint, path, width, height)
}
CornerType.DEFAULT -> {
rids = floatArrayOf(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)
drawPath(rids, canvas, paint, path, width, height)
}
}
}
private fun drawPath(
rids: FloatArray,
canvas: Canvas,
paint: Paint,
path: Path,
width: Int,
height: Int
) {
path.addRoundRect(RectF(0f, 0f, width.toFloat(), height.toFloat()), rids, Path.Direction.CW)
canvas.drawPath(path, paint)
}
}
最后
以上就是怡然钻石为你收集整理的Glide无法使用任何Transform/Gilde使用Transform不生效问题说明以及Gilde加载任意角圆角实现的全部内容,希望文章能够帮你解决Glide无法使用任何Transform/Gilde使用Transform不生效问题说明以及Gilde加载任意角圆角实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复