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

Posted

技术标签:

【中文标题】Nexus 7 上的 Android 4.2:canvas.drawText() 无法正常工作【英文标题】:Android 4.2 on Nexus 7: canvas.drawText() not working correctly 【发布时间】:2012-12-06 03:34:45 【问题描述】:

我的应用程序在 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" />

【问题讨论】:

实际上文本大小为 0.175f,我使用 backgroundCanvas.scale(getWidth(), getWidth()); 缩放画布; 【参考方案1】:

经过大量谷歌搜索后,我回答了自己的问题......

诀窍在于将 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 上)。请在此处查看我的另一个问题:

Android 4.2.1 wrong character kerning (spacing)

已编辑 (05/02/2013)

我看到另一个项目也有同样的问题。看下面的链接:

http://mindtherobot.com/blog/272/android-custom-ui-making-a-vintage-thermometer/

如果您在 Nexus 4.2.1(或模拟器 Android 4.2)上运行示例,您会得到相同的“奇怪”文本...

已编辑(2013 年 2 月 20 日)

找到了一个不使用setLinearText(true)的解决方法,看这里:

Android 4.2.1 wrong character kerning (spacing)

【讨论】:

谢谢谢谢谢谢!你把我从精神错乱中救了出来!它最终与 setLinearText(true) 一起工作。我希望他们能为开发人员更好地记录这些 API 更改。我花了 2 周时间才开始为 4.2.1 工作。咕噜…… 我很高兴我不是一个人遇到这个问题...如您所见,字符间距似乎是错误的(字符间距为 0)。你有同样的问题吗?我的另一个问题:***.com/questions/13974129/…【参考方案2】:

Android 4 默认开启硬件加速。启用此选项后,某些绘图功能无法正常工作。不记得具体是哪些,但请尝试在清单文件中关闭硬件加速,看看是否有影响。

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

【讨论】:

谢谢。不幸的是,标签 android:hardwareAccelerated 在 Android 3.0 以下无法识别,因此我无法将其用于我的应用程序(Android 2.3.3)... 如果您针对 Android 3 或 4 进行编译,则可以。在不支持它的版本上将被忽略。 (更改项目构建目标而不是您的清单文件) 好的,我用的是 Android SDK 3.2,重新编译了 android:hardwareAccelerated="false" 得到了同样的问题。 我使用 canvas.drawTextOnPath() 绘制弯曲的文本和在 android 4.0 设备中未显示的文本,然后将硬件加速设置为 false,一切正常,非常感谢 @Kuffs【参考方案3】:

我遇到了类似的问题,试图用自定义字母间距制作视图,所以我只做了这两种方法,希望有人觉得它们有帮助。

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

【讨论】:

以上是关于Nexus 7 上的 Android 4.2:canvas.drawText() 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

HttpPost:InputDispatcher:Nexus 7上的“频道无法恢复,将被丢弃!”

防止应用程序显示在 android kitkat 4.4.2 上的最近应用程序列表中

VideoView 在 Fragment 中播放 .m3u8 不会在 Nexus 7 中播放。在 HTC1 M8 中播放正常

Nexus设备上的Android相机捕获活动错误

我的应用与 Google Playstore 上的 Nexus 7 不兼容

Google Nexus 5x Android 7.0 Root