In a Scrabble-like word game app I am trying to draw black letters and values on yellow tiles.
While this works at Android emulators under Windows and Mac:
This doesn’t work (the text size is too big) on real devices like Moto G, Nexus 1 and 4:
<resources> <dimen name="big_tile_letter">60sp</dimen> <dimen name="big_tile_value">20sp</dimen> </resources>
And then use them in my BigTile.java class:
int letterSize = context.getResources().getDimensionPixelSize(R.dimen.big_tile_letter); mLetterPaint = new Paint(); mLetterPaint.setTextSize(letterSize); mLetterPaint.setAntiAlias(true); Rect letterBounds = new Rect(); mLetterPaint.getTextBounds(letter, 0, letter.length(), letterBounds); mLetterX = 0.45f * (width - letterBounds.width()); mLetterY = 0.45f * (height + letterBounds.height()); canvas.drawText(letter, mLetterX, mLetterY, mLetterPaint);
What am I doing wrong?
If what you want to is to convert a value from the R.dimen.something into pixels so:
float textSize = getResources().getDimensionPixelSize(R.dimen.something);
If what you want is to supply a manual value or something like that:
float maxTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, maxTextSize, getResources().getDisplayMetrics());
One thing that comes to mind is that you are using
sp values, which depend on the user setting in Settings / Display / Font Size. Can you check whether the setting is like
Large or so – this would apply an additional scaling on your font size. Also, I believe for 2.x devices the default setting is different than for 4.x devices (not sure though).
You could try use
dp values and see if that makes any difference.
The problem is that setTextSize gets the size value in SP, not in pixels. In your case getDimensionPixelSize converts sp to pixels, and then setTextSize treats the pixels as sp, and scales up again.
public void setTextSize(float size)
Set the default text size to the given value, interpreted as “scaled
pixel” units. This size is adjusted based on the current density and
user font size preference.
To fix this, you can call
setTextSize (TypedValue.COMPLEX_UNIT_PX, letterSize)
dp for the text sizes is definitely the way to go here, unless you wanted to manually compensate for the user’s font size setting by scaling your images as well.
Your problem is that your
big_tile image has inconsistent sizes in the mdpi (128×128) and xxhdpi (256×256) variants: xxhdpi should be 3 times the resolution of mdpi, but your image is only 2x the size, making it effectively ~85dp instead of 128dp.
I’m pretty sure I’ve seen Android Lint warn about this sort of stuff, try running it on your project.