2011-09-13 69 views
0

我的代碼有問題。當我試圖旋轉一切正常的文字,但我想這樣恢復的畫布,所以我打電話canvas.restore();在android上開發遊戲:在畫布中旋轉文字

當我這樣做,我的應用程序將立即關閉...

我的代碼的一部分:

觸摸屏幕的一個部分:

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(); 
} 

如果我不恢復的位圖,我有背景圖像將從屏幕的其他站點進行復制。 感謝您的幫助

回答

1

儘管我沒有使用過很多畫布,但在恢復之前我沒有看到您保存上下文的位置。我非常確定要對您首先必須保存上下文的上下文進行恢復。

+0

是真的,沒有保存就沒有什麼可恢復的...... – WarrenFaith

+0

請問我能做些什麼? 我是新編程的一個不是java專家 – kmartinho

+0

看看http://developer.android.com/reference/android/graphics/Canvas.html 注意恢復方法提到你必須以前有一個保存( )。基本上在你想恢復的時間點上,放上canvas.save(),所以當恢復被調用時,恢復到保存點。 – Contristo

0

你需要旋轉畫布之前調用

Canvas.save() 

。您可以通過調用Canvas.save()來隨時恢復Canvas。 我修改了下面的代碼。

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(); 
} 

我也有同樣的問題,它爲我工作。