Android Paint.setTypeface isn't working for 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开发者_如何学JAVA("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...
After experiencing the same difficulty, I found the solution by fishing around in theTextView
source code. Try this:
paint.setTextSkewX(-0.25f);
I have the same problem. looks like not all android typefaces supports ITALIC style. Try following, i'ts worked for me:
paint.setTypeface(Typeface.create(Typeface.SERIF,Typeface.ITALIC));
Works fine just with SERIF. DEFAULT, MONOSPACE, SANS_SERIF ingnores this style.
P.S. I'm talking about API 10.
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)?
精彩评论