I have an Android app that dynamically scales text depending on the resolution of the Android device. I tested this code at all the predefined resolutions in Android Simulator and my code is working fine. (This includes the same resolutions as HTC Desire and Motorola Droid)
It also works great on my HTC Wildfire.
Here are some screenshots from the simulations:



However ... I tried this on HTC Desire, and I had reports from users using the Motorola Droid that fonts do not scale correctly:

Pay attention to how to cut text.
Any ideas why this doesn't work on these devices?
I currently have a function that scales the text down depending on the available text height ... something like this:
public static float calculateHeight(FontMetrics fm) { return Math.abs(fm.ascent) + fm.descent; } public static int determineTextSize(Typeface font, float allowableHeight) { Paint p = new Paint(); p.setTypeface(font); int size = (int) allowableHeight; p.setTextSize(size); float currentHeight = calculateHeight(p.getFontMetrics()); while (size!=0 && (currentHeight) > allowableHeight) { p.setTextSize(size--); currentHeight = calculateHeight(p.getFontMetrics()); } if (size==0) { System.out.print("Using Allowable Height!!"); return (int) allowableHeight; } System.out.print("Using size " + size); return size; }
Any ideas why this only happens on a few devices? and how can i fix it? Is there another font metric that I need to consider here that I don't know about? How is the scale or DPI?
Thank.
java android
Sprooose Mar 10 '11 at 11:25 2011-03-10 11:25
source share