Android中的自定义字体在不同的API中呈现不同的方式

时间:2022-04-02 03:52:23

I am using a custom .ttf font in my android app. I load it the usual way:

我在我的Android应用程序中使用自定义.ttf字体。我按照通常的方式加载它:

myTypeface = Typeface.createFromAsset( getAssets(), "myTypeface.ttf");

then I assign my typeface within my activity... pretty straightforward stuff:

然后我在我的活动中分配我的字体...非常简单的东西:

TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setTextSize(12);
tv.setTypeface(App.myTypeface);

The problem I'm running into is that on some devices using later APIs (I've specifically noticed it in an emulator for the Asus Transformer), the text looks slightly bolder, less uniform in width, and more jumbled in vertical alignment. By that last part I mean that some characters are placed vertically a bit higher or lower than others, giving a little bit of a roller-coaster feel to the text.

我遇到的问题是在某些使用后期API的设备上(我在Asus Transformer的模拟器中特别注意到它),文本看起来稍微大一点,宽度不均匀,垂直对齐更混乱。在最后一部分,我的意思是某些角色垂直放置得比其他角色高一点或低一些,给文本带来一点过山车般的感觉。

Consider the screen shots below

考虑下面的屏幕截图

This is text rendered on an emulator with the same resolution and dpi as a Transformer, but using Google API level 8.

这是在仿真器上呈现的文本,具有与Transformer相同的分辨率和dpi,但使用的是Google API等级8。

Android中的自定义字体在不同的API中呈现不同的方式

Looks pretty standard, right?

看起来很标准,对吗?

Now consider the text rendered in an emulator with the same resolution and dpi, but using Google API level 15:

现在考虑在具有相同分辨率和dpi的模拟器中呈现的文本,但使用Google API等级15:

Android中的自定义字体在不同的API中呈现不同的方式

At first the text may look similar, though you might notice it seems a bit bolder. However, look at the "c" in "quick". You will notice that it sits lower, and is taller, than the "c" in the first rendering. You will also notice that if you look at the bottom of the characters in the word "quick", they are not aligned on the bottom.

起初文本可能看起来很相似,但你可能会注意到它看起来有点大胆。但是,请查看“快速”中的“c”。您会注意到它比第一次渲染中的“c”更低,并且更高。您还会注意到,如果您查看单词“quick”中字符的底部,它们不会在底部对齐。

These issues may seem small, but on screens with lots of text, it starts to look really unprofessional.

这些问题可能看起来很小,但在有大量文字的屏幕上,它看起来真的很不专业。

Anybody seen this, or have an explanation? I'd love to make the text look uniform in later APIs.

有谁看过这个,或者有解释?我希望在以后的API中使文本看起来统一。

Thanks so much for your time!

非常感谢你的时间!

1 个解决方案

#1


58  

Okay, so it seems to have just the following flags applied in both instances:

好的,所以它似乎只在以下两个实例中应用了以下标志:

Paint.DEV_KERN_TEXT_FLAG
Paint.ANTI_ALIAS_FLAG

Try doing this, and see if the results are any different (not necessarily improved, but even noticeable at all):

尝试这样做,看看结果是否有所不同(不一定改进,但甚至可以注意到):

textView.setPaintFlags(textView.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);

#1


58  

Okay, so it seems to have just the following flags applied in both instances:

好的,所以它似乎只在以下两个实例中应用了以下标志:

Paint.DEV_KERN_TEXT_FLAG
Paint.ANTI_ALIAS_FLAG

Try doing this, and see if the results are any different (not necessarily improved, but even noticeable at all):

尝试这样做,看看结果是否有所不同(不一定改进,但甚至可以注意到):

textView.setPaintFlags(textView.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);