<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [toc] # 1. 相關預備知識 我們知道在Canvas 中,使用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) { super.drawText(text, x, y, paint); } ~~~ 繪制的文本為text,位置為(x,y)。這里的坐標(x,y)和平時中所遇到的不同,這里不是左上角坐標,而是文本的右下角位置,如下圖所示: ![](https://img.kancloud.cn/e2/c5/e2c538874eda74b0473b630b883a14fa_701x221.png) 于此同時,在字體中存在四條邏輯的線,系統在繪制文字時還有 4 條線,分別是 ascent、descent、top 和 bottom。如下圖所示: ![](https://img.kancloud.cn/21/34/2134bbf39058a42af4f8c42919ea72ce_389x214.png) 對應的這些邏輯值存儲在FontMetrics中,且不是直接存儲在其中的,而是經過了轉換,也就是: ``` ascent = ascent 線的 y 坐標 - baseline 線的 y 坐標。 descent = descent 線的 y 坐標 - baseline 線的 y 坐標。 top = top 線的 y 坐標 - baseline 線的 y 坐標。 bottom = bottom 線的 y 坐標 - baseline 線的 y 坐標。 ``` 那么,如果我們要在自定義View中繪制出來這四條線,就需要基于baseline的坐標來進行計算,即: ~~~ // 繪制文本 val fontMetrics = mPaint.fontMetrics // startY這里就是baseline val ascent = fontMetrics.ascent + startY val bottom = fontMetrics.bottom + startY val top = fontMetrics.top + startY val descent = fontMetrics.descent + startY drawLine(startX, startY, startX + 300, startY, mPaint) // baseline drawLine(startX, ascent, startX + 300, ascent, mPaint) //ascent drawLine(startX, bottom, startX + 300, bottom, mPaint) // bottom drawLine(startX, top, startX + 300, top, mPaint) // top drawLine(startX, descent, startX + 300, descent, mPaint) // descent ~~~ # 2. 獲取字體寬高 ## 2.1 獲取字體寬度 可以利用畫筆工具來測量所占據的寬度: ~~~ // 使用畫筆工具來獲取字體的寬度 val defaultWidth = mPaint.measureText(mContent) ~~~ ## 2.2 獲取字體高度 ~~~ // 使用bottom線值減去top線值,得到字符串所占據的高度值 val bottom = mPaint.fontMetrics.bottom val top = mPaint.fontMetrics.top val defaultHeight = bottom - top ~~~ # 3. 獲取剛好罩住字體的最小矩形 可以創建一個Rect矩形對象,然后使用Paint的getTextBounds方法來傳入,進行測量: ~~~ // 獲取剛好罩住字體的矩形大小 val rect = Rect() mPaint.getTextBounds(mContent, 0, mContent.length, rect) Log.e("TAG", "rect: ${rect.toShortString()}", ) ~~~ 其中,其參數分別為: ~~~ /** * Retrieve the text boundary box and store to bounds. * * Return in bounds (allocated by the caller) the smallest rectangle that * encloses all of the characters, with an implied origin at (0,0). * * @param text string to measure and return its bounds * @param start index of the first char in the string to measure * @param end 1 past the last char in the string to measure * @param bounds returns the unioned bounds of all the text. Must be allocated by the caller */ public void getTextBounds(String text, int start, int end, Rect bounds) ~~~ 也就是第二、三個參數分別為始終的字符下標位置,前閉后開區間。 這里分別打印一下測量的字體的寬高和這里矩形的坐標: ![](https://img.kancloud.cn/3c/d6/3cd60c9e7b65e0749e1de4f4dccd46f7_512x58.png) 對應的矩形坐標,左上角為(0,-52),右下角為(129,1)。因為在代碼中我們并沒有給 getTextBounds()函數傳遞基線位置,那它就是以(0,0)點所在位置為基線來得到這個最小矩形的,所以這個最小矩形的位置就是以(0,0)點所在位置為基線的結果。 所以實際的矩形應該是向下平移baseline的距離。這里可以在onDraw方法中將它繪制出來: ~~~ // 獲取剛好罩住字體的矩形大小 val rect = Rect() mPaint.getTextBounds(mContent, 0, mContent.length, rect) val rectPaint = Paint() rectPaint.color = resources.getColor(R.color.rect, null) // #4400FF00 rectPaint.isDither = true rectPaint.isAntiAlias = true rectPaint.style = Paint.Style.FILL drawRect(rect.left.toFloat(), rect.top.toFloat() + startY, rect.right.toFloat(), rect.bottom.toFloat() + startY, rectPaint) ~~~ ![](https://img.kancloud.cn/76/5a/765ab56e95413dce7a8320a4331c86fd_111x61.png) # 4. 字體相關函數 設置文字大小: ``` setTextSize(float textSize) ``` 設置是否為粗體文字 ``` setFakeBoldText(boolean fakeBoldText) ``` 設置帶有刪除線效果 ``` setStrikeThruText(boolean strikeThruText) ``` 設置下畫線 ``` setUnderlineText(boolean underlineText) ``` 設置對其方式 ``` setTextAlign(Paint.Align align) ``` 設置字體樣式 ``` setTypeface(Typeface typeface) ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看