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

2023-09-04 23:51:49 作者:执笔画浮尘

我使用的是自定义的.ttf字体在我的Andr​​oid应用程序。我加载它通常的方式:

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

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

然后,我在我的活动... pretty的简单的东西赋予我的字体:

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);

我遇到的问题是使用后的API(我特别注意到它在仿真器华硕变压器)某些设备,文字看起来稍微大胆,宽欠均匀,并在垂直方向更混乱对准。通过这最后一部分,我的意思是,一些字符垂直放置高一点或比别人低,给人一种过山车般的一点点感觉的文字。

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.

考虑下面

这是文本渲染与作为变压器相同的分辨率和dpi的仿真器,但使用谷歌API级别8。

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

看起来pretty的标准,对吧?

Looks pretty standard, right?

现在考虑呈现在模拟器具有相同的分辨率和DPI文本,但使用谷歌API级别15:

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

起初,文字可能看起来相似,但你可能会注意到它似乎有点大胆。不过,看在快中的C。你会发现,它位于较低的,而且是高,比第一渲染的C。你也会发现,如果你的字快的人物的底部,他们不是在底部对齐。

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.

这些问题看起来很小,但有很多文字的屏幕,它开始看起来很专业。

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

任何人看到了这一点,还是有一个解释?我很想让文字看起来均匀后的API。

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

非常感谢您的时间!

推荐答案

好了,所以它似乎刚才在这两种情况下使用了以下标志:

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);
 
精彩推荐
图片推荐