一 .主题中设置
ActivityTranslationAni
<!-- Base application theme. qtip 默认白色 -->
<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:windowAnimationStyle">@style/ActivityTranslationAni </item>
<item name="actionBarTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="android:navigationIcon">@drawable/nav_back</item>
<item name="android:actionOverflowButtonStyle">@style/OverFlow</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowBackground">@android:color/white</item>`
</style>
<!-- 平移动画 @android:style/Animation.Activity-->
<style name="ActivityTranslationAni" parent="android:Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/anim_open_in</item>
<item name="android:activityOpenExitAnimation">@anim/anim_open_out</item>
<item name="android:activityCloseEnterAnimation">@anim/anim_close_in</item>
<item name="android:activityCloseExitAnimation">@anim/anim_close_out</item>
</style>
系统定义的Activity动画
/Users/qingyc/Library/Android/sdk/platforms/android-28/data/res/values/styles.xml
<style name="Animation.Activity">
<item name="activityOpenEnterAnimation">@anim/activity_open_enter</item>
<item name="activityOpenExitAnimation">@anim/activity_open_exit</item>
<item name="activityCloseEnterAnimation">@anim/activity_close_enter</item>
<item name="activityCloseExitAnimation">@anim/activity_close_exit</item>
<item name="taskOpenEnterAnimation">@anim/task_open_enter</item>
<item name="taskOpenExitAnimation">@anim/task_open_exit</item>
<item name="launchTaskBehindTargetAnimation">@anim/launch_task_behind_target</item>
<item name="launchTaskBehindSourceAnimation">@anim/launch_task_behind_source</item>
<item name="taskCloseEnterAnimation">@anim/task_close_enter</item>
<item name="taskCloseExitAnimation">@anim/task_close_exit</item>
<item name="taskToFrontEnterAnimation">@anim/task_open_enter</item>
<item name="taskToFrontExitAnimation">@anim/task_open_exit</item>
<item name="taskToBackEnterAnimation">@anim/task_close_enter</item>
<item name="taskToBackExitAnimation">@anim/task_close_exit</item>
<item name="wallpaperOpenEnterAnimation">@anim/wallpaper_open_enter</item>
<item name="wallpaperOpenExitAnimation">@anim/wallpaper_open_exit</item>
<item name="wallpaperCloseEnterAnimation">@anim/wallpaper_close_enter</item>
<item name="wallpaperCloseExitAnimation">@anim/wallpaper_close_exit</item>
<item name="wallpaperIntraOpenEnterAnimation">@anim/wallpaper_intra_open_enter</item>
<item name="wallpaperIntraOpenExitAnimation">@anim/wallpaper_intra_open_exit</item>
<item name="wallpaperIntraCloseEnterAnimation">@anim/wallpaper_intra_close_enter</item>
<item name="wallpaperIntraCloseExitAnimation">@anim/wallpaper_intra_close_exit</item>
<item name="fragmentOpenEnterAnimation">@animator/fragment_open_enter</item>
<item name="fragmentOpenExitAnimation">@animator/fragment_open_exit</item>
<item name="fragmentCloseEnterAnimation">@animator/fragment_close_enter</item>
<item name="fragmentCloseExitAnimation">@animator/fragment_close_exit</item>
<item name="fragmentFadeEnterAnimation">@animator/fragment_fade_enter</item>
<item name="fragmentFadeExitAnimation">@animator/fragment_fade_exit</item>
</style>
二.代码设置
设置activity之间的转场动画 A->B
在A的onPause 或者finis() 或者B的onResume之前调用
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
如果想要在B -> A B返回A时设置动画
override fun finish() {
super.finish()
//在super.finish()之前调用无效
overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_bottom_out)
}
//或者
override fun onBackPressed() {
super.onBackPressed()
//在super.onBackPressed()之前无效
overridePendingTransition(R.anim.slide_bottom_in, R.anim.slide_bottom_out)
}
网友评论