歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中FontMetrics對象的各種基准線

Android中FontMetrics對象的各種基准線

日期:2017/3/1 10:18:38   编辑:Linux編程

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. }
它的各基准線可以參考下圖:


上圖其實是通過代碼畫出來的,具體代碼如下:

  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. * Return the distance below (positive) the baseline (descent) based on the
  11. * current typeface and text size.
  12. *
  13. * @return the distance below (positive) the baseline (descent) based on
  14. * the current typeface and text size.
  15. */
  16. 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)

Copyright © Linux教程網 All Rights Reserved