For my Android game I have some calls to Canvas.drawText()
.
For testing, I use the standard font size which seems to be working fine.
However, when I bump up the resolution to a higher density, the larger images are automatically loaded but the text is now incredibly small.
Is there an easy way to calculate what size the text should be drawn at or am I bound to do this manually?
edit: What was the point of editing my post @Suragch ?
The easiest way is to define your font sizes in your resources with the units of scale-independent pixels (sp
) -- this unit is like density independent pixels (dp
or dip
) in that it takes into account the screen density but it also takes into account the font-size setting of the user.
To add a new dimension create a dimens.xml
file in your res/values
folder and enter the following code to add a new dimension with the name myFontSize
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="myFontSize">20sp</dimen>
</resources>
You can then get the text size in your application using
int scaledSize = getResources().getDimensionPixelSize(R.dimen.myFontSize);
The resulting size will be correctly scaled to take into account the current screen density and font-size setting.
For more information see the Android Developers page on More Resources.
The easiest way is to define your font size in your resources directory with the units of scale-independent pixel (sp)
or the density-independent pixel (dp)
. Then get the text size using
int scaledSize = getResources().getDimensionPixelSize(R.dimen.font_size);
mTextView.setTextSize(scaledSize);
This will scale the text-size appropriately according to the current screen density/resolution and the user's font-size setting.
How to define font size in resource directory?
Create a file named dimens.xml
in the folder res->values
. Then copy the following code.
<resources>
<dimen name="font_size">25sp</dimen> // sp or dp accordingly
</resources>
The name
attribute of the tag <dimen>
will be used as resource ID.
When calling Canvas.drawText()
the text size is first determined by the passed in Paint
object, which can be set via Paint.setTextSize()
. The text size is automatically scaled by Canvas
based on the canvas density, which can be found using Canvas.getDensity()
.
When setting the text size on a paint object that will be drawn on Canvas, work with a unit value of dp
or sp
and let Canvas handle the scaling for you.
You can do it yourself using a simple math operation:
You need to calculate a relation that draws your text just the same size for any canvas size, so use the actual canvas size, like this:
double relation = Math.sqrt(canvas.getWidth() * canvas.getHeight());
But that number is just too big, so divide it by one that suits your needs, lets say 250:
relation = relation / 250;
Now you can set your text size like this:
paint.setTextSize((float) (myFontSize * relation));
You don't have to necessary divide relation by any number but in that case you will have to use very small font sizes for the variable myFontSize
. For me 250 works just fine to use regular font sizes that will adjust to any screen size, since you are already taking in count the pixels dimension from your canvas.