Android绘制文本之宽高

在自定义View的需要绘制文字时候,经常要考虑到文本的中心位置,这就需要计算并获取整个文本的宽度和高度,为以后使用方便,这里记录一下。

我们先看图了解一下字体基准线:

这里写图片描述
上面所有的属性都被封装在FontMetrics类中,通过它可以获取并计算文本的宽高,大体翻译一下,可能不准确;

top:在一个大小确定的字体中,被当做最高字形,基线(base)上方的最大距离。
ascent:单行文本中,在基线(base)上方被推荐的距离。
descent:单行文本中,在基线(base)下方被推荐的距离。
bottom:在一个大小确定的字体中,被当做最低字形,基线(base)下方的最大距离。
我捣鼓半天也就这样了,不解释了,如果我们想要计算这个文字的高度,只需要使用
(descent-ascent)

FontMetrics类的源代码,英文好的可以自己翻译一下;

public static class FontMetrics {
/**
* The maximum distance above the baseline for the tallest glyph in
* the font at a given text size.
*/
public float   top;
/**
* The recommended distance above the baseline for singled spaced text.
*/
public float   ascent;
/**
* The recommended distance below the baseline for singled spaced text.
*/
public float   descent;
/**
* The maximum distance below the baseline for the lowest glyph in
* the font at a given text size.
*/
public float   bottom;  
}

通过上面只是总体上知道字体的基线,通过下面代码会更清晰:

protected void onDraw(Canvas canvas) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(50);
canvas.drawColor(Color.WHITE);
float baseX = 30;
float baseY = 200;
//获取FontMetrics对象,可以获得top,bottom,ascent,descent
Paint.FontMetrics metrics = mPaint.getFontMetrics();
float top = baseY + metrics.top;
float ascent = baseY + metrics.ascent;
float descent = baseY + metrics.descent;
float bottom = +baseY + metrics.bottom;

String text = "Android*java";
canvas.drawText(text, baseX, baseY, mPaint);
//绘制base
canvas.drawLine(0, baseY, getWidth(), baseY, mPaint);
//绘制descent
mPaint.setColor(Color.RED);
canvas.drawLine(0, descent, getWidth(), descent, mPaint);
//绘制ascent
mPaint.setColor(Color.GREEN);
canvas.drawLine(0, ascent, getWidth(), ascent, mPaint);
//绘制bottom
mPaint.setColor(Color.BLACK);
canvas.drawLine(0, bottom, getWidth(), bottom, mPaint);
//绘制top
mPaint.setColor(Color.BLUE);
canvas.drawLine(0, top, getWidth(), top, mPaint);
}

上面先是绘制了文字,然后分别绘制了基线,descent线,ascent线,bottom线,top线;

想要计算高度只需要 descent-ascent
上面代码其实使用FontMetricsInt一样也可以达到效果。
Paint.FontMetricsInt metrics = mPaint.getFontMetricsInt();
另外descent和ascent也可以直接通过Paint来获取
mPaint.ascent()
mPaint.descent()

上面说的是如何计算文本高度,再看看宽度如何计算?
Paint提供了下面4个重载,返回文本的宽度并且都是float类型

public float measureText(String text)
public float measureText(char[] text, int index, int count)
public float measureText(String text, int start, int end)
public float measureText(CharSequence text, int start, int end)

上面四个重载看Api是很容易懂的,这里不再介绍了

当然想要一次性获取宽高,可以使用下面这种方式
代码片段如下:

String text = "Android";
Rect rect = new Rect();
mPaint.getTextBounds(text, 0, text.length(), rect);
int width = rect.width();//文本的宽度
int height = rect.height();//文本的高度  ---- 文本跟你要的中心点还是有点差距
//也相当于FontMetrics.descent -FontMetrics.ascent

原文

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值