I am using object animator to create a blink effect for my buttons. Everything works fine. Except that I couldnt stop the animation. Is it a bug or am I missing somthing. I have the following methods.
public void manageBlinkEffect(View view){
objectAnimator = ObjectAnimator.ofInt(view, "backgroundColor", Color.GRAY, Color.YELLOW);
objectAnimator.setDuration(1000);
objectAnimator.setEvaluator(new ArgbEvaluator());
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
objectAnimator.start();
}
public void stopBlinkEffect(View view){
objectAnimator = ObjectAnimator.ofInt(view, "backgroundColor", Color.GRAY, Color.YELLOW);
objectAnimator.cancel();
}
You are creating a new object of ObjectAnimator to stop the animation which is started by different ObjectAnimator.
It should be like this
ObjectAnimator objectAnimator;
public void manageBlinkEffect(View view){
objectAnimator = ObjectAnimator.ofInt(view, "backgroundColor", Color.GRAY, Color.YELLOW)
objectAnimator.setDuration(1000);
objectAnimator.setEvaluator(new ArgbEvaluator());
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
objectAnimator.start();
}
public void stopBlinkEffect(View view){
objectAnimator.cancel();
}
Please see the source code,The ObjectAnimator.ofInt(view, "backgroundColor", Color.GRAY, Color.YELLOW); return new object every time.
you should :
public void manageBlinkEffect(View view){
objectAnimator = ObjectAnimator.ofInt(view, "backgroundColor",
Color.GRAY, Color.YELLOW);
objectAnimator.setDuration(1000);
objectAnimator.setEvaluator(new ArgbEvaluator());
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
objectAnimator.start();
}
public void stopBlinkEffect(View view){
objectAnimator.cancel();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With