Android 如何在 BackStack 上反转片段动画?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10886669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Reverse Fragment Animations on BackStack?
提问by
I thought the system would reverse animations on the backstack when the back button is pressed when using fragments using the following code:
我认为系统会在使用以下代码使用片段时按下后退按钮时反转后台堆栈上的动画:
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();
回答by
According to the android documentation for custom animation:
Change:
改变:
ft.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out);
To:
到:
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!!
现在 backstack 动画 - 反过来!!
回答by Aniket
Use the Correct animation I have used the following and its working like a charm
使用正确的动画我使用了以下内容,它的工作就像一个魅力
slide_in_left.xml
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
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
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
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>
Then Use following while adding fragment
然后在添加片段时使用以下内容
setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left,
R.anim.slide_out_right, R.anim.slide_in_right)
and it will worked 100%
它会 100% 奏效
回答by Hoang Nguyen Huu
in my case
就我而言
fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right,
R.anim.slide_in_right, R.anim.slide_out_left);
would create perfect animation.
将创造完美的动画。
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>
回答by TarikW
.setCustomAnimations(R.animator.fragment_fade_in,
R.animator.fragment_fade_out,
R.animator.fragment_fade_p_in,
R.animator.fragment_fade_p_out)
Replace the above with:
将以上内容替换为:
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();
回答by Islam Alshnawey
This is as mentioned in Fragment Transaction class .
这是在 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);
so finally you can use method like this
所以最后你可以使用这样的方法
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();
回答by MODERN
this work for me!! this code for fragment! if you want to use this code in activity, delete at the beginning getActivity()
!!
这对我有用!!这段代码片段!如果要在活动中使用此代码,请在开头删除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();
Good luck to you!!
祝你好运!!