2017-03-25 13 views
0

Ich habe ein ObjectAnimator-Objekt und kann es anscheinend nicht mit den Methoden end() oder cancel() stoppen, es sei denn, ich setze diese Methoden direkt nach der animation.start() -Methode, sonst funktionieren sie einfach nicht. Wie kann ich die Animation stoppen? Die Methode cancel() befindet sich innerhalb von if (foo == 49) geschweiften Klammern. Auch wo und wie könnte ich das Animationsobjekt instanziieren, so dass ich auch die animation.finish() -Methode (oder was auch immer benötigt wird, um die Animation zu beenden) innerhalb meiner cancel() -Methode ebenfalls verwenden kann.Wie man Animation stoppt?

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); 
} 

Antwort

0

Ich musste nur löschen animation.setRepeatCount (ObjectAnimator.INFINITE); Ich nehme an, dass es die .cancel() Methode überschrieben haben muss.

1

Animation den Code unten verwenden zu beenden:

object.clearAnimation(); 

Sie können auch anim.cancel nennen(); aber Sie sollten auch anim.reset() aufrufen; unmittelbar danach. Wenn Sie es dann erneut starten möchten, rufen Sie startAnimation in der Ansicht auf. Verwenden Sie object.clearAnimation(), um eine Animation zu stoppen.

Hoffe, das hilft! :)