Measuring Text27 Mar 2014
If you start manually drawing things to Android’s Canvas, you will probably start to draw text as well.
When doing so you need to know where to position the text when you draw, and to do that you will need to measure the text before drawing it, to compute the starting x/y values.
In an app recently I needed to draw some text centered both vertically and horizontally on the Canvas. So I started off with the following code:
I didn’t expect this to work first-time and it didn’t, producing the following:
Next I tried to position the text, calculating the text’s height/width and modifying the drawing X and Y values appropriately:
This time we’re getting much closer, but as you can see that it’s not quite centered correctly.
To make sure that I wasn’t seeing things, I added an extra call to draw a rectangle behind the text, with the exact bounds calculated with
As you can see, the text clearly draws outside of it’s calculated bounds, both in height and width.
Another text measuring method
It was at this point where I saw that Paint has another method for calculating text width:
This method only calculates the width and not the height, so I next tried combining the two methods:
Which resulted in almost perfectly centered text! Phew.