带圆角的背景的TextView
效果图
应用中经常会遇到使用圆角TextView的情况,这是一个自定义的圆角TextView
类文件
/**
* 带圆角的文本显示控件textview
* Created by Bear on 2017/6/23.
*/
class RoundTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int = 0) :
AppCompatTextView(context, attrs, defStyleAttr) {
private var mPaint: Paint = Paint()
private var mBgColor: Int = 0 //背景颜色
private var mRoundSize: Float = 0.toFloat() //圆角的值
private var mRoundType = RoundType.ROUND_RECT //圆角的类型
private var rectF: RectF? = null
enum class RoundType {
ROUND_RECT,
ROUND_TOP,
ROUND_BOTTOM,
LEFT_BOTTOM,
RIGHT_BOTTOM
}
init {
if (attrs != null)
init(attrs)
}
/**
* 设置textView的背景色
* @param bgColor
*/
var bgColor: Int
get() = mBgColor
set(bgColor) {
this.mBgColor = bgColor
invalidate()
}
var roundSize: Float
get() = mRoundSize
set(roundSize) {
this.mRoundSize = roundSize
invalidate()
}
var roundType: RoundType
get() = mRoundType
set(mRoundType) {
this.mRoundType = mRoundType
invalidate()
}
/**
* 初始化
*/
private fun init(attributeSet: AttributeSet) {
val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.RoundTextView)
mRoundSize = typedArray.getDimension(R.styleable.RoundTextView_roundSize, resources.getDimension(R.dimen.border_radius_normal))
mBgColor = typedArray.getColor(R.styleable.RoundTextView_bgColor, 0)
mRoundType = RoundType.values()[typedArray.getInt(R.styleable.RoundTextView_roundType, 0)]
typedArray.recycle()
gravity = Gravity.CENTER
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
rectF = RectF(0f, 0f, w.toFloat(), h.toFloat())
}
override fun onDraw(canvas: Canvas) {
if (mBgColor == 0) {
super.onDraw(canvas)
return
}
mPaint.color = mBgColor
mPaint.isAntiAlias = true
canvas.drawRoundRect(rectF!!, mRoundSize, mRoundSize, mPaint)
when (mRoundType) {
RoundTextView.RoundType.ROUND_RECT -> {
}
RoundTextView.RoundType.ROUND_TOP -> {
canvas.drawRect(0f, height - mRoundSize, mRoundSize, height.toFloat(), mPaint)
canvas.drawRect(width - mRoundSize, height - mRoundSize, width.toFloat(), height.toFloat(), mPaint)
}
RoundTextView.RoundType.ROUND_BOTTOM -> {
canvas.drawRect(0f, 0f, mRoundSize, mRoundSize, mPaint)
canvas.drawRect(width - mRoundSize, 0f, width.toFloat(), mRoundSize, mPaint)
}
RoundTextView.RoundType.LEFT_BOTTOM -> {
canvas.drawRect(0f, 0f, mRoundSize, mRoundSize, mPaint)
canvas.drawRect(0f, 0f, width.toFloat(), mRoundSize, mPaint)
canvas.drawRect(width - mRoundSize, height - mRoundSize, width.toFloat(), height.toFloat(), mPaint)
}
RoundTextView.RoundType.RIGHT_BOTTOM -> {
canvas.drawRect(width - mRoundSize, 0f, width.toFloat(), mRoundSize, mPaint)
canvas.drawRect(0f, 0f, width.toFloat(), mRoundSize, mPaint)
canvas.drawRect(0f, height - mRoundSize, mRoundSize, height.toFloat(), mPaint)
}
}
super.onDraw(canvas)
}
/**
* 用于代码生成
*/
class Builder(private val mContext: Context) {
private var bgColor: Int = 0 //背景颜色
private var roundSize: Float = 0.toFloat() //圆角的值
private var textColor: Int = 0 //文本颜色
private var textSize: Float = 0.toFloat() //文字大小
private var textString: String? = null//文本
private var roundType = RoundType.ROUND_RECT //圆角的类型
fun setRoundSize(roundSize: Float): Builder {
this.roundSize = roundSize
return this
}
fun setRoundType(roundType: RoundType): Builder {
this.roundType = roundType
return this
}
fun setTextColor(textColor: Int): Builder {
this.textColor = textColor
return this
}
fun setTextString(text: String): Builder {
this.textString = text
return this
}
fun setBgColor(bgColor: Int): Builder {
this.bgColor = bgColor
return this
}
fun setTextSize(textSize: Float): Builder {
this.textSize = textSize
return this
}
fun build(): RoundTextView {
val roundTextView = RoundTextView(mContext, null, 0)
roundTextView.mRoundType = roundType
roundTextView.mBgColor = bgColor
roundTextView.mRoundSize = roundSize
roundTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)
roundTextView.setTextColor(textColor)
roundTextView.text = textString
roundTextView.requestLayout()
return roundTextView
}
}
}
属性
<declare-styleable name="RoundTextView">
<attr name="roundSize" format="dimension|reference" />
<!--背景色-->
<attr name="bgColor" format="color" />
<attr name="roundType" format="enum">
<enum name="roundRect" value="0" />
<enum name="roundTop" value="1" />
<enum name="roundBottom" value="2" />
<enum name="leftBottom" value="3" />
<enum name="rightBottom" value="4" />
</attr>
</declare-styleable>