简体   繁体   English

Android-如何在onDraw(Canvas)内正确调用intent(activity)?

[英]Android - How to call an intent(activity) inside the onDraw(Canvas) properly?

I'm trying to call an intent inside the canvas but it doesn't seem to work the way I thought it should be. 我正在尝试在画布内调用一个意图,但它似乎并没有按照我认为的方式工作。 What I have in mind is when it passes through the startactivity() , then that's it, it exits the canvas right away but I was wrong. 我要记住的是,当它通过startactivity() ,就是这样,它立即退出了画布,但是我错了。

Flow should be: 流量应为:

Menu -> Pong(Activity) -> Canvas -> Game_Over

What's happening: 发生了什么:

Menu -> Pong(Activity) -> Canvas(screen stuck here) -> Game_Over(iterates unknown times)

Sometimes it crashes and won't proceed to Game_Over. 有时会崩溃,并且无法继续进行Game_Over。

Inside my onDraw() is the code below when the Game Over condition is met: 游戏结束条件满足时,在我的onDraw()内部是以下代码:

Intent i = new Intent(getContext(), GameOver.class);
i.putExtra("score", Integer.toString(ball.getScore()));
i.putExtra("level", Integer.toString(ball.getLevel()));
getContext().startActivity(i);

From what I've observed after making the scores, it seems to be looping for like 20+ times since the toast for the "High Score" found in the next activity won't stop and the screen is stuck at the canvas until the loop stops. 从我记分之后的观察来看,它似乎循环了20多次,因为在下一个活动中发现的“高分”吐司并不会停止并且屏幕一直停留在画布上,直到循环为止停止。 This gave me a clue that the activity to be called is looping countless times. 这给了我一个线索,即将被调用的活动循环了无数次。

Who's at fault here? 这是谁的错 Is it the onDraw() for redrawing even though it started the startActivity() already? 即使它已经启动了startActivity()它仍然是用于重绘的onDraw()吗? If yes, how do I stop onDraw() from looping before it reaches intent so it won't loop the next activity? 如果是,如何在onDraw()达到意图之前停止onDraw()的循环,以使其不会循环下一个活动?

Here's my onDraw() : 这是我的onDraw()

public void onDraw(Canvas canvas) {
    // Background
    canvas.drawColor(Color.DKGRAY);

    // Score
    canvas.drawText("Score: " + Integer.toString(ball.getScore()), 25, 50,
            paint);

    // Level
    canvas.drawText("Level: " + Integer.toString(ball.getLevel()), 25, 100,
            paint);

    // =========================
    // Draw Paddle
    // paint.setColor(Color.parseColor("#AA00FFFF"));
    paint.setColor(Color.argb(170, 0, 255, 255));
    paint.setStrokeWidth(20);
    canvas.drawLine(padX, padY, padX + padWidth, padY, paint);

    // Draw Ball
    canvas.drawCircle(ball.getX(), ball.getY(), BALL_RADIUS, paint);

    if (ball.getScore() % 200 == 0) {
        // counter = counter + 1;
        if (ball.getLevel() < MAX_LEVEL) {
            ball.setSpeedX(ball.getSpeedX() * 0.25f);
            ball.setSpeedY(ball.getSpeedY() * 0.25f);
        }
    }

    postInvalidate();

    if (ball.getY() + (float) BALL_RADIUS >= y - 6) {
        if (ball.getX() + BALL_RADIUS / 2 > padX
                && ball.getX() - BALL_RADIUS / 2 < padX + padWidth) {
            ball.setGameOver(false);
        }

        else {
            ball.setGameOver(true);
            canvas.drawText("Game Over! Please Wait.", x / 2 - 170, y / 3,
                    paint);

            Intent i = new Intent(getContext(), GameOver.class);
            i.putExtra("score", Integer.toString(ball.getScore()));
            i.putExtra("level", Integer.toString(ball.getLevel()));
            getContext().startActivity(i);
        }

    }
}

onDraw can be called 60 times a second, it should just draw, not handle activity flow. onDraw可以每秒调用60次,它应该只是绘制,而不是处理活动流。 The minimal change is to add a boolean and make sure you call startActivity only once. 最小的更改是添加一个布尔值,并确保仅调用一次startActivity。 A better solution is to move the activity managemen out of the drawing, and into a state management logic 更好的解决方案是将活动管理器从工程图中移出,并移入状态管理逻辑中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM