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. 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. The font I chose is Choco Cooky
on a Samsung Galaxy S7.
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. 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. However this sounds like a lot of rewriting without any guarantee it will fix the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.