一、设置APP主题,配置文件方式实现切换动画
1.自定义一个动画样式继承系统提供的@android:style/Animation
<style name="activityAnimation" parent="@android:style/Animation">
<item name="android:activityOpenEnterAnimation">@anim/slide_in_from_top</item>
<item name="android:activityOpenExitAnimation">@anim/slide_out_from_bottom</item>
<item name="android:activityCloseEnterAnimation">@anim/slide_in_from_top</item>
<item name="android:activityCloseExitAnimation">@anim/slide_out_from_bottom</item>
</style>
2.设置AppTheme主题的android:windowAnimationStyle属性
<style name="AppTheme" parent="@android:style/Theme">
<item name="android:windowAnimationStyle">@style/activityAnimation</item>
</style>
例:
FirstActivity与SecondActivity切换,从First启动Second,再从Second回到First
OpenEnterAnimation:starActivity时,SecondActivity进入动画
OpenExitAnimation:starActivity时,FirstActivity退出动画
CloseEnterAnimation:finish时,FirstActivity进入动画
CloseExitAnimation:finish时,SecondActivity退出动画
二、代码方式实现某两个activity的切换动画
overridePendingTransition(int enterAnim, int exitAnim),这个方法可以放在startActivity(intent)和finish()方法的后面。
例:
FirstActivity启动SecondActivity,Second是enterAnim,First是exitAnim
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_from_bottom,R.anim.slide_out_from_top);
SecondActivity返回FirstActivity,First是enterAnim,Second是exitAnim
overridePendingTransition(R.anim.slide_in_from_top,R.anim.slide_out_from_bottom);
从下进入slide_in_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="-100%p"
android:toYDelta="0"/>
从上进入slide_in_from_top.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="-100%p"
android:toYDelta="0"/>
从下退出slide_out_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="0"
android:toYDelta="100%p"/>
从上退出slide_out_from_top.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromYDelta="0"
android:toYDelta="-100%p"/>