要想理解这个首先看张图
image
像图上这样安卓的文字绘制是相对于基线绘制的,也就是图中的红线,而top+bottom的长度就等于字体高度.即等于|top|+|bottom|绝对值
实际绘制的时候取决于基线上一个点来绘制文字,而这个点有三种分别对应为left,center,right如下图
image
而drawText()方法中x,y坐标所指的点就是上图基线上三个点中的一个,具体是哪一个根据paint的setTextAlign()方法设置,默认为left
示例代码如下
Rect rect = new Rect(100,100,500,500);//画一个矩形
Paint rectPaint = new Paint();
rectPaint.setColor(Color.BLUE);
rectPaint.setStyle(Paint.Style.FILL);
canvas.drawRect(rect, rectPaint);
Paint textPaint = new Paint();
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(50);
textPaint.setStyle(Paint.Style.FILL);
//该方法即为设置基线上那个点究竟是left,center,还是right 这里我设置为center