如何制作按钮弹出动画?

时间:2019-05-31 05:49:02

标签: java android animation

在我的TicTacToe游戏中,我希望获胜的三个字母弹出,然后恢复到正常大小以产生弹出效果。

我相信代码应该在方法winAnimation下的注释中。我尝试过一个for循环,以将字体大小增加1,但是直到for循环完成执行后,它才会显示在屏幕上。

public class button1 extends AppCompatActivity implements View.OnClickListener {
private Button[][] buttons = new Button[3][3];
private boolean player1Turn = true;
private int roundCount = 0;
private int player1Points = 0;
private int player2Points = 0;
private int winCondition;
/*winCondition
    0 = first row
    1 = second row
    2 = third row
    3 = first column
    4 = second column
    5 = third column
    6 = top left to bottom right diagonal
    7 = top right to bottom left diagonal
        */

private TextView textViewPlayer1;
private TextView textViewPlayer2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_button1);
    textViewPlayer1 = findViewById(R.id.player1__number_score);
    textViewPlayer2 = findViewById(R.id.player2__number_score);

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            String buttonID = "button_" + i + j;
            int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
            buttons[i][j] = findViewById(resID);
            buttons[i][j].setOnClickListener(this);
        }
    }
}


@Override
public void onClick(View view) {
    if (!((Button) view).getText().toString().equals("")) {
        return;
    }
    if (player1Turn) {
        ((Button) view).setText("X");
    } else {
        ((Button) view).setText("O");

    }
    roundCount++;
    if (checkForWin()) {
        if (player1Turn) {
            player1Wins();
        } else {
            player2Wins();
        }
    } else if (roundCount == 9) {
        draw();
    } else {
        player1Turn = !player1Turn;
    }

}

private boolean checkForWin() {
    String[][] check = new String[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            check[i][j] = buttons[i][j].getText().toString();

        }
    }
    //Checks rows
    for (int i = 0; i < 3; i++) {
        if (check[i][0].equals(check[i][1]) && check[i][0].equals(check[i][2]) && !check[i][0].equals("")) {
            if (i == 0) {
                winCondition = 0;
            }
            if (i == 1) {
                winCondition = 1;
            }
            if (i == 2) {
                winCondition = 2;
            }
            return true;
        }

    }
    //Check columns
    for (int i = 0; i < 3; i++) {
        if (check[0][i].equals(check[1][i]) && check[0][i].equals(check[2][i]) && !check[0][i].equals("")) {
            if (i == 0) {
                winCondition = 3;
            }
            if (i == 1) {
                winCondition = 4;
            }
            if (i == 2) {
                winCondition = 5;
            }
            return true;
        }

    }
    if (check[0][0].equals(check[1][1]) && check[0][0].equals(check[2][2]) && !check[0][0].equals("")) {
        winCondition = 6;
        return true;
    }
    if (check[0][2].equals(check[1][1]) && check[0][2].equals(check[2][0]) && !check[0][2].equals("")) {
        winCondition = 7;
        return true;
    }
    return false;
}

private void player1Wins() {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            buttons[i][j].setEnabled(false);
        }
    }
    player1Points++;
    textViewPlayer1.setText(Integer.toString(player1Points));
    Toast.makeText(this, "Player 1 has won", Toast.LENGTH_SHORT).show();
    final Handler handler = new Handler();
    final Runnable r = new Runnable() {
        public void run() {
            resetBoard();
        }
    };
    handler.postDelayed(r, 500);
    winAnimation(winCondition);
}





private void player2Wins(){
    for (int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            buttons[i][j].setEnabled(false);
        }
    }
    player2Points++;
    textViewPlayer2.setText(Integer.toString(player2Points));
    Toast.makeText(this,"Player 2 has won",Toast.LENGTH_SHORT).show();
    final Handler handler = new Handler();
    final Runnable r = new Runnable(){
        public void run(){
            resetBoard();
        }
    };
    handler.postDelayed(r,500);


}
private void resetBoard(){
    for(int i=0;i<3;i++){
        for(int j=0; j<3; j++){
            buttons[i][j].setText("");
            buttons[i][j].setEnabled(true);
        }
    }
    player1Turn=!player1Turn;
    roundCount =0;
}
private void draw(){
    for (int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            buttons[i][j].setEnabled(false);
        }
    }
    Toast.makeText(this,"Draw",Toast.LENGTH_SHORT).show();
    final Handler handler = new Handler();
    final Runnable r = new Runnable(){
        public void run(){
            resetBoard();
        }
    };
    handler.postDelayed(r,500);



}

private void winAnimation(int winCondition) {

    if (winCondition < 3) {
        for (int i = 0; i < 3; i++) {
            String buttonID = "button_" + winCondition + i;
            int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
            Button btn = findViewById(resID);
            //some button animation code here
            }
        }
    }
}

0 个答案:

没有答案