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

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

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();
43286 次浏览

根据 自定义动画的 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 );

and now the backstack animates - In reverse!!

Use the Correct animation 我已经使用了以下方法,它的工作像一个魅力

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 _ 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 _ 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)

而且会百分之百奏效

.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();

对我来说

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

可以创造出完美的动画效果。

向右滑进

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

向左滑出

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

这对我有用! !这个代码的碎片!如果您想在活动中使用此代码,请从 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();

祝你好运! !

这正如片段事务类中所提到的。

/**
* 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();