Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nexus7上的Android 4.2:canvas.drawText()无法正常工作_Android_Layout_User Interface_Drawtext_Android 4.2 Jelly Bean - Fatal编程技术网

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

Nexus7上的Android 4.2:canvas.drawText()无法正常工作,android,layout,user-interface,drawtext,android-4.2-jelly-bean,Android,Layout,User Interface,Drawtext,Android 4.2 Jelly Bean,我面临着一个严重的问题,我的应用程序发布在Google Play上,除了>4.0之外,在所有版本的Android上都运行良好 这是我的Android 4.0 HTC手机的屏幕截图: 这就是我在Nexus7、Android 4.2.1(仿真器中的相同行为)上得到的结果: 我看到使用canvas.drawText() 用于绘制文本的油漆为: paint = new Paint(); paint.setAntiAlias(true); paint.setColor(color); //some c

我面临着一个严重的问题,我的应用程序发布在Google Play上,除了>4.0之外,在所有版本的Android上都运行良好

这是我的Android 4.0 HTC手机的屏幕截图:

这就是我在Nexus7、Android 4.2.1(仿真器中的相同行为)上得到的结果:

我看到使用
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" />

Android 4默认为硬件加速。启用此选项时,某些绘图功能无法正常工作。记不清具体是哪一个,但请尝试在清单文件中关闭硬件加速,看看这是否会有所不同


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

在谷歌搜索了很多次之后,我回答了自己的问题

技巧在于对用于绘制文本的绘制对象使用
setLinearText(true)
。现在,一切看起来都很好

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);
下面是拯救我的一天的链接:

我希望它能帮助别人

文本的渲染效果最好:

编辑(14/01/2013)

我仍然面临着一个刻蚀问题(仅在4.2.1中)。请看我的另一个问题:

编辑(2013年2月5日)

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

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

编辑(20/02/2013)

找到一个不使用setLinearText(true)的解决方法,请查看此处:


我也遇到了类似的问题,试图用自定义字母间距创建视图,所以我只使用了这两种方法,希望有人能对我有所帮助

/**
 * 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 );
}
/**
*在画布中以每个字母之间的间距绘制文本。
*基本上,这个方法的作用是将给定的文本拆分成单独的字母
*并使用Canvas.drawText单独绘制每个字母,分隔为
*每个字母之间的{@code spacingX}。
*@param canvas将绘制文本的画布
*@param text将绘制的文本
*@param left文本的左侧位置
*@param top文本的顶部位置
*@param paint保存文本的样式信息
*@param spacingPx将绘制的每个字母之间的像素数
*/
公共静态void drawSpacedText(画布、字符串文本、左侧浮动、顶部浮动、绘制、浮动间距gpx){
浮动电流左=左;
对于(int i=0;i
实际上文本大小是0.175f,我使用backgroundCanvas.scale(getWidth(),getWidth())缩放画布;谢谢不幸的是,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,一切正常,非常感谢@KuffsThank谢谢,谢谢,谢谢!你救了我的命!它最终与setLinearText(true)一起工作。我希望他们能更好地为开发人员记录这些API更改。我花了两个星期才开始为4.2.1工作。Grrr…我很高兴我不是唯一一个处理这个问题的人。。。如您所见,字符紧排似乎是错误的(字符间距为0)。你也有同样的问题吗?我的另一个问题是: