如何停止动画?

时间:2017-03-25 17:17:20

标签: android

我有一个ObjectAnimator对象,似乎不能用end()或cancel()方法来阻止它,除非我把这些方法放在animation.start()方法之后,否则它们就不起作用了。我怎么能停止动画?如果(foo == 49)括号,则cancel()方法在else中。我也可以在哪里以及如何实例化动画对象,以便我也可以在cancel()方法中使用animation.finish()方法(或者结束动画所需的任何方法)。

public class GameScreen extends Activity {

private TextView time;
private ImageButton start;
private ImageButton gameButton;
private ImageButton button2;
private ImageButton hiddenBadGameButton;
private ImageButton hiddenGoodButton;
private ImageButton showTimeButton;
private CountDownTimer countDownTimer;
public static int count = 0;
public static int countPass = 0;
public String countPassString = "";
MyDBHandler dbHandler;

final Handler handler = new Handler();
final Runnable r = new Runnable() {
    public void run() {
        cancel();
    }
};




private View.OnClickListener btnClickListener = new View.OnClickListener(){

    @Override
    public void onClick(View v) {

        switch(v.getId()) {
            case R.id.start_ID:
                start();
                break;
            case R.id.gameButton_ID:
                gameButton();
                break;
            case R.id.button2_ID:
                button2();
                break;
            case R.id.hiddenBadGameButton_ID:
                hiddenBadGameButton();
                break;
            case R.id.hiddenGoodButton_ID:
                hiddenGoodButton();
                break;
            case R.id.timeButton_ID:
                hiddenTimeButton();
                break;
        }

    }


};


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


    start = (ImageButton) findViewById(R.id.start_ID);
    start.setOnClickListener(btnClickListener);
    time = (TextView) findViewById(R.id.time);
    gameButton = (ImageButton) findViewById(R.id.gameButton_ID);
    gameButton.setOnClickListener(btnClickListener);
    button2 = (ImageButton) findViewById(R.id.button2_ID);
    button2.setOnClickListener(btnClickListener);
    hiddenBadGameButton = (ImageButton) findViewById(R.id.hiddenBadGameButton_ID);
    hiddenBadGameButton.setOnClickListener(btnClickListener);
    hiddenGoodButton = (ImageButton) findViewById(R.id.hiddenGoodButton_ID);
    hiddenGoodButton.setOnClickListener(btnClickListener);
    showTimeButton = (ImageButton) findViewById(R.id.timeButton_ID);
    showTimeButton.setOnClickListener(btnClickListener);


    button2.setVisibility(View.GONE);
    hiddenBadGameButton.setVisibility(View.GONE);
    hiddenGoodButton.setVisibility(View.GONE);
    showTimeButton.setVisibility(View.GONE);


}

public void start(){

    count = 0;
    start.setVisibility(View.GONE);

    time.setText("100");
    //this doesnt work and makes app crash when you hit start button

    countDownTimer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millsUntilFinished){
            time.setText("" + millsUntilFinished / 1000);

            //turns textview string to int
            int foo = Integer.parseInt(time.getText().toString());

            ObjectAnimator animation = ObjectAnimator.ofFloat(gameButton, "rotationY", 0.0f, 360f);
            animation.setDuration(3600);
            animation.setRepeatCount(ObjectAnimator.INFINITE);
            animation.setInterpolator(new AccelerateDecelerateInterpolator());

            if(foo == 93) {
                //time.setVisibility(View.GONE);
                button2.setVisibility(View.VISIBLE);
            }
            else if(foo == 97) {
                gameButton.animate().translationX(200).setDuration(5000).start(); // move away
                hiddenBadGameButton.animate().translationX(200).setDuration(5000).start(); // move away
            }
            else if(foo == 90){
                gameButton.animate().translationY(200).setDuration(5000).start(); // move away
                hiddenBadGameButton.animate().translationY(200).setDuration(5000).start(); // move away
            }
            else if(foo == 87){
                gameButton.setVisibility(View.GONE);
                button2.setVisibility(View.GONE);
                hiddenBadGameButton.setVisibility(View.VISIBLE);
                hiddenGoodButton.setVisibility(View.VISIBLE);
            }
            else if(foo == 83){
                hiddenBadGameButton.setVisibility(View.GONE);
                hiddenGoodButton.setVisibility(View.GONE);
                gameButton.setVisibility(View.VISIBLE);
                button2.setVisibility(View.VISIBLE);
            }
            else if(foo == 82){
                time.setVisibility(View.GONE);
            }
            else if(foo == 80){
                showTimeButton.setVisibility(View.VISIBLE);
            }
            else if(foo == 78){
                showTimeButton.setVisibility(View.GONE);
            }
            else if(foo == 72){
                //time.setVisibility(View.GONE);
                button2.setVisibility(View.GONE);
            }
            else if(foo == 69){
                gameButton.animate().translationX(-50).setDuration(1000).start(); // move away
            }
            else if(foo == 68){
                gameButton.animate().translationY(-250).setDuration(1000).start(); // move away
            }
            else if(foo == 67){
                gameButton.animate().translationX(-100).setDuration(1000).start(); // move away
            }
            else if(foo == 66){
                gameButton.animate().translationY(-450).setDuration(1000).start(); // move away
            }
            else if(foo == 65){
                showTimeButton.setVisibility(View.VISIBLE);
            }
            else if(foo == 64){
                gameButton.animate().translationY(250).setDuration(2000).start(); // move away
                gameButton.animate().translationX(300).setDuration(1000).start(); // move away
            }
            else if(foo == 63){
                showTimeButton.setVisibility(View.GONE);
            }
            else if(foo == 58){
                //time.setVisibility(View.GONE);
            }
            else if(foo == 56){

                animation.start();
            }
            else if(foo == 49){
                gameButton.animate().translationY(400).setDuration(3000).start(); // move away
                showTimeButton.setVisibility(View.VISIBLE);
                //why can I not use the end method here
                animation.cancel();
            }

            if(foo  % 2 == 0){

                handler.postDelayed(r, 1000);

            }
        }

        public void onFinish() {
            start.setVisibility(View.VISIBLE);
            time.setVisibility(View.VISIBLE);
            time.setText("Done !");

            //need to check if score will reset if you win the game

            Toast.makeText(getApplicationContext(), "You scored " + count, Toast.LENGTH_LONG).show();
            gameButton.clearAnimation();
            gameButton.animate().translationX(0).setDuration(500).start(); //move back
            gameButton.animate().translationY(0).setDuration(500).start(); //move back
            button2.setVisibility(View.GONE);

            //holds score at end of game
            countPass = count;
            //reset score for new game
            count = 0;
            Toast.makeText(getApplicationContext(), "You scored " + countPass, Toast.LENGTH_LONG).show();
            countPassString = Integer.toString(countPass);

            int length = countPassString.length(); // length == 8
            if(length == 1){
                countPassString = "00".concat(countPassString);
            }
            else if(length == 2){
                countPassString = "0".concat(countPassString);
            }




            dbHandler = new  MyDBHandler(getApplicationContext(), null, null, 1);
            //mainActivity.productText.setText(countPassString);
            dbHandler.addButtonClicked(countPassString);
        }
    };
    countDownTimer.start();
}

private void cancel(){
    if(countDownTimer != null){
        countDownTimer.cancel();
        countDownTimer = null;
        start.setVisibility(View.VISIBLE);
        time.setVisibility(View.VISIBLE);

        showTimeButton.setVisibility(View.GONE);
        hiddenBadGameButton.setVisibility(View.GONE);
        hiddenGoodButton.setVisibility(View.GONE);
        gameButton.setVisibility(View.VISIBLE);
        gameButton.clearAnimation();
        gameButton.animate().translationX(0).setDuration(500).start(); //move back
        gameButton.animate().translationY(0).setDuration(500).start(); //move back
        hiddenBadGameButton.clearAnimation();
        hiddenBadGameButton.animate().translationX(0).setDuration(500).start(); //move back
        hiddenBadGameButton.animate().translationY(0).setDuration(500).start(); //move back


        //animation.cancel();

        button2.setVisibility(View.GONE);

        //holds score at end of game
        countPass = count;
        //reset score for new game
        count = 0;
        Toast.makeText(getApplicationContext(), "You scored " + countPass, Toast.LENGTH_LONG).show();
        countPassString = Integer.toString(countPass);

        int length = countPassString.length(); // length == 8
        if(length == 1){
            countPassString = "00".concat(countPassString);
        }
        else if(length == 2){
            countPassString = "0".concat(countPassString);
        }




        dbHandler = new MyDBHandler(this, null, null, 1);
        //mainActivity.productText.setText(countPassString);
        dbHandler.addButtonClicked(countPassString);
    }
}

private void gameButton(){
    int foo = Integer.parseInt(time.getText().toString());

    if(foo  % 2 == 0 ) {
        final Toast toast = Toast.makeText(getApplicationContext(), "+1", Toast.LENGTH_SHORT);
        toast.show();

        // makes +1 toast half a second
        Handler gameButtonToastHandler = new Handler();
        gameButtonToastHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, 500);

        handler.removeCallbacks(r);
        ++count;
    }
    else{
        cancel();
    }
}

private void hiddenGoodButton(){
    gameButton();
}

private void button2(){
    cancel();
}

private void hiddenBadGameButton(){
    cancel();
}

private void hiddenTimeButton(){
    int foo = Integer.parseInt(time.getText().toString());
        time.setVisibility(View.VISIBLE);
}

2 个答案:

答案 0 :(得分:2)

要停止动画,请使用以下代码:

object.clearAnimation();

你也可以调用anim.cancel();但你也应该调用anim.reset();紧接着它。然后,当您想再次启动它时,只需在视图上调用startAnimation。使用object.clearAnimation()停止动画。

希望这有帮助! :)

答案 1 :(得分:0)

我只是要删除 animation.setRepeatCount(ObjectAnimator.INFINITE); 我想它一定是重写了.cancel()方法。