0
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):

enter image description here enter image description here

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?

xjcl
  • 12,848
  • 6
  • 67
  • 89

1 Answers1

0

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.

xjcl
  • 12,848
  • 6
  • 67
  • 89