Android 自定义view中TextView的绘制

为了将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>

    原文作者:超神的菠萝
    原文地址: https://www.jianshu.com/p/656ec4f67a07
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞