安卓中在canvas.drawText()时发现很多需要注意的问题:
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setTextAlign(Paint.Align.LEFT);
//mPaint.setTextAlign(Paint.Align.CENTER);
初始化Paint对象时可以指定文本对齐方式,先讨论默认情况(也就是Paint.Align.LEFT)。
通过Paint对象可以获得与文本相关的两个重要的对象:
//获取可包裹文本的最小矩形(left,top,right,bottom指的是相对基准点的值,而不是实际的矩形的位置坐标)
Rect bounds=new Rect();
mPaint.getTextBounds(text,0,text.length(),bounds);
//获取FontMetrics对象
FontMetrics mFontMetrics = mPaint.getFontMetrics();
FontMetrics对象在安卓中的解释:
为了能直观的看出这些值的含义,我将这些值的相对位置画了出来:
相应的参数解释如下图:
无论是bounds(left,top,right,bottom)还是fontMetrics(top,ascent,descent,bottom)都是相对于基准点的数值差
因此,在绘制文本,并且要求文本中心在(x,y)处时,可以如下处理:
public class DrawTextUtil {
public static void drawText(Canvas canvas, String text, int x,int y,Paint paint){
//将基准点放在文本的中心(仍在基准线上)
paint.setTextAlign(Paint.Align.CENTER);
Rect bounds=new Rect();
paint.getTextBounds(text,0,text.length(),bounds);
//求出基准线y坐标的值,y-bounds.height()/2得到文本最上沿的y坐标,再-bounds.top地到基准线y坐标
int realY=y-bounds.height()/2-bounds.top;
canvas.drawText(text,x,realY,paint);
}
}
最后,附上绘制对应线的代码
public class DrawText extends View {
String text="dghiwk";
private Paint mPaint;
private Rect bounds;
private int drawX=100;
private int drawY=300;
private Paint.FontMetrics mFontMetrics;
public DrawText(Context context) {
super(context);
init(context);
}
public DrawText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DrawText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public DrawText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr);
}
public void init(Context context){
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(200);
mPaint.setTextAlign(Paint.Align.LEFT);
bounds=new Rect();
mPaint.getTextBounds(text,0,text.length(),bounds);
mFontMetrics = mPaint.getFontMetrics();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制文本
canvas.drawText(text,drawX,drawY,mPaint);
//top相对位置
mPaint.setColor(Color.RED);
canvas.drawLine(drawX,drawY+mFontMetrics.top,drawX+bounds.right,drawY+mFontMetrics.top,mPaint);
//ascent相对位置
mPaint.setColor(Color.BLUE);
canvas.drawLine(drawX,drawY+mFontMetrics.ascent,drawX+bounds.right,drawY+mFontMetrics.ascent,mPaint);
//descent相对位置
mPaint.setColor(Color.GRAY);
canvas.drawLine(drawX,drawY+mFontMetrics.descent,drawX+bounds.right,drawY+mFontMetrics.descent,mPaint);
//bottom相对位置
mPaint.setColor(Color.MAGENTA);
canvas.drawLine(drawX,drawY+mFontMetrics.bottom,drawX+bounds.right,drawY+mFontMetrics.bottom,mPaint);
//bounds相对位置
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawRect(drawX+bounds.left,drawY+bounds.top,drawX+bounds.right,drawY+bounds.bottom,mPaint);
//基准点位置
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(drawX,drawY,2,mPaint);
//基准线
mPaint.setColor(Color.GRAY);
canvas.drawLine(drawX,drawY,drawX+bounds.right,drawY,mPaint);
}