Android GlideV4为图像添加圆形渐变前景

Android GlideV4为图像添加圆形渐变前景,android,performance,android-layout,kotlin,android-glide,Android,Performance,Android Layout,Kotlin,Android Glide,是否可以使用Glide v4在位图上添加圆形渐变前景 我试图这么做,但我从RoundedCorners()转换中透支了角点 val requestOptions = RequestOptions().transforms(CenterCrop(), RoundedCorners(14.dp())) Glide.with(context) .asBitmap() .load(item.image) .apply(requestOptions) .listener(object : RequestL

是否可以使用Glide v4在位图上添加圆形渐变前景

我试图这么做,但我从RoundedCorners()转换中透支了角点

val requestOptions = RequestOptions().transforms(CenterCrop(), RoundedCorners(14.dp()))
Glide.with(context)
.asBitmap()
.load(item.image)
.apply(requestOptions)
.listener(object : RequestListener<Bitmap> {
    override fun onLoadFailed(@Nullable e: GlideException?, model: Any, target: Target<Bitmap>, isFirstResource: Boolean): Boolean {
        return false
    }

    override fun onResourceReady(resource: Bitmap, model: Any, target: Target<Bitmap>, dataSource: DataSource, isFirstResource: Boolean): Boolean {
        val canvas = Canvas(resource)
        val p = Paint()
        p.shader = LinearGradient(0f, 0f, 0f, resource.height.toFloat(), Color.parseColor("#4c000000"), Color.parseColor("#cc000000"), Shader.TileMode.MIRROR);
        canvas.drawPaint(p)
        return false
    }
})
.into(imageView)
val requestOptions=requestOptions()
带(上下文)滑动
.asBitmap()
.load(项目.图像)
.apply(请求选项)
.listener(对象:RequestListener{
重写fun onLoadFailed(@Nullable e:GlideException?,型号:Any,目标:target,isFirstResource:Boolean):Boolean{
返回错误
}
重写onResourceReady(资源:位图、模型:任意、目标:目标、数据源:数据源、isFirstResource:Boolean):布尔{
val canvas=canvas(资源)
val p=油漆()
p、 shader=LinearGradient(0f,0f,0f,resource.height.toFloat(),Color.parseColor(#4c000000),Color.parseColor(#cc000000),shader.TileMode.MIRROR);
帆布.绘画颜料(p)
返回错误
}
})
.into(图像视图)
梯度或史密斯有这样的变换吗


我知道我可以将xml前台设置为ImageView,但我正试图避免这种情况。

最后,我找到了一个解决方案

若首先下载图片(从互联网上,我在源位图上绘制圆形渐变)

这是image\u rounded\u front\u gradient.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="90"
        android:endColor="#33000000"
        android:startColor="#99000000"
        android:type="linear" />

    <corners
        android:bottomLeftRadius="14dp"
        android:bottomRightRadius="14dp"
        android:topLeftRadius="14dp"
        android:topRightRadius="14dp" />

    <size
        android:width="276dp"
        android:height="340dp" />
</shape>

最后,我找到了一个解决方案

若首先下载图片(从互联网上,我在源位图上绘制圆形渐变)

这是image\u rounded\u front\u gradient.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="90"
        android:endColor="#33000000"
        android:startColor="#99000000"
        android:type="linear" />

    <corners
        android:bottomLeftRadius="14dp"
        android:bottomRightRadius="14dp"
        android:topLeftRadius="14dp"
        android:topRightRadius="14dp" />

    <size
        android:width="276dp"
        android:height="340dp" />
</shape>

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="90"
        android:endColor="#33000000"
        android:startColor="#99000000"
        android:type="linear" />

    <corners
        android:bottomLeftRadius="14dp"
        android:bottomRightRadius="14dp"
        android:topLeftRadius="14dp"
        android:topRightRadius="14dp" />

    <size
        android:width="276dp"
        android:height="340dp" />
</shape>