在Android设备上运行的FontMetrics不正确。模拟器罚款 [英] FontMetrics not correct when run on android device. Simulator fine

查看:206
本文介绍了在Android设备上运行的FontMetrics不正确。模拟器罚款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序取决于Android设备的分辨率动态调整文本。
我已经测试了在Android模拟器的所有predefined决议,这code和我的code正常工作。 (这包括在同一决议,作为HTC Desire的与摩托罗拉Droid)

I have an Android App that dynamically scales text depending on the resolution of the android device. I have tested this code on all the predefined resolutions in the Android Simulator and my code works fine. (This includes the same resolutions as on HTC Desire and Motorola Droid)

它也可以在我的HTC野火的罚款。

It also works fine on my HTC Wildfire.

下面是一些屏幕截图来自模拟器:

Here are some screen shots from the simulators:



不过......我已经试过这对HTC Desire的,和我有使用摩托罗拉Droid用户的报告,该字体不正确缩放:

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

请注意它是如何砍文本关闭。

Note how it is chopping the text off.

这是为什么不上这些特定设备的工作任何想法?

Any ideas why this is not working on these particular devices?

我目前有缩放文本向下取决于文本......像这样可用高度的函数:

I currently have a function that scales the text down depending on the available height for the text... 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;
}

任何想法,为什么这是发生在只有一对夫妇的设备?和我如何能解决这个问题?
是否有另一种字体的度量比我这里需要考虑,我不知道?像秤或DPI?

Any ideas why this is happening on only a couple of devices? and how I can fix it? Is there another font metric than I need to be considering here which I don't know about? Like Scale or DPI?

感谢。

推荐答案

有两件事情我想提一提。

There are two things I would like to mention.

在我的经验,我已经从FontMetrics.bottom减去FontMetrics.top计算像素字体高度。这是由于对底部和顶部按Y轴的正和负值。请参阅此Android文档。所以,我会改变你的calculateHeight方法如下:

In my experience I have calculated font height in pixels by subtracting FontMetrics.top from FontMetrics.bottom. This is due to the positive and negative values for bottom and top as per Y axis. Please see android documentation for this. So I would change your calculateHeight method as follows:

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

其次,你应该记住,你的determineTextSize方法将在返回像素的大小。如果您在使用此功能来设置一个TextView或某事的文字大小,那么你应该指定单位TypedValue.COMPLEX_UNIT_PX。此方法的默认单位为TypedValue.COMPLEX_UNIT_SP

Secondly you should remember that your determineTextSize method would return the size in pixels. If you are using this to set the Text Size of a TextView or something then you should specify the units as TypedValue.COMPLEX_UNIT_PX. The default unit for this method is TypedValue.COMPLEX_UNIT_SP

这篇关于在Android设备上运行的FontMetrics不正确。模拟器罚款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆