怎么在Android中使用FontMetrics對象計(jì)算坐標(biāo)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在Android中使用FontMetrics對象計(jì)算坐標(biāo),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

讓胡路網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,讓胡路網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為讓胡路超過千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個(gè)售后服務(wù)好的讓胡路做網(wǎng)站的公司定做!

Canvas繪制文本時(shí),使用FontMetrics對象,計(jì)算位置的坐標(biāo)。

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;
}

它的各基準(zhǔn)線可以參考下圖:

怎么在Android中使用FontMetrics對象計(jì)算坐標(biāo)

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

/** 繪制FontMetrics對象的各種線 */
mPaint.reset();
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(80);
// FontMetrics對象
FontMetrics fontMetrics = mPaint.getFontMetrics();
String text = "abcdefg";
// 計(jì)算每一個(gè)坐標(biāo)
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類有兩個(gè)方法

/**
 * 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都已經(jīng)揭曉了:

獲取height : mPaint.ascent() + mPaint.descent()

獲取width : mPaint.measureText(text)

上述就是小編為大家分享的怎么在Android中使用FontMetrics對象計(jì)算坐標(biāo)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

標(biāo)題名稱:怎么在Android中使用FontMetrics對象計(jì)算坐標(biāo)
文章分享:http://bm7419.com/article18/iippgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航域名注冊、App開發(fā)、關(guān)鍵詞優(yōu)化、建站公司外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站