Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Animator not working in Battery Saver mode post JellyBean(>Android 5.x)

My application heavily uses ObjectAnimator. But I have recently noticed that animations using ObjectAnimator do not work or sometimes even crash when the battery saver mode is turned on. Since the smoothness of the app UI heavily relies on the animations, I can't omit any of them. Please provide a workaround so as to use these animations even in battery saver mode. Do all the animators pose this problem?? Thanks in advance.

like image 893
kiran joseph Avatar asked Nov 08 '22 16:11

kiran joseph


1 Answers

Actually, Android disable animation when battery saver mode is turned on, which means any duration you set to animator will be change to 0. Animator will get the last value to apply to target view since duration was 0. For example:

View container = inflater.inflate(R.layout.container);
View view = inflater.inflate(R.layout.view, null);
container.addView(view);
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "y", 0, 100);
animator.setDuration(200);
animator.start();

The position of view will be set as (0, 100) when animation finish. In battery saver mode, the duration of animator will be change to 0, and view has not layout yet when animator set 'y' property of view, thus animation seems failed.

Resolution:

View container = inflater.inflate(R.layout.container);
View view = inflater.inflate(R.layout.view, null);
new Handler().post(new Runnable() {
    @Override
    public void run() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "y", 0, 100);
        animator.setDuration(200);
        animator.start();
    }
});

We should make sure view had layout finish before animator execution.

like image 78
user2636641 Avatar answered Nov 14 '22 22:11

user2636641