Skip to content

Commit 1b9d3b5

Browse files
committed
Update
1 parent 2300836 commit 1b9d3b5

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

‎CustomView/Advance/[7]DrawText.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,37 @@ leading | + | 行间距,当前行bottom与下一行top之间的距离的推荐
182182

183183
> 不会, **FontMetrics中的数值受字体大小(FontSize) 和 字体风格(Typeface) 影响**, 而不会因为具体绘制的内容不同而改变,给Paint设置完字体大小和字体风格后就能获取到正确的FontMetrics。
184184
185+
**获取FontMetrics的方法**
186+
``` java
187+
Paint.FontMetrics mMetrics = mPaint.getFontMetrics();
188+
189+
Log.e("TAG", "top=" + mMetrics.top);
190+
Log.e("TAG", "ascent=" + mMetrics.ascent);
191+
Log.e("TAG", "descent=" + mMetrics.descent);
192+
Log.e("TAG", "bottom=" + mMetrics.bottom);
193+
Log.e("TAG", "leading=" + mMetrics.leading);
194+
```
195+
结果:
196+
```
197+
05-15 21:18:13.315 13112-13112/com.gcssloop.canvas E/TAG: top=-152.98828
198+
05-15 21:18:13.315 13112-13112/com.gcssloop.canvas E/TAG: ascent=-129.88281
199+
05-15 21:18:13.315 13112-13112/com.gcssloop.canvas E/TAG: descent=34.179688
200+
05-15 21:18:13.315 13112-13112/com.gcssloop.canvas E/TAG: bottom=37.939453
201+
05-15 21:18:13.315 13112-13112/com.gcssloop.canvas E/TAG: leading=0.0
202+
```
203+
204+
**由于FontMetrics中的 ascent 与 descent 比较常用,所以可以直接通过Paint获取:**
205+
``` java
206+
Log.e("TAG", "P.ascent=" + mPaint.ascent());
207+
Log.e("TAG", "P.descent=" + mPaint.descent());
208+
```
209+
结果,可以看到与通过FontMetrics获得的结果相同。
210+
```
211+
05-15 21:24:18.950 13112-13112/com.gcssloop.canvas E/TAG: P.ascent=-129.88281
212+
05-15 21:24:18.955 13112-13112/com.gcssloop.canvas E/TAG: P.descent=34.179688
213+
```
214+
215+
185216
#### 文字居中
186217

187218
对于绘制居中的文本来说,我们可以封装一个方法用中心点作为绘制坐标,在绘制的时候转换为实际坐标。

0 commit comments

Comments
 (0)