在使用Canvas绘制文字时,需要得到字符串的长度,Paint类内给了两个方法,measureText(),getTextBound();

可是对于同于字符串两个方法得出来的值有些差别:

Paint类measureText与getTextBounds的区别Paint类measureText与getTextBounds的区别

getTextBounds() 得到的宽度总是比 measureText() 得到的宽度要小一点。

就查看方法的源码

getTextBounds():

Paint类measureText与getTextBounds的区别Paint类measureText与getTextBounds的区别

measureText():

Paint类measureText与getTextBounds的区别Paint类measureText与getTextBounds的区别

Paint类measureText与getTextBounds的区别


额,只能看出两种方法测量的方式不一样,getTextBounds()使用了nativeGetCharArrayBounds();measureText()使用了native_measureText().

然而并不能解决疑问,就上网搜。运气不错,有位大神详细的回答了。


下面贴出重点:

Paint类measureText与getTextBounds的区别


ps:

这是字体大小60,在红色是边界矩形,在紫色是measureText的结果。

可以看到,左边界左边开始了一些像素,而measureText的值在左右两边都增加了这个值。


相关文章:

  • 2022-02-01
  • 2022-01-20
  • 2021-06-10
  • 2021-11-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-04
  • 2021-10-22
  • 2021-10-19
  • 2021-09-26
  • 2022-12-23
  • 2021-08-16
相关资源
相似解决方案