Android 는 FontMetrics 대상 을 사용 하여 위치 좌 표를 계산 합 니 다.
3967 단어 AndroidFontMetrics위치 좌표
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(텍스트)가 져 오기
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.