Android Paint.setTypeface 不适用于斜体
Paint.setTypeface 不适用于斜体,或者我做错了事情。我可以创建普通、粗体、等宽和衬线文本,但无法创建斜体文本。它看起来总是正常的(或者在粗斜体的情况下,它看起来是粗体)。
//This will appear monospace
paint.setTypeface(Typeface.MONOSPACE);
canvas.drawText("foo", 10, 10, paint);
//This will appear serif
paint.setTypeface(Typeface.SERIF);
canvas.drawText("foo", 10, 10, paint);
//This will appear bold
paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
canvas.drawText("foo", 10, 10, paint);
//This will NOT appear italic <=== PROBLEM
paint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
canvas.drawText("foo", 10, 10, paint);
// This isn't working either <=== PROBLEM
paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC));
现在的问题是:是否有已知的解决方法?我的简单目标是用斜体风格绘制一些单词......
The Paint.setTypeface is not working for italic or I'm doing something the wrong way. I can create normal, bold, monospace, and serif text, but I can't create italic text. It always looks normal (or in the case of bold-italic, it looks bold).
//This will appear monospace
paint.setTypeface(Typeface.MONOSPACE);
canvas.drawText("foo", 10, 10, paint);
//This will appear serif
paint.setTypeface(Typeface.SERIF);
canvas.drawText("foo", 10, 10, paint);
//This will appear bold
paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
canvas.drawText("foo", 10, 10, paint);
//This will NOT appear italic <=== PROBLEM
paint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
canvas.drawText("foo", 10, 10, paint);
// This isn't working either <=== PROBLEM
paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC));
So now the question: is there a known workaround for this? My simple goal is to draw some words with italic style...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
遇到同样的困难后,我在
TextView
源代码中摸索找到了解决方案。试试这个:After experiencing the same difficulty, I found the solution by fishing around in the
TextView
source code. Try this:我有同样的问题。看起来并不是所有的 android 字体都支持 ITALIC 风格。尝试以下操作,这对我有用:
仅与 SERIF 配合使用即可正常工作。 DEFAULT、MONOSPACE、SANS_SERIF 忽略此样式。
PS 我说的是 API 10。
I have the same problem. looks like not all android typefaces supports ITALIC style. Try following, i'ts worked for me:
Works fine just with SERIF. DEFAULT, MONOSPACE, SANS_SERIF ingnores this style.
P.S. I'm talking about API 10.
为了为不支持默认字体的设备获取斜体模式,我们应该使用 setTextSkewX 方法。然而,在应用它之前,我们必须确保不支持斜体模式。我们通过创建一个临时 TextView 对象并在两种模式(正常和斜体)下测量其宽度来实现它。如果它们的宽度相同,则表示不支持斜体模式。
请查看另一个问题中提出的解决方案:三星设备支持 setTypeface(Typeface.Italic)?
In order to get an italic mode for devices which don't support it for a default font, we should use setTextSkewX method. However, before applying it we have to be sure that an italic mode is not supported. We achieve it by creating a temporal TextView object and measuring its width in both modes (NORMAL and ITALIC). If their widths are same, then it means an ITALIC mode is NOT supported.
Please, take a look at a solution presented in another question: Samsung devices supporting setTypeface(Typeface.Italic)?