'android textview gradient does not apply for ImageSpan

i have a SpannableString which apply to TextView

fun appendIconBeforeText(context : Context, text : String, iconName : String, baseColor : String? = "#2da346") : SpannableStringBuilder{
        val span = SpannableStringBuilder(" $text")
        val start: Int = span.toString().indexOf(text)
        val end: Int = text.length
        val imgResId = context.resources.getIdentifier(iconName, "mipmap", context.packageName)
        val drawable = ContextCompat.getDrawable(context, imgResId)!!.mutate()
        drawable!!.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(Color.parseColor(baseColor), BlendModeCompat.SRC_ATOP)
        drawable!!.setBounds(0, 0, convertDpToPixel(22, context).toInt(), convertDpToPixel(22, context).toInt())
        val image = ImageSpan(drawable!!, ImageSpan.ALIGN_CENTER)
        span.setSpan(image, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        span.setSpan(ForegroundColorSpan(Color.parseColor(baseColor)), start, start + end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        return span
    }

Then i want to apply gradient to TextView, the text gradient is show success but only the Image can not apply gradient:

tv.post {
 val length = tv.paint.measureText(tv.text.toString())
 val angle = Math.toDegrees(gradient.angel.toDouble())
 val endX = cos(angle) * length
 val endY = sin(angle) * length
 val shader = LinearGradient(0f, 0f, endX.toFloat(), endY.toFloat(), gradient.colors.toIntArray(), gradient.locations.toFloatArray(), Shader.TileMode.CLAMP)
 tv.paint.shader = shader
 tv.invalidate()                                                                                                                                                                                         
}

Here is the result: enter image description here

Can someone help me what's wrong here? Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source