Android 中的自定义字体在不同的 API 中呈现不同 [英] Custom Font in Android renders differently in different APIs

查看:29
本文介绍了Android 中的自定义字体在不同的 API 中呈现不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我遇到的问题是,在某些使用较新 API 的设备上(我在 Asus Transformer 的模拟器中特别注意到了这一点),文本看起来更粗,宽度不均匀,垂直方向更混乱结盟.最后一部分我的意思是有些字符在垂直方向上比其他字符高一点或低一点,给文本带来一点过山车的感觉.

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.

考虑下面的屏幕截图

这是在模拟器上呈现的文本,具有与 Transformer 相同的分辨率和 dpi,但使用的是 Google API 级别 8.

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

看起来很标准,对吧?

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

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

起初文字可能看起来很相似,但您可能会注意到它看起来更粗一些.但是,请查看快速"中的c".您会注意到它比第一次渲染中的c"更低,更高.您还会注意到,如果您查看quick"一词中字符的底部,它们并没有在底部对齐.

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

这篇关于Android 中的自定义字体在不同的 API 中呈现不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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