实现类似微信的activity切换动画

1、首先定义4个动画文件。

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%p"
        android:toXDelta="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"
        android:toXDelta="-100%p" />

</set>

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%p"
        android:toXDelta="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"
        android:toXDelta="100%p" />

</set>

2、定义一个style

<style name="activityAnimation" parent="@android:style/Animation">
        <item name="android:activityOpenEnterAnimation">@anim/a_slide_right_in</item>
        <item name="android:activityOpenExitAnimation">@anim/a_slide_left_out</item>
        <item name="android:activityCloseEnterAnimation">@anim/a_slide_left_in</item>
        <item name="android:activityCloseExitAnimation">@anim/a_slide_right_out</item>
    </style>

3、把定义的style放到AppTheme里边

<style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowAnimationStyle">@style/activityAnimation</item>
    </style>

4、在AndroidManifest.xml里边设置application的theme

<application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

</application>

相关推荐