Android给Textview中文字添加颜色渐变、描边以及应用字体文件,自定义TextView

实现效果

在这里插入图片描述

加描边

要给textview加描边,可以通过在原textview下面再绘制一个textview并设置strokeWidth来实现比原textview的文本更粗的同样的文本,这里定义为backGroundText。具体实现方式看下面代码

加颜色渐变

这里我们仅给原textview加颜色渐变,通过给paint画笔加渲染器LinearGradient来实现颜色的渐变。具体实现方式看代码

设置应用本体字体文件

Typeface使用assets文件夹下的字体文件。

实现代码

通过自定义Textview来实现我们的需求,下面贴出完整代码


import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Shader
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat

/**
 * 字体加渐变、描边、assets字体文件应用
 */
class MyPurchaseColorTextView : AppCompatTextView {
    constructor(context: Context) : super(context) {
    	//同时初始化描边TextView
        backGroundText = TextView(context)
        //应用字体文件,实现描边的TextView也需要应用同样的字体文件
        if (!isInEditMode) {
            typeface = Typeface.createFromAsset(context.assets, "AlibabaSans-Black.ttf")
            backGroundText?.typeface = typeface
        }
    }

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
        backGroundText = TextView(context, attributeSet)
        if (!isInEditMode) {
            typeface = Typeface.createFromAsset(context.assets, "AlibabaSans-Black.ttf")
            backGroundText?.typeface = typeface
        }
    }

    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        backGroundText = TextView(context, attributeSet, defStyleAttr)
        if (!isInEditMode) {
            typeface = Typeface.createFromAsset(context.assets, "AlibabaSans-Black.ttf")
            backGroundText?.typeface = typeface
        }
    }

    var textViewWidth: Int = 0 //TextView的宽度
    var textViewHeight: Int = 0 //TextView的高度
    private var mLinearGradient: LinearGradient? = null //渲染器
    private var paint: Paint? = null

    private var backGroundText: TextView? = null //用于描边的TextView

    override fun setLayoutParams(params: ViewGroup.LayoutParams?) {
        //同步布局参数
        backGroundText?.layoutParams = params
        super.setLayoutParams(params)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val tt = backGroundText!!.text
        //两个TextView上的文字必须一致
        if (tt == null || tt != this.text) {
            backGroundText!!.text = text
            this.postInvalidate()
        }
        backGroundText!!.measure(widthMeasureSpec, heightMeasureSpec)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        backGroundText!!.layout(left, top, right, bottom)
        super.onLayout(changed, left, top, right, bottom)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //在 onSizeChanged 方法中获取到高度,并对各个类进行初始化
        if (textViewHeight == 0) {
            textViewHeight = measuredHeight
            if (textViewHeight > 0) {
                //得到 父类 TextView 中写字的那支笔
                paint = getPaint()
                //初始化线性渲染器
                mLinearGradient = LinearGradient(
                    0f, 0f, 0f, textViewHeight.toFloat(),
                    //这里代表颜色渐变的路径,(初始X,初始y,结束x,结束y),这里的设置表示我们仅需要从上到下实现渐变,有其他需求自行设置
                    intArrayOf(
                        ContextCompat.getColor(context, R.color.color_FD66A4),
                        ContextCompat.getColor(context, R.color.color_FFA932)
                    ), null, Shader.TileMode.CLAMP
                );
                //把渲染器给笔套上
                paint?.setShader(mLinearGradient);
            }
        }
    }

    override fun onDraw(canvas: Canvas) {
        val tp1 = backGroundText!!.paint
        //设置描边宽度
        tp1.strokeWidth = 14f
        //背景描边并填充全部
        tp1.style = Paint.Style.FILL_AND_STROKE
        //设置描边颜色
        backGroundText!!.setTextColor(Color.parseColor("#FFFFFF"))
        //将背景的文字对齐方式做同步
        backGroundText!!.gravity = gravity
        backGroundText!!.draw(canvas)
        super.onDraw(canvas)
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值