FontMetrics ----- 绘制文本,获取文本高度

Canvas 绘制文本时,使用FontMetrics对象,计算位置的坐标。

 1 public static class FontMetrics {
 2     /**
 3      * The maximum distance above the baseline for the tallest glyph in 
 4      * the font at a given text size.
 5      */
 6     public float   top;
 7     /**
 8      * The recommended distance above the baseline for singled spaced text.
 9      */
10     public float   ascent;
11     /**
12      * The recommended distance below the baseline for singled spaced text.
13      */
14     public float   descent;
15     /**
16      * The maximum distance below the baseline for the lowest glyph in 
17      * the font at a given text size.
18      */
19     public float   bottom;
20     /**
21      * The recommended additional space to add between lines of text.
22      */
23     public float   leading;
24 }

它的各基准线可以参考下图:

 1 /** 绘制FontMetrics对象的各种线 */
 2 mPaint.reset();
 3 mPaint.setColor(Color.WHITE);
 4 mPaint.setTextSize(80);
 5 // FontMetrics对象
 6 FontMetrics fontMetrics = mPaint.getFontMetrics();
 7 String text = "abcdefg";
 8 // 计算每一个坐标
 9 float textWidth = mPaint.measureText(text);
10 float baseX = 30;
11 float baseY = 700;
12 float topY = baseY + fontMetrics.top;
13 float ascentY = baseY + fontMetrics.ascent;
14 float descentY = baseY + fontMetrics.descent;
15 float bottomY = baseY + fontMetrics.bottom;
16 // 绘制文本
17 canvas.drawText(text, baseX, baseY, mPaint);
18 // BaseLine描画
19 mPaint.setColor(Color.RED);
20 canvas.drawLine(baseX, baseY, baseX + textWidth, baseY, mPaint);
21 mPaint.setTextSize(20);
22 canvas.drawText("base", baseX + textWidth, baseY, mPaint);
23 // Base描画
24 canvas.drawCircle(baseX, baseY, 5, mPaint);
25 // TopLine描画
26 mPaint.setColor(Color.LTGRAY);
27 canvas.drawLine(baseX, topY, baseX + textWidth, topY, mPaint);
28 canvas.drawText("top", baseX + textWidth, topY, mPaint);
29 // AscentLine描画
30 mPaint.setColor(Color.GREEN);
31 canvas.drawLine(baseX, ascentY, baseX + textWidth, ascentY, mPaint);
32 canvas.drawText("ascent", baseX + textWidth, ascentY + 10, mPaint);
33 // DescentLine描画
34 mPaint.setColor(Color.YELLOW);
35 canvas.drawLine(baseX, descentY, baseX + textWidth, descentY, mPaint);
36 canvas.drawText("descent", baseX + textWidth, descentY, mPaint);
37 // ButtomLine描画
38 mPaint.setColor(Color.MAGENTA);
39 canvas.drawLine(baseX, bottomY, baseX + textWidth, bottomY, mPaint);
40 canvas.drawText("buttom", baseX + textWidth, bottomY + 10, mPaint);

相信通过以上程序,能够很好的理解topLine,buttomLine,baseLine,ascentLine,descentLine。

另外:Paint类有两个方法

 1 /**
 2  * Return the distance above (negative) the baseline (ascent) based on the
 3  * current typeface and text size.
 4  *
 5  * @return the distance above (negative) the baseline (ascent) based on the
 6  *         current typeface and text size.
 7  */
 8 public native float ascent();
 9 
10 /**
11  * Return the distance below (positive) the baseline (descent) based on the
12  * current typeface and text size.
13  *
14  * @return the distance below (positive) the baseline (descent) based on
15  *         the current typeface and text size.
16  */
17 public native float descent();

ascent():the distance above the baseline(baseline以上的height)

descent():the distance below the baseline(baseline以下的height)

所以ascent() + descent() 可以看成文字的height。

到此为止,怎么获取文字的height和width都已经揭晓了:

获取height : mPaint.ascent() + mPaint.descent()

获取width : mPaint.measureText(text)

原文地址:https://www.cnblogs.com/androidsj/p/4421780.html