自定义Activity切换动画

2017-02-17 17:58

默认情况下,Activity的切换动画为,右边滑入,左边滑出,我们也可以自定义Activity切换动画,来改变默认的效果

首先,在res目录下新建文件夹anim,用于存放动画文件come_in.xml和come_out.xml

come_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="500"
 android:fromAlpha="0.0"
 android:toAlpha="1.0"
 android:interpolator="@android:anim/accelerate_interpolator"/>

come_out.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="500"
 android:fromAlpha="1.0"
 android:toAlpha="0.0"
 android:interpolator="@android:anim/accelerate_interpolator"/>

然后在styles.xml文件下添加代码

 <style name="anim_fade" parent="Theme.AppCompat.Light">
        <item name="android:windowAnimationStyle">@style/fade</item>
    </style>

    <style name="fade" parent="@android:style/Animation.Activity">

        <item name="android:activityOpenEnterAnimation">@anim/come_in</item>
        <item name="android:activityOpenExitAnimation">@anim/come_out</item>
        <item name="android:activityCloseEnterAnimation">@anim/come_in</item>
        <item name="android:activityCloseExitAnimation">@anim/come_out</item>
    </style>

最后一步,在清单文件中activity标签加入theme

这里写图片描述

源码下载

源码下载:https://github.com/zeke123/ThemeActivity