class TextProgressBar
@JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defStyleAttr: Int = 0,
attrs: AttributeSet?
) : ProgressBar(context, attrs, defStyleAttr){
val progressText = ""
private var mPaint:Paint
private var textColor = Color.WHITE
private var textSize = 30f
init {
mPaint = Paint()
mPaint.color = textColor
mPaint.textSize = textSize
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val rect = Rect()
// 获取进度文本的矩形边界
mPaint.getTextBounds(progressText,0,progressText.length,rect)
val x= width/2 - rect.centerX()
val y = height/2 - rect.centerY()
// 把文本绘制在进度条的中间位置
canvas.drawText(progressText,x.toFloat(),y.toFloat(),mPaint)
}
}
自定义文本进度条
最新推荐文章于 2022-11-27 11:38:21 发布