Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

深入 canvas.drawText:搞懂 baseline、FontMetrics 與 Rect 的含義

編輯:關於 Android 程式設計

本來這篇文章是想寫寫設計高級跑馬燈的心得,結果在編寫過程中花了近一天多的時間,才徹底搞明白 canvas.drawText 第三個參數 [float y] 背後的真實含義。看完本文,相信能幫大家掌握 FontMetrics 類和 Rect 類各成員變量的具體含義。

drawText 引出的問題

先看看 API 中是如何定義 drawText 參數的。

  /** * Draw the text, with origin at (x,y), using the specified paint. The * origin is interpreted based on the Align setting in the paint. * * @param text The text to be drawn * @param x The x-coordinate of the origin of the text being drawn * @param y The y-coordinate of the baseline of the text being drawn * @param paint The paint used for the text (e.g. color, size, style) */ public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) { native_drawText(mNativeCanvasWrapper, text, 0, text.length(), x, y, paint.mBidiFlags, paint.getNativeInstance(), paint.mNativeTypeface); }

簡單解釋各參數:
@param text:要繪製的文本內容,好理解。
@param x:文本相對屏幕原點在 x 方向的距離,沒什麼深意。
@param y:文本 baseline 相對屏幕原點在 y 方向的距離。問題來了,baseline 是個什麼東西?
@param paint:畫筆。

baseline 的來龍去脈

先看看 baseline 在文字區域中所處的位置,通過一個實例來分析,這裡只貼主要代碼。

  @Override protected void onDraw(Canvas canvas) { mRect = new Rect(); mPaint = new Paint(); mPaint.setTextSize(64); mPaint.setColor(Color.RED); Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt(); mPaint.getTextBounds(textStr, 0, textStr.length(), mRect); int w = mRect.width(); int h = mRect.height(); mRect = new Rect(mRectLeft, mRectTop, mRectLeft+w, mRectTop+(-fontMetrics.top)+fontMetrics.bottom); canvas.drawRect(mRect, mPaint); mBaseLine = (mRect.bottom+mRect.top)/2 - (fontMetrics.bottom+fontMetrics.top)/2; mBaseLine = -fontMetrics.top; mPaint.setColor(Color.WHITE); canvas.drawText(textStr, mRectLeft, mBaseLine, mPaint); mPaint.setColor(Color.BLUE); mPaint.setStrokeWidth(3); canvas.drawLine(mRectLeft, mBaseLine, mRectLeft+w, mBaseLine, mPaint); }

效果圖:

分析

從圖上可以看出,baseline 並不是文字區域的底部,但這根線的位置想必大家都很熟悉——除非你沒學過英語書寫。

FontMetrics

FontMetrics 是 Paint 的靜態內部類,主要定義了 Paint 繪製文本時的關鍵坐標。API 是這樣描述的:

 Class that describes the various metrics for a font at a given text size. Remember, Y values increase going down, so those values will be positive, and values that measure distances going up will be negative. This class is returned by getFontMetrics(). 

大意是:根據設定的 fontsize 獲得字體的各種度量值,通過 getFontMetrics() 獲取。FontMetrics 類含有 5 個成員變量:

  public static class FontMetrics { public float top; public float ascent; public float descent; public float bottom; public float leading; }

很明顯,所有值都是相對於 baseline 來計算的。這裡重點關注 top:
top:字體可繪製區域最高位置與 baseline 的距離,由於它是負數,所以 -top 就是我們的 baseline 坐標值。
ascent:字體單個字符最高位置與 baseline 的距離。
descent:字體單個字符最低位置與 baseline 的距離,與 ascent 鏡像對應。
bottom:字體可繪製區域最低位置與 baseline 的距離。
leading:字與字之間額外的間距,一般為 0。

Rect

Rect 類表示一塊矩形區域。Paint 的 getTextBounds() 方法可以獲取對應 font size 下文本所占的矩形區域,注意這個矩形的坐標值同樣是相對 baseline 計算的。Rect 有四個成員變量:left、top、right、bottom。由於是相對 baseline 計算,真實的 top 和 bottom 都要再加上 baseline 的值。

借助實例理解 FontMetrics 和 Rect

主要源碼:

  @Override protected void onDraw(Canvas canvas) { mRect = new Rect(); mPaint = new Paint(); mPaint.getTextBounds(textStr, 0, 1, mRect); mPaint.setTextSize(textSize); mPaint.setColor(0xffF4A460); Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt(); mPaint.getTextBounds(textStr, 0, textStr.length(), mRect); int w = mRect.width(); int h = mRect.height(); mRect = new Rect(mRectLeft, mRectTop, mRectLeft+w, mRectTop+(-fontMetrics.top)+fontMetrics.bottom); canvas.drawRect(mRect, mPaint); mBaseLine = -fontMetrics.top + mRectTop; mPaint.setColor(Color.WHITE); canvas.drawText(textStr, mRectLeft, mBaseLine, mPaint); mPaint.setColor(Color.BLUE); mPaint.setStrokeWidth(4); canvas.drawLine(mRectLeft, mBaseLine, mRectLeft+w, mBaseLine, mPaint); mPaint.setColor(Color.GREEN); canvas.drawLine(mRectLeft, mBaseLine+fontMetrics.bottom, mRectLeft+2*w , mBaseLine+fontMetrics.bottom, mPaint); mPaint.setColor(Color.RED); canvas.drawLine(mRectLeft, mBaseLine+fontMetrics.top, mRectLeft+2*w , mBaseLine+fontMetrics.top, mPaint); mPaint.setColor(Color.GRAY); canvas.drawLine(mRectLeft, mBaseLine+fontMetrics.ascent, mRectLeft+w+50 , mBaseLine+fontMetrics.ascent, mPaint); mPaint.setColor(Color.WHITE); canvas.drawLine(mRectLeft, mBaseLine+fontMetrics.descent, mRectLeft+w+50 , mBaseLine+fontMetrics.descent, mPaint); mPaint.setColor(Color.YELLOW); canvas.drawLine(mRectLeft, mBaseLine+fontMetrics.leading, mRectLeft+w/2 , mBaseLine+fontMetrics.leading, mPaint); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.STROKE); mPaint.getTextBounds(textStr, 0, 1, mRect); mRect.top+=mBaseLine; mRect.bottom+=mBaseLine; canvas.drawRect(mRect, mPaint); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.STROKE); mPaint.getTextBounds(textStr, 0, 7, mRect); mRect.top+=mBaseLine; mRect.bottom+=mBaseLine; canvas.drawRect(mRect, mPaint); } 

效果圖:

FontMetrics:
1. 淺黃色區域就是文字的 FontMetrics 區域,可以看出文字在此區域垂直居中繪製。
2. 紅線為 top;灰線為 ascent;黃線為 leading。
3. 藍線為 baseline;白線為 descent;綠線為 bottom。

Rect:
1. 第一個小黑矩形為「測」字的 rect 區域。
2. 第二個長黑矩形為「測試:12hg」的 rect 區域。

總結

只有徹底搞懂 baseline,才能輕鬆掌握 drawText 接口,開發時才不會總為「文字為什麼偏上或偏下」而苦惱。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved