0

我的代码有问题。当我尝试旋转文本时,一切正常,但我想恢复画布,所以我打电话canvas.restore();

当我这样做时,我的应用程序将立即关闭...

我的代码的一部分:

触摸屏幕的某一部分:

if (wahrheitswert1  == true) { 
    x = 480;
    y = 100;    

    // draw bounding rect before rotating text
    Rect rect = new Rect();
    canvas.translate(x, y);

    // undo the translate
    canvas.translate(-x, -y);
    // rotate the canvas on center of the text to draw
    canvas.rotate(-180, x + rect.exactCenterX(), y + rect.exactCenterY());
    // draw the rotated text
    canvas.drawText("Spieler1 touch", x, y, paint);
    //undo the rotate
    //canvas.restore();
    wahrheitswert1 = false;
    canvas.restore();
}

如果我不恢复位图,我的背景图像将从屏幕的其他站点复制。感谢您的帮助

4

2 回答 2

1

虽然我没有使用很多画布,但我看不到在恢复之前将上下文保存在哪里。我很确定要对您首先必须保存上下文的上下文进行恢复。

于 2011-09-13T22:29:46.807 回答
0

You need to call

Canvas.save()

before rotating the Canvas. You can restore the Canvas anytime by calling the Canvas.save(). I've modified your code below.

if (wahrheitswert1  == true) { 
    x = 480;
    y = 100;   

    canvas.save();

    // draw bounding rect before rotating text
    Rect rect = new Rect();
    canvas.translate(x, y);

    // undo the translate
    canvas.translate(-x, -y);
    // rotate the canvas on center of the text to draw
    canvas.rotate(-180, x + rect.exactCenterX(), y + rect.exactCenterY());
    // draw the rotated text
    canvas.drawText("Spieler1 touch", x, y, paint);
    //undo the rotate
    //canvas.restore();
    wahrheitswert1 = false;
    canvas.restore();
}

I also had the same problem and it worked for me.

于 2013-09-17T07:11:43.640 回答