安卓給文字漸變色(文本漸變色)

效果圖1

實現代碼

private fun setGradient(textView: TextView) {
    val endX = textView.paint.textSize * textView.text.length
    val linearGradient = LinearGradient(
        0f, 0f, endX, 0f,
        Color.parseColor("#FFFF68FF"),
        Color.parseColor("#FFFED732"),
        Shader.TileMode.CLAMP
    )
    textView.paint.shader = linearGradient
    textView.invalidate()
}

效果圖2

實現代碼

private fun setGradient(textView: TextView) {
    val endX = textView.paint.textSize * textView.text.length
    val colors = intArrayOf(
        Color.RED,
        Color.GREEN,
        Color.BLUE
    ) //顏色的數組
    val position = floatArrayOf(0f, 0.7f, 1.0f) //顏色漸變位置的數組
    val linearGradient =
        LinearGradient(0f, 0f, endX, 0f, colors, position, Shader.TileMode.CLAMP)
    textView.paint.shader = linearGradient
    textView.invalidate()
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。