type
status
date
slug
summary
tags
category
password
icon
⽂字的测量
- 绘制⽂字:
drawText()


⽂字测量难点之⼀:居中的纵向测量
- ⽅式⼀:
Paint.getTextBounds()
之后,使⽤(bounds.top + bounds.bottom) / 2
这种方式适合静态文字,如果文字会变,那么
bounds.top
和bounds.bottom
会发生变化,文字看起来就会出现跳动的现象- ⽅式⼆:
Paint.getFontMetrics(
) 之后,使⽤(fontMetrics.ascend + fontMetrics.descend) / 2
这种方式就适合动态文字:
⽂字测量难点之⼆:左对⻬
- ⽤ getTextBounds() 之后的 left 来计算


这里看似还没有完全对齐,是因为字体设计时的左边有一定的宽度,系统无法干预,上层尽最大的力也只能这样了。
⽂字测量难点之三:换⾏
- ⽤
android.graphics.Paint#breakText(java.lang.CharSequence, int, int, boolean, float, float[])
来计算
- 作者:shuouyang
- 链接:https://notion-tree.vercel.app/article/daec801b-b2e2-45e9-a7eb-4964b5726d77
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。