androidobjectanimator

How to stop ObjectAnimator in android?


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

Solution

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