Android 替换片段时如何应用淡入/淡出动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23421701/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:07:15  来源:igfitidea点击:

How to apply a fade-in/fade-out animation when replacing a fragment

android

提问by Qadeer Hussain

I am replacing a fragment with another fragment. I want the first fragment to disappear with a fade-out effect and second fragment to appear with fade-in effect. How is this done?

我正在用另一个片段替换一个片段。我希望第一个片段以淡出效果消失,第二个片段以淡入效果出现。这是怎么做的?

回答by Rafique Mohammed

With addition to @MD code

除了@MD 代码

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();

ft.setCustomAnimations(R.anim.fade_in,
                R.anim.fade_out);
ft.replace(R.id.realtabcontent, fragment);
ft.commit();

and When you Pop Fragment then apply animation like:

当你弹出片段然后应用动画,如:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(R.anim.fade_out, R.anim.fade_in);

ft.replace(R.id.realtabcontent, fragment);      
ft.commit();

and XML for fadeIn

和用于淡入的 XML

<set xmlns:android="http://schemas.android.com/apk/res/android">
       <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

and XML for fadeOut

和用于淡出的 XML

<set xmlns:android="http://schemas.android.com/apk/res/android">
       <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

回答by M D

When you Push a Fragmentthen apply animation like:

当你 Push aFragment然后应用动画,如:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();

ft.setCustomAnimations(R.anim.fade_in,
                R.anim.fade_out);
ft.replace(R.id.realtabcontent, fragment);
ft.commit();

and When you Pop Fragmentthen apply animation like:

当你弹出Fragment然后应用动画,如:

FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(R.anim.fade_out, R.anim.fade_in);

ft.replace(R.id.realtabcontent, fragment);      
ft.commit();

Hope this works for you.

希望这对你有用。

Update:For more information go to

更新:有关更多信息,请访问

  1. http://android-er.blogspot.in/2013/04/implement-animation-in.html
  2. Animate the transition between fragments
  1. http://android-er.blogspot.in/2013/04/implement-animation-in.html
  2. 动画片段之间的过渡

回答by Simon

It is worth adding that setCustomAnimations can also have 4 arguments:

值得补充的是 setCustomAnimations 也可以有 4 个参数:

FragmentTransaction setCustomAnimations (int enter, 
                                         int exit, 
                                         int popEnter, 
                                         int popExit)

Set specific animation resources to run for the fragments that are entering and exiting in this transaction. The popEnter and popExit animations will be played for enter/exit operations specifically when popping the back stack.

设置特定动画资源以运行此事务中进入和退出的片段。popEnter 和 popExit 动画将在弹出返回堆栈时专门针对进入/退出操作播放。