Android 设计TextView的样式,动态改变颜色

设计Border的颜色、弧度以及宽度

首先在value的styles中添加以下代码

	// 自定义控件的属性, format定义属性
    <declare-styleable name="TextViewBorder">
        <attr name="borderColor" format="color"/>
        <attr name="borderRadius" format="integer"/>
    </declare-styleable>

TextBorderView

package com.demo.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.widget.TextView
import com.demo.flowHistory.R

/**
 * 复写TextView,增加功能
 * 文本框填充方式:STROKE FILL
 * 设置文本框边框颜色
 * 设置文本填充颜色
 * 设置文本框的弧度
 * 设置文本框的宽度
 */
@SuppressLint("AppCompatCustomView")
class TextBorderView(context: Context,attrs: AttributeSet?) :
    TextView(context, attrs) {
    var mStrokeColor = 0
    var mFillColor = 0
    var mRadius = 0
    var mStrokeWidth = 1
    var mStyle = Paint.Style.STROKE  // 文本框填充方式:STROKE FILL
    private val borderPaint = Paint()  // 设置Stroke
    private val fillPaint = Paint()  // 设置Fill
    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {

        val w = this.measuredWidth
        val h = this.measuredHeight
        val r = RectF(0F, 0F, (w - 1).toFloat(), (h - 1).toFloat())

        // 设置边框颜色
        borderPaint.color = mStrokeColor
        borderPaint.strokeWidth = mStrokeWidth.toFloat()
        borderPaint.style = Paint.Style.STROKE
        borderPaint.isAntiAlias = true

        canvas.drawRoundRect(r, mRadius.toFloat(), mRadius.toFloat(), borderPaint)

        // 设置填充颜色
        fillPaint.color = mFillColor
        fillPaint.strokeWidth = mStrokeWidth.toFloat()
        fillPaint.style = Paint.Style.FILL
        fillPaint.isAntiAlias = true

        canvas.drawRoundRect(r, mRadius.toFloat(), mRadius.toFloat(), fillPaint)

        super.onDraw(canvas)
    }

    /**
     * 设置边框颜色
     */
    fun setStrokeColor(newColor: Int) {
        mStrokeColor = newColor
        invalidate()
        requestLayout()
    }

    /**
     * 设置填充颜色
     */
    fun setFillColor(newColor: Int) {
        mFillColor = newColor
        invalidate()
        requestLayout()
    }

    /**
     * 设置文本框弧度
     */
    fun setViewRadius(newRadius: Int) {
        mRadius = newRadius
        invalidate()
        requestLayout()
    }

    /**
     * 设置边框宽度
     */
    fun setStrokeWidth(newWidth: Int){
        mStrokeWidth = newWidth
        invalidate()
        requestLayout()
    }

    /**
     * 设置填充方式 :FILL STROKE FILL_AND_STROKE
     */
    fun setViewStyle(newStyle: Paint.Style){
        mStyle = newStyle
        invalidate()
        requestLayout()
    }

    init {
        val a = context.theme.obtainStyledAttributes(attrs,
            R.styleable.TextViewBorder, 0, 0)
        try {
            mStrokeColor = a.getInteger(R.styleable.TextViewBorder_borderColor, 0)
            mRadius = a.getInteger(R.styleable.TextViewBorder_borderRadius, 0) //  默认为矩形 弧度为0

        } finally {
            a.recycle()
        }
    }
}




使用

import com.demo.widget.TextBorderView
import androidx.core.content.ContextCompat

val tv = TextBorderView(context, null)
tv.setFillColor(ContextCompat.getColor(context, R.color.red)) // color形式为@ColorRes int id
tv.setStrokeColor(ContextCompat.getColor(context, R.color.black)) 
tv.setViewRadius(44)  // tv.mRadius = 44
tv.setStrokeWidth(2)

tv.setPadding(30,20, 30, 20)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值