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.
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.
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