FontMetrics does not work when run on an Android device. Simulator is excellent

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:

enter image description hereenter image description hereenter image description here

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

enter image description here

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.

+4
java android
Mar 10 '11 at 11:25
source share
1 answer

There are two things that I would like to mention.

In my experience, I calculated the font height in pixels by subtracting FontMetrics.top from FontMetrics.bottom. This is due to the positive and negative values ​​for the lower and upper points along the Y axis. For this, refer to the Android documentation. Therefore, I would modify the calculateHeight method as follows:

 public static float calculateHeight(FontMetrics fm) { return fm.bottom - fm.top; } 

Secondly, you must remember that your defineTextSize method will return the size in pixels. If you use this to set the text size of a TextView or something else, then you must specify the units as TypedValue.COMPLEX_UNIT_PX. The default unit for this method is TypedValue.COMPLEX_UNIT_SP

+6
May 20 '11 at 6:34 am
source share
β€” -



All Articles