如何在BackStack上反转片段动画?


114

我以为使用以下代码片段时,按下后退按钮时,系统会在后退堆栈上反转动画:

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out);
ft.replace(R.id.viewContainer, new class(), "layout").addToBackStack(null).commit();

Answers:


265

根据android文档的自定义动画

更改:

ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out);

至:

ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out );

现在堆栈动画-反过来!


2
顺便说一句,我知道这与您的问题和答案无关,但是您可以将我链接到一些可以解释customAnimations的内容吗?:P
AreusAstarte

2
AreusAstarte:看developer.android.com/reference/android/app/...,INT,INT,INT)
mDroidd

您好,我实际上使用的是Content Transition,工作正常,但是当我按回去并转到上一个片段时,背景消失了,不仅使视图变得生气蓬勃,而且还覆盖了繁琐的视图,有什么办法可以避免这种情况?
user3497504

23

使用正确的动画我已经使用了以下内容,其工作方式就像是魅力

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime" >
    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="1000"
        android:valueTo="0"
        android:valueType="floatType" />
</set>

slide_in_right.xml

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime" >

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="0"
        android:valueTo="1000"
        android:valueType="floatType" />

</set>

slide_out_left.xml

   <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime" >

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="0"
        android:valueTo="-1000"
        android:valueType="floatType" />

</set>

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime" >

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="x"
        android:valueFrom="-1000"
        android:valueTo="0"
        android:valueType="floatType" />

</set>

然后在添加片段时使用以下内容

setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left,
                                R.anim.slide_out_right, R.anim.slide_in_right)

它会100%工作


2
请注意,如果您正在使用支持片段管理器,或者您的片段扩展了片段的支持版本,则此方法将无效
w3bshark 2015年

@ w3bshark如何使用支持库FragmentManagerFragment从支持库获取动画?
Daniel Shatz

2
@DanielShatz您必须使用翻译而不是objectAnimators。例如,slide_in_left.xml将是:<translate android:fromXDelta="100%" android:startOffset="25" android:toXDelta="0" />请参阅以下答案:stackoverflow.com/a/5151774/1738090
w3bshark

1
我正在尝试(在棉花糖设备上-没有尝试其他版本)。它不起作用。最后的FragmentTransaction fragmentTransaction = getFragmentManager()。beginTransaction(); fragmentTransaction.setCustomAnimations(R.anim.enter_from_right,R.anim.exit_to_left,R.anim.enter_from_left,R.anim.exit_to_right); fragmentTransaction.replace(R.id.fl_right_container,detailFragment); fragmentTransaction.replace(R.id.fl_left_container,子类别Fragment,TestActivity.TAG_SUBCATEGORIES_FRAGMENT); fragmentTransaction.commit();
techtinkerer's

13

就我而言

fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, 
                       R.anim.slide_in_right, R.anim.slide_out_left);

将创建完美的动画。

slide_in_right

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="50%p" android:toXDelta="0"
               android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
           android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_out_left

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-50%p"
               android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
           android:duration="@android:integer/config_mediumAnimTime" />
</set>

1
我想自己做,但是我太懒了。我说有人应该将其发布在StackOverflow上,并且在这里!哈哈
F.Mysir

1
以前没有人发布过这个帖子,我非常相信现在该轮到我发布这个答案了,以帮助可能和我一样的人...大声笑@ F.Mysir
Hoang Nguyen Huu

3
.setCustomAnimations(R.animator.fragment_fade_in,
        R.animator.fragment_fade_out,
        R.animator.fragment_fade_p_in,
        R.animator.fragment_fade_p_out)

将以上内容替换为:

mFragmentManager.beginTransaction()
    .setCustomAnimations(R.animator.fragment_fade_in,
            R.animator.fragment_fade_out,
            R.animator.fragment_fade_p_in,
            R.animator.fragment_fade_p_out)
    .replace(R.id.main_container, FragmentPlayerInfo.getInstance(data))
    .addToBackStack(FragmentPlayerInfo.TAG)
    .commit();

1
我建议您添加关于您的建议如何帮助的解释。
Wtower '16

2
我不知道为什么会这样(:,但是在replace和之后添加动画时addToBackstack不起作用
TarikW

2
@TarikW我来晚了,但是顺序很重要,您需要在替换之前调用setCostomAnimations,addToBackStack方法
MD Husnain Tahir

1

如Fragment Transaction类中所述。

/**
     * Set specific animation resources to run for the fragments that are
     * entering and exiting in this transaction. The <code>popEnter</code>
     * and <code>popExit</code> animations will be played for enter/exit
     * operations specifically when popping the back stack.
     *
     * @param enter An animation or animator resource ID used for the enter animation on the
     *              view of the fragment being added or attached.
     * @param exit An animation or animator resource ID used for the exit animation on the
     *             view of the fragment being removed or detached.
     * @param popEnter An animation or animator resource ID used for the enter animation on the
     *                 view of the fragment being readded or reattached caused by
     *                 {@link FragmentManager#popBackStack()} or similar methods.
     * @param popExit An animation or animator resource ID used for the enter animation on the
     *                view of the fragment being removed or detached caused by
     *                {@link FragmentManager#popBackStack()} or similar methods.
     */
    @NonNull
    public abstract FragmentTransaction setCustomAnimations(@AnimatorRes @AnimRes int enter,
            @AnimatorRes @AnimRes int exit, @AnimatorRes @AnimRes int popEnter,
            @AnimatorRes @AnimRes int popExit);

所以最后你可以使用这样的方法

 mFragmentManager.beginTransaction()
                        .replace(R.id.container, fragment)
                        .setCustomAnimations(R.anim.slide_left,//enter
                                             R.anim.slide_out_left,//exit
                                             R.anim.slide_right,//popEnter
                                             R.anim.slide_out_right)//popExit
                        .addToBackStack(fragment.toString())
                        .commit();

0

这项工作对我来说!!此代码为片段!如果您想在活动中使用此代码,请在开始处删除getActivity()

getActivity().getSupportFragmentManager()
        .beginTransaction()
        .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.fade_out,android.R.anim.slide_in_left, android.R.anim.fade_out)
        .replace(R.id.fragment_container, new YourFragment)
        .addToBackStack(null)
        .commit();

祝你好运!!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.