Android DrawText无法在SurfaceView上运行

时间:2014-12-19 02:58:40

标签: java android eclipse graphics

正在向我的画布添加Text但是canvas.drawText(...)方法不起作用(不渲染任何东西),尽管其他绘制调用工作完全正常。例如,我在画布上绘制线条/位图,但绘图文字仍然失败。

订单:MainActivity - > GameActivity - > GameThread:Thread - > GamePanel:SurfaceView

代码:     public void render(Canvas canvas){         canvas.drawColor(Color.BLACK);

    //draws the vector line!
    if(this.PAUSED == 2)
        this.drawLine(canvas);
    playerOne.render(canvas);
    for(int a=0; a < GameConstants.floatingStructures.size(); a++)
    {
        GameConstants.floatingStructures.get(a).render(canvas);//renders each item to the canvas
    }

    Paint textPaint =  new Paint(Color.RED);
    textPaint.setTextSize(16);
    textPaint.setStrokeWidth(30);
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setStyle(Style.FILL);
    canvas.drawText("HelloWorld", 0, 400,textPaint);
}

感谢任何帮助!

P.S。我也尝试过这段代码:

    Paint textPaint =  new Paint(Color.RED);
    canvas.drawText("HelloWorld", 0, 400,textPaint);

截图: enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您正在使用的Paint构造函数中的参数 - 即Paint(int) - 用于标记,例如ANTI_ALIAS_FLAG,而不是颜色值。更改实例化和初始化,如下所示:

Paint textPaint = new Paint();
textPaint.setColor(Color.RED);