自定义控件

Android 自定义view中TextView的绘制

2016-10-21  本文已影响164人  超神的菠萝
为了将TextView绘制到布局的中间

<code>
private Stirng text = "test";
private Paint.FontMetrics fm;
</code>
X轴:
<code>
//文本x
float textX = width/2-paint.measureText(text)/2;
</code>
Y轴:
<code>
//文本y
float textY = height / 2 - fm.descent + (fm.descent - fm.ascent) / 2;
</code>
这种方式获取的Y轴坐标是尽可能接近TextView的baseline的位置~~
之后drawText()
<code>
canvas.drawText( text,textX,textY,paint);
</code>

上一篇 下一篇

猜你喜欢

热点阅读