[英]Android 4.2 on Nexus 7: canvas.drawText() not working correctly
我的应用程序面临严重问题,在Google Play上发布,并且除了> 4.0之外,显然在所有Android版本上都能正常运行。
这是我的Android 4.0 HTC手机的截图:
这就是我在Nexus 7,Android 4.2.1(模拟器中的相同行为)上得到的结果:
我看到使用canvas.drawText()
绘制的每个文本都有相同的行为
用于绘制文本的Paint是:
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(color); //some color
paint.setTextSize(size); //some size
paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
paint.setTextAlign(Align.CENTER);
在logCat(4.2.1模拟器)中,我看到了很多
12-18 20:42:21.096: W/Trace(276): Unexpected value from nativeGetEnabledTags: 0
我在清单中使用这些设置:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
经过大量的谷歌搜索,我回答了自己的问题......
诀窍在于使用setLinearText(true)
作为用于绘制文本的Paint对象。 现在,一切看起来都很棒。
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(color);
paint.setTextSize(size);
paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
paint.setTextAlign(Align.CENTER);
paint.setLinearText(true);
这里是节省我一天的链接:
http://gc.codehum.com/p/android/issues/detail?id=39755
我希望它可以帮助一些人。
该文本尚未最佳呈现:
编辑(14/01/2013)
我仍然面临着一个kering问题(仅限于4.2.1)。 请在此处查看我的其他问题:
编辑(05/02/2013)
我看到另一个项目有同样的问题。 请看下面的链接:
http://mindtherobot.com/blog/272/android-custom-ui-making-a-vintage-thermometer/
如果您在Nexus 4.2.1(或模拟器Android 4.2)上运行示例,您将获得相同的“奇怪”文本...
编辑(20/02/2013)
找到一个不使用setLinearText(true)
的解决方法,请看这里:
Android 4默认为硬件加速开启。 使用此功能时,某些绘图功能无法正常工作。 不能完全记住哪些,但尝试在清单文件中关闭硬件加速,看看它是否有所作为。
当然这可能不是原因,但值得一试。
我有一个类似的问题,试图用自定义字母间距制作一个视图所以我只是做了这2个方法,希望有人发现它们有用。
/**
* Draws a text in the canvas with spacing between each letter.
* Basically what this method does is it split's the given text into individual letters
* and draws each letter independently using Canvas.drawText with a separation of
* {@code spacingX} between each letter.
* @param canvas the canvas where the text will be drawn
* @param text the text what will be drawn
* @param left the left position of the text
* @param top the top position of the text
* @param paint holds styling information for the text
* @param spacingPx the number of pixels between each letter that will be drawn
*/
public static void drawSpacedText(Canvas canvas, String text, float left, float top, Paint paint, float spacingPx){
float currentLeft = left;
for (int i = 0; i < text.length(); i++) {
String c = text.charAt(i)+"";
canvas.drawText(c, currentLeft, top, paint);
currentLeft += spacingPx;
currentLeft += paint.measureText(c);
}
}
/**
* returns the width of a text drawn by drawSpacedText
*/
public static float getSpacedTextWidth(Paint paint, String text, float spacingX){
return paint.measureText(text) + spacingX * ( text.length() - 1 );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.