Reputation: 1482
I have an ObjectAnimator object and cant seem to stop it with the end() or cancel() methods unless I put these methods right after the animation.start() method otherwise they just don't work. How can I stop the animation? the cancel() method is inside else if(foo == 49) braces. Also where and how could I instantiate the animation object so that I can also use the animation.finish() method (or whatever is needed to end the animation) inside my cancel() method as well.
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);
}
Upvotes: 0
Views: 138
Reputation: 1482
I just had to delete animation.setRepeatCount(ObjectAnimator.INFINITE); I suppose it must have been overriding the .cancel() method.
Upvotes: 0
Reputation: 5069
To stop animation use the code below:
object.clearAnimation();
You can also call anim.cancel(); but you should also call anim.reset(); immediately after it. Then when you want to start it again, just call startAnimation on the view. Use object.clearAnimation()
to stop an animation.
Hope this helps ! :)
Upvotes: 2