Nexus 7上的Android 4.2:canvas.drawText()无法正常工作

7
我遇到了一个严重的问题,我的应用程序发布在Google Play上,在除4.0以上版本之外的所有Android版本中都能正常工作。

这是我在Android 4.0 HTC手机上的截图:

enter image description here

而在Nexus 7, Android 4.2.1(模拟器中表现相同)上,我得到了以下结果:

enter image description here

每个使用canvas.drawText()绘制的文本都会出现相同的问题。

用于绘制文本的画笔是:

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" />

实际文本大小为0.175f,我使用backgroundCanvas.scale(getWidth(), getWidth())对画布进行缩放。 - Seraphim's
3个回答

14

在大量搜索后,我回答了自己的问题...

诀窍在于在绘制文本时使用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

希望它能帮助其他人。

文本还没有完全渲染出来:

enter image description here

编辑(14/01/2013)

我仍然遇到一个严重问题(仅在4.2.1上)。请查看我的另一个问题:

Android 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.1错误字符字距(间距)


谢谢,谢谢,谢谢!你让我免于疯狂!使用setLinearText(true)终于可以工作了。我希望他们能更好地为开发人员记录这些API更改。这花了我两个星期才在4.2.1上运行。真气人... - ssuperz28
我很高兴我不是唯一遇到这个问题的人...正如你所看到的,字符字距似乎有误(字符间距为0)。你有同样的问题吗?我的另一个问题在这里:https://dev59.com/mmYr5IYBdhLWcg3wE2Sz - Seraphim's

1

我曾经遇到过类似的问题,试图创建一个具有自定义字母间距的视图,所以我创造了这两种方法,希望对某些人有用。

/**
 * 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 );
}

1

Android 4默认启用硬件加速。其中一些绘图功能在此模式下无法正常工作。无法确切记住哪些功能,但可以尝试在清单文件中关闭硬件加速并查看是否有所不同。

当然,这可能不是原因,但值得一试。


谢谢。不幸的是,在Android 3.0以下版本中不被识别的标签android:hardwareAccelerated,因此我无法在我的应用程序(Android 2.3.3)中使用它... - Seraphim's
如果你编译针对Android 3或4,那么你可以这样做。在不支持它的版本上将被忽略。(更改项目构建目标而不是清单文件) - Kuffs
好的,我使用了Android SDK 3.2,android:hardwareAccelerated="false"重新编译后仍然出现相同的问题。 - Seraphim's
我使用canvas.drawTextOnPath()来绘制弯曲的文本,但在Android 4.0设备上文本未显示,然后将硬件加速设置为false,一切都正常工作了。非常感谢@Kuffs。 - Farzad Farazmand

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接