需求:
- 将Activity出场动画改为从上至下
- 将Activity退出动画改为从下至上
在xml中定义动画效果
进场动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true">
<translate
android:fromYDelta="100%p"
android:toYDelta="0"
android:duration="600" />
</set>
退出动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true">
<translate
android:fromYDelta="0"
android:toYDelta="100%p"
android:duration="600" />
</set>
使用:进场动画,退出动画需要在startActivity()或者finsh()方法调用之后,通过调用 overridePendingTransition(,)方法
进场:overridepending(R.anim.activity_enter_anim,0)
退出:overridepending(0,R.anim.activity_exit_anim)
注意
由于在进出动画中设置了duration 延迟效果所以Activity在切换时会看到黑色的背景。用户体验很不好
解决办法就是在AndroidManifeset中将Application的Theme在<item>中添加一个属性windowIsTranslucent设置为 true 代表将切换时的背景设置为透明。
例:
<style name="AppTheme"parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!--切换背景换为透明-->
<item name="android:windowIsTranslucent">true</item>
</style>
done