Android 는 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;
}
그것 의 각 기준 선 은 다음 그림 을 참고 할 수 있다.

위의 그림 은 사실 코드 를 통 해 그 려 진 것 이 고 구체 적 인 코드 는 다음 과 같다.

/**   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,descent Line 을 잘 이해 할 수 있 을 것 이 라 고 믿 습 니 다.
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(텍스트)가 져 오기
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기