我可以从 SurfaceView 切换活动吗?

Can I switch activities from a SurfaceView?

我正在尝试在 Android Studio 中改编贪吃蛇游戏,以便游戏结束时触发 GameOverActivity。问题是游戏结束文本当前在 redrawCanvas() 函数中写入屏幕:

public void redrawCanvas(Canvas canvas) {
        if (this.snake.isEnabled()) {
            this.snake.draw(canvas);
            this.apple.draw(canvas);
            this.score.draw(canvas);
        } else {
            Paint p = getPaint();
            p.setTextSize(50);
            p.setColor(Color.BLACK);
            canvas.drawText("Game over!", 100, 100, p);

            // LAUNCH PLAY AGAIN ACTIVITY HERE?
        }
    }

并且此 redrawCanvas 代码包含在 class 扩展 SurfaceView 中。我通常会调用一个意图,但我不能从 SurfaceView class 中调用 startActivity()。我的 SnakeGameActivity 只是设置了内容视图,所有代码都发生在这个表面视图中。这是 SnakeGameActivity 中的代码:

public class SnakeGameActivity extends com.codepath.simplegame.GameActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        
        //before activity is created : cold start
        //switch back to original Theme (App Theme)
        setTheme(R.style.AppTheme);

        switchFullscreen();
        setContentView(new SnakeGamePanel(this));
    }
}

下面是 SnakeGamePanel 中的完整代码:

public class SnakeGamePanel extends AbstractGamePanel {

    public SnakeGamePanel(Context context) {
        super(context);
    }

    private SnakeActor snake;
    private AppleActor apple ;
    private ScoreBoard score;
    private boolean isPaused = false;

    @Override
    public void onStart() {
        this.snake = new SnakeActor(100, 100);
        this.apple = new AppleActor(200, 50);
        this.score = new ScoreBoard(this);
    }

    @Override
    public void onTimer() {
        if (!isPaused) {
            if (this.snake.checkBoundsCollision(this)) {
                this.snake.setEnabled(false);
            }
            this.snake.move();
            if (this.apple.intersect(this.snake)) {
                this.snake.grow();
                this.score.earnPoints(50);
                this.apple.reposition(this);
            }
        }
    }

    @Override
    public void redrawCanvas(Canvas canvas) {
        if (this.snake.isEnabled()) {
            this.snake.draw(canvas);
            this.apple.draw(canvas);
            this.score.draw(canvas);
        } else {
            Paint p = getPaint();
            p.setTextSize(50);
            p.setColor(Color.BLACK);
            canvas.drawText("Game over!", 100, 100, p);

            // LAUNCH PLAY AGAIN ACTIVITY?
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        this.snake.handleKeyInput(keyCode);
        if (keyCode == KeyEvent.KEYCODE_G) {
            this.onStart();
        }
        if (keyCode == KeyEvent.KEYCODE_P) {
            isPaused = !isPaused;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            this.snake.handleTouchInput(event);
            return true;
        }
        return false;
    }

}

有什么方法可以在游戏结束时开始activity吗?

到目前为止,我找到了似乎是解决方案。我刚刚将 SurfaceView 扩展 class 设为 activity 中的内部 class。这样,它就有一个 activity 作为包含 class.

内部 class 解决方案的一个替代方法是将 Context 对象传递给 SurfaceView 子class 的构造函数。然后您可以在该上下文中调用 startActivity()