在Android设备上运行时,FontMetrics不正确。模拟器很好

我有一个Android应用程序,根据Android设备的分辨率动态缩放文本。 我已在Android模拟器中的所有预定义分辨率上测试了此代码,我的代码运行正常。 (这包括与HTC Desire和Motorola Droid相同的分辨率) 它也适用于我的HTC Wildfire。 以下是模拟器的一些屏幕截图:   但是......我在HTC Desire上尝试了这个,我收到了使用Motorola Droid的用户报告,这些字体没有正确缩放: 请注意它是如何切断文本的。 任何想法为什么这不适用于这些特定的设备? 我目前有一个函数可以根据文本的可用高度缩小文本...这样的事情:
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;
}
任何想法为什么只在几个设备上发生这种情况?以及如何解决它? 还有其他字体指标,而不是我需要考虑的,我不知道吗?像Scale还是DPI? 谢谢。     
已邀请:
我想提一下两件事。 根据我的经验,我通过从FontMetrics.bottom中减去FontMetrics.top来计算字体高度(以像素为单位)。这是由于Y轴的底部和顶部的正负值。请参阅Android文档。所以我会改变你的calculateHeight方法如下:
public static float calculateHeight(FontMetrics fm) {
    return fm.bottom - fm.top;
}
其次,您应该记住,您的determineTextSize方法将返回以像素为单位的大小。如果您使用它来设置TextView的文本大小,那么您应该将单位指定为TypedValue.COMPLEX_UNIT_PX。此方法的默认单位是TypedValue.COMPLEX_UNIT_SP     

要回复问题请先登录注册