Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 971
cantona_7 Avatar asked Feb 23 '26 12:02

cantona_7


2 Answers

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();
    }
like image 61
Prithvi Bhola Avatar answered Feb 25 '26 02:02

Prithvi Bhola


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();
}
like image 35
Longalei Avatar answered Feb 25 '26 01:02

Longalei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!