暂停和恢复井字游戏获胜动画

Pausing and resuming tic tac toe win animation

我制作了井字游戏并在最后添加了一个动画...每次鞭炮动画从下到上播放时它都会被按钮重叠(附上屏幕截图)。我想知道是否有一种简单的方法可以在这里使用线程在使按钮可见之前停止进程,以便鞭炮动画可​​以轻松完成。

代码如下:

for (int[] winningPosition : winningPositions)
            {
                if (gameState[winningPosition[0]] == gameState[winningPosition[1]] && gameState[winningPosition[1]] == gameState[winningPosition[2]] && gameState[winningPosition[0]] != 2) {
                    // Someone has won!
                    gameActive = false;

                    String winner = "";

                    if (activePlayer == 0) {
                        winner = "O";
                    } else {
                        winner = "X";
                    }

                    Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
                    TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
                    winnerTextView.setText(winner + " has won!");

                    ImageView firecracker = (ImageView) findViewById(R.id.firecracker);
                    firecracker.setTranslationY(-1000);
                    firecracker.setVisibility(View.VISIBLE);
                    firecracker.animate().translationYBy(-3000).setDuration(1000);
                    firecracker.setTranslationY(4000);
                    playAgainButton.setVisibility(View.VISIBLE);
                    winnerTextView.setVisibility(View.VISIBLE);
                }
            }

重播按钮的点击方法:

public void reset(View view)
    {
        ImageView firecracker = (ImageView) findViewById(R.id.firecracker);
        firecracker.setVisibility(View.INVISIBLE);
        Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
        TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
        playAgainButton.setVisibility(View.INVISIBLE);
        winnerTextView.setVisibility(View.INVISIBLE);
}

完整代码:

import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.security.spec.InvalidKeySpecException;

public class MainActivity extends AppCompatActivity {

    // 0: X, 1: O, 2: Empty

    int [] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
    int [][] winningPositions = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0,3,6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
    boolean gameActive = true;



    int activePlayer = 0;


    public void dropIn(View view)
    {
        ImageView counter = (ImageView) view;


        int tappedCounter = Integer.parseInt(counter.getTag().toString());

        if (gameState[tappedCounter] == 2 && gameActive)
        {

            gameState[tappedCounter] = activePlayer;


            counter.setTranslationY(-2000);
            counter.setImageResource(R.drawable.o);

            counter.animate().translationYBy(2000).setDuration(300);

            if (activePlayer == 0) {
                counter.setImageResource(R.drawable.x);
                activePlayer = 1;
            } else {
                counter.setImageResource(R.drawable.o);
                activePlayer = 0;
            }


            boolean emptySquare = false;
            for (int squareState : gameState) {
                if (squareState == 2) {
                    emptySquare = true;
                    break;
                }
            }
            if (!emptySquare && gameActive) {
                // Game is a draw
                gameActive = true;
                Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
                TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
                winnerTextView.setText("Game is a draw...");
                playAgainButton.setVisibility(View.VISIBLE);
                winnerTextView.setVisibility(View.VISIBLE);
            }


            for (int[] winningPosition : winningPositions)
            {
                if (gameState[winningPosition[0]] == gameState[winningPosition[1]] && gameState[winningPosition[1]] == gameState[winningPosition[2]] && gameState[winningPosition[0]] != 2) {
                    // Someone has won!
                    gameActive = false;

                    String winner = "";

                    if (activePlayer == 0) {
                        winner = "O";
                    } else {
                        winner = "X";
                    }
                    Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
                    TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
                    winnerTextView.setText(winner + " has won!");

                    ImageView firecracker = (ImageView) findViewById(R.id.firecracker);
                    firecracker.setTranslationY(-1000);
                    firecracker.setVisibility(View.VISIBLE);
                    firecracker.animate().translationYBy(-3000).setDuration(1000);
                    firecracker.setTranslationY(4000);

                    playAgainButton.setVisibility(View.VISIBLE);
                    winnerTextView.setVisibility(View.VISIBLE);
                }
            }
        }
    }


    public void reset(View view)
    {
        ImageView firecracker = (ImageView) findViewById(R.id.firecracker);
        firecracker.setVisibility(View.INVISIBLE);
        Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
        TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
        playAgainButton.setVisibility(View.INVISIBLE);
        winnerTextView.setVisibility(View.INVISIBLE);

        android.support.v7.widget.GridLayout gridLayout = (android.support.v7.widget.GridLayout) findViewById(R.id.gridLayout);

        for (int i = 0; i < gridLayout.getChildCount(); i++)
        {
            ImageView counter = (ImageView) gridLayout.getChildAt(i);
            counter.setImageDrawable(null);
        }

        for (int i = 0; i < gameState.length; i++)
        {
            gameState[i] = 2;
        }

        activePlayer = 0;

        gameActive = true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

好吧,如果我没看错的话,我通常使用一种方法,那就是创建一个睡眠方法。代码如下:

static void Sleep(int ms)
{
  try {
    Thread.sleep(ms);
   }catch (InterruptedException e) {
    e.printStackTrace();
   }
 }

如果我没看错问题,我的解释是你想暂停线程一小段时间然后恢复它。

为了实现这个,只需在使按钮可见之前调用 Sleep({Time in milliseconds})