场景:
- A打开B页面,A页面保持不动,B页面从底部滑入显示
- B返回A页面,A页面保持不动, B页面从顶部向下滑出消失
1:通过Theme的windowAnimationStyle属性实现
<style name="Kt.DownIn" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowAnimationStyle">@style/AudioPlayAnim</item>
</style>
<style name="AudioPlayAnim" parent="@android:style/Animation.Activity">
<!--马上要进入到的Activity的动画,页面B-->
<item name="android:activityOpenEnterAnimation">@anim/anim_bottom_in</item>
<!--马上要推到后台或者finish的Activity的动画,页面A-->
<item name="android:activityOpenExitAnimation">@anim/anim_no</item>
<!--退出即将回到的Activity的动画,页面A-->
<item name="android:activityCloseEnterAnimation">@anim/anim_no</item>
<!--当前即将finish退出的Activity的动画,页面B-->
<item name="android:activityCloseExitAnimation">@anim/anim_bottom_out</item>
</style>
使用过程用,如果没有设置android:activityOpenExitAnimation和android:activityCloseEnterAnimation这两个A页面的消失动画,就可能会出现短暂的黑屏交互现象;
如果theme设置windowIsTranslucent为true,则建议继承Animation.Translucen,否则动画可能不生效,如下:
<style name="AudioPlayAnim" parent="@android:style/Animation.Translucen">
<item name="android:windowEnterAnimation">@anim/anim_bottom_in</item>
<item name="android:windowExitAnimation">@anim/anim_bottom_out</item>
<!--马上要进入到的Activity的动画,页面B-->
<item name="android:activityOpenEnterAnimation">@anim/anim_bottom_in</item>
<!--马上要推到后台或者finish的Activity的动画,页面A-->
<item name="android:activityOpenExitAnimation">@anim/anim_no</item>
<!--退出即将回到的Activity的动画,页面A-->
<item name="android:activityCloseEnterAnimation">@anim/anim_no</item>
<!--当前即将finish退出的Activity的动画,页面B-->
<item name="android:activityCloseExitAnimation">@anim/anim_bottom_out</item>
</style>
另外一种解决windowIsTranslucent为true的时候,动画效果不生效的方案:
open var activityCloseEnterAnimation: Int = 0
open var activityCloseExitAnimation: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initWindowAnimAttr()
}
@SuppressLint("ResourceType")
private fun initWindowAnimAttr() {
try {
var activityStyle = theme.obtainStyledAttributes(intArrayOf(R.attr.windowAnimationStyle))
val windowAnimationStyleResId = activityStyle.getResourceId(0, 0)
activityStyle.recycle()
activityStyle = theme.obtainStyledAttributes(windowAnimationStyleResId, intArrayOf(R.attr.activityCloseEnterAnimation, R.attr.activityCloseExitAnimation))
activityCloseEnterAnimation = activityStyle.getResourceId(0, 0)
activityCloseExitAnimation = activityStyle.getResourceId(1, 0)
activityStyle.recycle()
} catch (e: Exception) {
}
}
override fun finish() {
super.finish()
isDestroy = true
overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation)
}
anim_bottom_in, anim_no以及anim_bottom_out的动画文件如下
!---anim_bottom_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toYDelta="0" />
</set>
!---anim_no
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"//一定要设置跟进入动画相同的时间,解决黑屏的关键
android:fromYDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:toYDelta="0" />
</set>
!---anim_bottom_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:toYDelta="100%" />
</set>
2: 通过overridePendingTransition实现效果
在页面B的onCreate方法和finish方法中加入以下代码
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
overridePendingTransition(R.anim.anim_bottom_in,R.anim.anim_no)
}
//重写finish方法
override fun finish() {
super.finish()
overridePendingTransition(R.anim.anim_no,R.anim.anim_bottom_out)
}
同样anim_no页面的消失动画一定要设置
网友评论