Android自动义控件的文字居中可以根据其绘制文字的绝对边界和绘制时的基线来计算偏移的值
代码:
package com.example.opencvapplication
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.graphics.setBlendMode
class MyTextView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
strokeWidth = 10f.dp
strokeCap = Paint.Cap.ROUND
style = Paint.Style.STROKE
}
val txt = "90%"
val txtRect = Rect()
var metrics = Paint.FontMetrics()
val txtPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
textSize = 50f.dp
color = Color.RED
textAlign = Paint.Align.CENTER
}
val rectF = RectF()
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
rectF.left = 0f + paint.strokeWidth / 2
rectF.right = w.toFloat() - paint.strokeWidth / 2
rectF.top = 0f + paint.strokeWidth / 2
rectF.bottom = h.toFloat() - paint.strokeWidth / 2
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
paint.color = Color.LTGRAY
//画圆
canvas?.drawOval(rectF, paint)
paint.color = Color.GREEN
//画弧线
canvas?.drawArc(rectF, -90f, 150f, false, paint)
//没加居中
canvas?.drawText(txt, width / 2f, height / 2f, txtPaint)
//textBounds方式居中 (静态文字的绝对的居中)
txtPaint.getTextBounds(txt, 0, txt.length, txtRect)
canvas?.drawText(
txt,
width / 2f,
height / 2f + (txtRect.bottom - txtRect.top) / 2,
txtPaint
)
//Metrics方式居中 (以基线的形式 适用动态文字)
txtPaint.getFontMetrics(metrics)
canvas?.drawText(
txt,
width / 2f,
height / 2f + (metrics.descent - metrics.ascent) / 2,
txtPaint
)
}
}