在自定义View的需要绘制文字时候,经常要考虑到文本的中心位置,这就需要计算并获取整个文本的宽度和高度,为以后使用方便,这里记录一下。
看图了解一下字体基准线:
上面所有的属性都被封装在FontMetrics类中,通过它可以获取并计算文本的宽高
top:在一个大小确定的字体中,被当做最高字形,基线(base)上方的最大距离。
ascent:单行文本中,在基线(base)上方被推荐的距离。
descent:单行文本中,在基线(base)下方被推荐的距离。
bottom:在一个大小确定的字体中,被当做最低字形,基线(base)下方的最大距离。
如果我们想要计算这个文字的高度,只需要使用(descent-ascent)
通过上面的代码只是总体上知道字体的基线,通过下面的代码会更清楚:
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();//文本的高度