FragmentTransaction.setCustomAnimations() 在节电模式开启时不起作用

FragmentTransaction.setCustomAnimations() doesn't work when battery saver is on

我在片段之间的过渡中使用自定义动画(从左向右滑动)。但是当调用 FragmentTransaction.commit() 后节电模式打开时,第二个片段没有显示。

    FragmentTransaction ft = getFragmentManager().beginTransaction();

    ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
    ft.replace(R.id.fragment_help_container, newFragment);
    // Start the animated transition.
    ft.commit();

这是我在 LogCat 中找到的与此问题相关的内容。

    02-18 20:33:26.908 932-932/? E/Icon: Unable to load resource 0x00000000 from pkg=
            android.content.res.Resources$NotFoundException: Resource ID #0x0
            at android.content.res.Resources.getValue(Resources.java:1351)
            at android.content.res.Resources.getDrawable(Resources.java:804)
            at android.graphics.drawable.Icon.loadDrawableInner(Icon.java:313)
            at android.graphics.drawable.Icon.loadDrawable(Icon.java:269)
            at android.widget.RemoteViews$TextViewDrawableAction.apply(RemoteViews.java:1502)
            at android.widget.RemoteViews.performApply(RemoteViews.java:2804)
            at android.widget.RemoteViews.apply(RemoteViews.java:2764)
            at android.widget.RemoteViews$ViewGroupAction.apply(RemoteViews.java:1373)
            at android.widget.RemoteViews.performApply(RemoteViews.java:2804)
            at android.widget.RemoteViews.reapply(RemoteViews.java:2795)
            at com.android.systemui.statusbar.BaseStatusBar.updateNotificationViews(BaseStatusBar.java:2064)
            at com.android.systemui.statusbar.BaseStatusBar.updateNotification(BaseStatusBar.java:1939)
            at com.android.systemui.statusbar.BaseStatusBar.run(BaseStatusBar.java:487)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

slide_in_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" >

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:valueFrom="1.0"
    android:valueTo="0"
    android:propertyName="xFraction"
    android:duration="@android:integer/config_mediumAnimTime" />

</set>

slide_out_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" >

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:valueFrom="0"
    android:valueTo="-1.0"
    android:propertyName="xFraction"
    android:duration="@android:integer/config_mediumAnimTime" />

</set>

在大多数情况下,删除设置似乎对我有用。当设备处于电池模式时,objectAnimator 似乎表现为 noOp,因此您不会拥有漂亮的动画,但您不必担心任何并发症。如果您需要设置更复杂的动画(比如同时淡入淡出和平移),那么您可能必须添加一个辅助方法来设置自定义动画。在我的例子中,我实际上有一个应该动画的视图,只是因为 set

而没有出现
    /**
 * NOTE: on lollipop+ the battery save screws with the animations. in most cases its fine but in some it actually prevents
 * the fragment from displaying. because of that I am creating this helper that must always be used
 */
public static void setCustomAnimation(Activity activity, FragmentTransaction transaction, int enter, int exit, int popEnter, int popExit)
{
    PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && powerManager.isPowerSaveMode())
    {
        return;
    }
    transaction.setCustomAnimations(enter, exit, enter, exit);
}