动画XML文件
slide_right_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromXDelta="100.0%p" android:toXDelta="0.0" /> </set>
slide_left_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromXDelta="0.0" android:toXDelta="-100.0%p" /> </set>
slide_left_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromXDelta="-100.0%p" android:toXDelta="0.0" /> </set>
slide_right_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromXDelta="0.0" android:toXDelta="100.0%p" /> </set>
bottom_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="350"/> </set>
bottom_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="350" /> </set>
使用动画
styles.xml
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> <!-- 设置activity切换动画 --> <item name="android:windowAnimationStyle">@style/activityAnimation</item> </style> <!-- animation 样式 --> <style name="activityAnimation" parent="@android:style/Animation.Activity"> <!--当我们从 A1 启动 A2 时,A2 出现在屏幕上,这个动画叫做 android:activityOpenEnterAnimation--> <item name="android:activityOpenEnterAnimation">@anim/slide_right_in</item> <!--当我们从 A1 启动 A2 时,A1 从屏幕上消失,这个动画叫做 android:activityOpenExitAnimation--> <item name="android:activityOpenExitAnimation">@anim/slide_left_out</item> <!--当我们从 A2 退出回到 A1 时,A1 出现在屏幕上,这个叫做 android:activityCloseEnterAnimation--> <item name="android:activityCloseEnterAnimation">@anim/slide_left_in</item> <!--当我们从 A2 退出回到 A1 时,A2 从屏幕上消失,这个叫做 android:activityCloseExitAnimation--> <item name="android:activityCloseExitAnimation">@anim/slide_right_out</item> </style> </resources>
或
启动 Activity A中
Intent intent = new Intent(); intent.setClassName(cxt,"com.test.DemoActivity");//打开一个activity cxt.startActivity(intent); cxt.overridePendingTransition(R.anim.bottom_in,0);
退出 Activity B中
@Override public void finish() { super.finish(); overridePendingTransition(0, R.anim.bottom_out); }