对象动画师在电池保护模式下不能使用JellyBean(> Android 5.x)

kir*_*eph 5 animation android objectanimator battery-saver android-5.0-lollipop

我的应用程序大量使用ObjectAnimator.但我最近注意到,当打开省电模式时,使用ObjectAnimator的动画不起作用,有时甚至会崩溃.由于应用UI的流畅性很大程度上依赖于动画,因此我不能省略它们中的任何一个.请提供解决方法,以便即使在省电模式下也可以使用这些动画.所有的动画师都会造成这个问题吗?提前致谢.

小智 1

实际上,当打开省电模式时,Android会禁用动画,这意味着您为animator设置的任何持续时间都将更改为0。由于duration为0,Animator将获取应用于目标视图的最后一个值。例如:

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();
Run Code Online (Sandbox Code Playgroud)

动画结束时视图的位置将设置为 (0, 100)。在省电模式下,动画师设置视图的“y”属性时,动画师的持续时间将更改为0,并且视图尚未布局,因此动画似乎失败。

解决:

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();
    }
});
Run Code Online (Sandbox Code Playgroud)

我们应该确保视图在动画师执行之前完成布局。