关于Canvas.drawText中xy位置问题

问:canvas.drawText("3", x, y, paint);  x和y是指画得时候数字3中心的坐标吗?还是左上角的坐标?
答:x默认是‘3’这个字符的左边在屏幕的位置,如果设置了paint.setTextAlign(Paint.Align.CENTER);那就是字符的中心,y是指定这个字符baseline在屏幕上的位置。

public void drawText (String text, float x, float y, Paint paint)
Since: API Level 1 Draw the text, with origin at (x,y), using the specified paint.
The origin is interpreted based on the Align setting in the paint.
Parameters
text The text to be drawn
x The x-coordinate of the origin of the text being drawn
y The y-coordinate of the origin of the text being drawn
paint The paint used for the text (e.g. color, size, style)

Canvas 作为绘制文本时,使用FontMetrics对象,计算位置的坐标。 它的思路和java.awt.FontMetrics的基本相同。 
FontMetrics对象它以四个基本坐标为基准,分别为:

FontMetrics.top
FontMetrics.ascent
FontMetrics.descent
FontMetrics.bottom

 

   Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG);
   textPaint.setTextSize( 35);
   textPaint.setColor( Color.WHITE);

   // FontMetrics对象
   FontMetrics fontMetrics = textPaint.getFontMetrics();
   String text = "abcdefghijklmnopqrstu";

   // 计算每一个坐标
   float baseX = 0;
   float baseY = 100;
   float topY = baseY + fontMetrics.top;
   float ascentY = baseY + fontMetrics.ascent;
   float descentY = baseY + fontMetrics.descent;
   float bottomY = baseY + fontMetrics.bottom;

   // 绘制文本
   canvas.drawText( text, baseX, baseY, textPaint);

   // BaseLine描画
   Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);>
   baseLinePaint.setColor( Color.RED);
   canvas.drawLine(0, baseY, getWidth(), baseY, baseLinePaint);

   // Base描画
   canvas.drawCircle( baseX, baseY, 5, baseLinePaint);

   // TopLine描画
   Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
   topLinePaint.setColor( Color.LTGRAY);
   canvas.drawLine(0, topY, getWidth(), topY, topLinePaint);

   // AscentLine描画
   Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
   ascentLinePaint.setColor( Color.GREEN);
   canvas.drawLine(0, ascentY, getWidth(), ascentY, ascentLinePaint);

   // DescentLine描画
   Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
   descentLinePaint.setColor( Color.YELLOW);
   canvas.drawLine(0, descentY, getWidth(), descentY, descentLinePaint);

   // ButtomLine描画
   Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
   bottomLinePaint.setColor( Color.MAGENTA);
   canvas.drawLine(0, bottomY, getWidth(), bottomY, bottomLinePaint);

 

drawText画字符串是baseline对齐的。所以要特别注意这点,不然画文字可能画到其它地方而误以为没有画出来。

如果baseline对齐的话:底端的Y坐标是:(行高-字体高度)/2+字体高度 ,但是字符串并不居中,经过测试如果:(行高-字体高度)/2+字体高度-6 ,就稍微居中了一点。 以上的方法只是一个取巧的做法,网上也没有找到设置文字居中的方法。

按上面办法会有误差。加上那段距离应该就行了:

FontMetrics fontMetrics = mPaint.getFontMetrics();
float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
float offY = fontTotalHeight / 2 - fontMetrics.bottom;
float newY = baseY + offY;
canvas.drawText(text, baseX, newY, paint);


 

  • 8
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值