打开电池保护程序时,FragmentTransaction.setCustomAnimations()不起作用

时间:2016-02-18 18:42:31

标签: android animation fragment battery-saver

我在片段之间的过渡中使用自定义动画(从左到右滑动)。但是,在调用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$6$2.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>

1 个答案:

答案 0 :(得分:2)

在大多数情况下,移除套装似乎对我有效。当设备处于电池模式时,objectAnimator似乎表现为noOp,因此您不会拥有漂亮的动画,但您不必担心任何复杂情况。如果你需要更复杂的动画需要集合(比如说淡入淡出并同时翻译),那么你可能需要添加一个辅助方法来设置自定义动画。在我的情况下,我实际上有一个视图,因为设置

而应该仅显示动画
    /**
 * 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);
}