Android中FontMetrics对象的各种基准线

Android中FontMetrics对象的各种基准线(以及怎么获取文字的width和height) 。

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. }
复制代码
它的各基准线可以参考下图:

120628055573351.png

上图其实是通过代码画出来的,具体代码如下:

  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。

复制代码
相信通过以上程序,能够很好的理解topLine,buttomLine,baseLine,ascentLine,descentLine。
另外:Paint类有两个方法

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

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

  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)

  18. descent():the distance below the baseline(baseline以下的height)
复制代码
获取height : mPaint.ascent() + mPaint.descent()

获取width : mPaint.measureText(text)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值