在 Android 中使用自定义 TTF 会导致文本变得非常难看
我有一个包含自定义视图的应用程序,我想在其中使用自定义字体。不幸的是,这会导致渲染出非常难看的文本。
我的自定义视图扩展了 Surface(刚刚意识到:这是一个坏主意),我使用以下代码绘制文本:
// p = new Paint();
Typeface font = Typeface.createFromAsset(parent.getAssets(), "komtit.ttf");
p.setColor(Color.BLACK);
p.setTypeface(font);
c.drawText(this.text, x + width / 2 - p.measureText(this.text) / 2, y + height / 2, p);
结果如下所示:
http://img.skitch.com/20101014-rxw8j8igj1jci2fx9ui32ejcp.jpg
不好玩。我正在使用 p.setFlags(Paint.ANTI_ALIAS_FLAG) 但即使这样,结果也是模糊的。
那么,有没有办法改进自定义 TTF 渲染,或者我应该坚持使用系统字体?
I have an app that contains a custom view, and in it I want to use a custom font. Unfortunately, this results in really ugly text being rendered.
My custom view extends Surface (and just realized: is this a bad idea), and I draw the text with the following code:
// p = new Paint();
Typeface font = Typeface.createFromAsset(parent.getAssets(), "komtit.ttf");
p.setColor(Color.BLACK);
p.setTypeface(font);
c.drawText(this.text, x + width / 2 - p.measureText(this.text) / 2, y + height / 2, p);
The result looks like this:
http://img.skitch.com/20101014-rxw8j8igj1jci2fx9ui32ejcp.jpg
Not fun. I'm using p.setFlags(Paint.ANTI_ALIAS_FLAG) but even with that the result is just fudgly.
So, is there a way to improve custom TTF rendering, or should I just stick to the system fonts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里回答我自己的问题。问题是我正在扩展 Surface(我最终确实没有适当的理由),它显然使用 OpenGL 进行渲染,并且 OpenGL 和自定义 TTF 字体不混合。
当我切换到仅扩展视图时,字体看起来很完美。
Answering my own question here. The problem was that I was extending Surface (which I really in the end didn't have a proper reason for) which apparently uses OpenGL for rendering and OpenGL and custom TTF fonts don't mix.
When I switched to extending just View, the font looks perfect.