如果想设定应用的activity之间切换可以使用下面的属性
activityOpenEnterAnimation
activityOpenExitAnimation
activityCloseEnterAnimation
activityCloseExitAnimation
他们的关系如下:
<style name="AppTheme" parent="android:Theme.Light"> <item name="android:windowNoTitle">true</item> <item name="android:windowAnimationStyle">@style/activityAnimation</item> </style> <!-- animation 样式 --> <style name="activityAnimation" parent="@android:style/Animation"> <item name="android:activityOpenEnterAnimation">@anim/slide_right_in</item> <item name="android:activityOpenExitAnimation">@anim/slide_left_out</item> <item name="android:activityCloseEnterAnimation">@anim/slide_left_in</item> <item name="android:activityCloseExitAnimation">@anim/slide_right_out</item> </style> <application android:name=".MainApp" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
使用代码设置
public void overridePendingTransition (int enterAnim, int exitAnim) API level 5
enterAnim resource ID of the animation resource to use for the incoming activity. Use 0 for no animation.
exitAnim resource ID of the animation resource to use for the outgoing activity. Use 0 for no animation.
动态的修改activity切换时的动画, 要在startActivity(Intent) 或者 finish() 之后调用。
比如从打开一个页面,从当前页面的底部向上滑出,当前页面不动。
bottom_up.xml 往上滑出现
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="100%"
android:toYDelta="0"
/>
up_bottom.xml 向下滑消失
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_mediumAnimTime" android:fromYDelta="0" android:toYDelta="100%" />
still.xml 不变的动画,透明度不变
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromAlpha="1.0" android:toAlpha="1.0" />
当前页面打开新页面:
startActivity(intent);
overridePendingTransition(R.anim.bottom_up, 0);
(这里退出的动画设为0的话,新界面出来的时候,当前页面在后面会消失掉,可以设一个不变的动画来解决:
overridePendingTransition(R.anim.bottom_up, R.anim.still);)
新页面退出:
finish(); overridePendingTransition(0, R.anim.up_bottom);