[英]Android Canvas Paint: getTextBounds() ignores system font
paint.getTextBounds(text, 0, text.length, textBounds)
...
canvas.drawText(text, canvas.width/2F - textBounds.exactCenterX(), canvas.height/2F - textBounds.exactCenterY(), paint)
I am using getTextBounds()
to center text in a circle graphic.我正在使用
getTextBounds()
在圆形图形中居中文本。 And while it looks fine with the system's default font ( left screenshot ), the text slides to the left of the circle center when I change the system font ( right screenshot ):虽然使用系统的默认字体(左截图)看起来不错,但当我更改系统字体(右截图)时,文本会滑动到圆心的左侧:
The 103
and 234
look particularly bad to me. 103
和234
对我来说特别糟糕。 The font I chose is Choco Cooky
on a Samsung Galaxy S7.我选择的字体是三星 Galaxy S7 上的
Choco Cooky
。
I put a debug print after the titular line of code and discovered it returns the same bounds regardless of typeface chosen , which would explain the result since the letters of Choco Cooky
are smaller.我在名义上的代码行之后放置了一个调试打印,发现无论选择哪种字体,它都会返回相同的边界,这可以解释结果,因为
Choco Cooky
的字母更小。 However this behavior seems like a bug to me.然而,这种行为对我来说似乎是一个错误。
What can I do to get an accurate measurement of a text's bounds that respects the system font?我该怎么做才能准确测量尊重系统字体的文本边界?
One approach might be to use a TextView
with a ShapeDrawable
as background, which then gets converted into a Bitmap so it can be used as a Google Maps marker.一种方法可能是使用
TextView
和ShapeDrawable
作为背景,然后将其转换为 Bitmap以便它可以用作谷歌地图标记。 However this sounds like a lot of rewriting without any guarantee it will fix the issue.然而,这听起来像很多重写,没有任何保证它会解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.