欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android使用FontMetrics对象计算位置坐标

程序员文章站 2022-04-30 22:43:14
canvas绘制文本时,使用fontmetrics对象,计算位置的坐标。 public static class fontmetrics { /**...

canvas绘制文本时,使用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;
  /**
   * the recommended additional space to add between lines of text.
   */
  public float  leading;
}

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

Android使用FontMetrics对象计算位置坐标

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

/** 绘制fontmetrics对象的各种线 */
mpaint.reset();
mpaint.setcolor(color.white);
mpaint.settextsize(80);
// fontmetrics对象
fontmetrics fontmetrics = mpaint.getfontmetrics();
string text = "abcdefg";
// 计算每一个坐标
float textwidth = mpaint.measuretext(text);
float basex = 30;
float basey = 700;
float topy = basey + fontmetrics.top;
float ascenty = basey + fontmetrics.ascent;
float descenty = basey + fontmetrics.descent;
float bottomy = basey + fontmetrics.bottom;
// 绘制文本
canvas.drawtext(text, basex, basey, mpaint);
// baseline描画
mpaint.setcolor(color.red);
canvas.drawline(basex, basey, basex + textwidth, basey, mpaint);
mpaint.settextsize(20);
canvas.drawtext("base", basex + textwidth, basey, mpaint);
// base描画
canvas.drawcircle(basex, basey, 5, mpaint);
// topline描画
mpaint.setcolor(color.ltgray);
canvas.drawline(basex, topy, basex + textwidth, topy, mpaint);
canvas.drawtext("top", basex + textwidth, topy, mpaint);
// ascentline描画
mpaint.setcolor(color.green);
canvas.drawline(basex, ascenty, basex + textwidth, ascenty, mpaint);
canvas.drawtext("ascent", basex + textwidth, ascenty + 10, mpaint);
// descentline描画
mpaint.setcolor(color.yellow);
canvas.drawline(basex, descenty, basex + textwidth, descenty, mpaint);
canvas.drawtext("descent", basex + textwidth, descenty, mpaint);
// buttomline描画
mpaint.setcolor(color.magenta);
canvas.drawline(basex, bottomy, basex + textwidth, bottomy, mpaint);
canvas.drawtext("buttom", basex + textwidth, bottomy + 10, mpaint);

相信通过以上程序,能够很好的理解topline,buttomline,baseline,ascentline,descentline。
另外:paint类有两个方法

/**
 * return the distance above (negative) the baseline (ascent) based on the
 * current typeface and text size.
 *
 * @return the distance above (negative) the baseline (ascent) based on the
 *     current typeface and text size.
 */
public native float ascent();
 
/**
 * return the distance below (positive) the baseline (descent) based on the
 * current typeface and text size.
 *
 * @return the distance below (positive) the baseline (descent) based on
 *     the current typeface and text size.
 */
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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。