我尝试将setAnimationListener
类实现为从anim xml加载的animationSet
,但发现一些奇怪的事情:
- 中调用时返回
Animation.hasStarted()
,应该在true
onAnimationStart
- 中调用时返回
Animation.hasEnded()
,应该在true
onAnimationEnd
发生了什么事,我的Animation.hasStarted()
和Animation.hasEnded()
在false
和onAnimationStart
中分别返回了onAnimationEnd
。
这是我的代码:
text_box_slide_down_fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="0" android:toYDelta="750"
android:duration="1000"
/>
<alpha
android:duration="800"
android:fromAlpha="1.0"
android:toAlpha="0.0"
/>
</set>
MainActivity.kt
val slideDownFadeOut = AnimationUtils.loadAnimation(this, R.anim.text_box_slide_down_fade_out)
slideDownFadeOut.setAnimationListener(object : AnimationListener {
override fun onAnimationStart(a: Animation) {
println("slideDown.hasStarted() 00-1: ${a.hasStarted()}")
println("slideDown.hasEnded() 00-2: ${a.hasEnded()}")
}
override fun onAnimationRepeat(a: Animation) {
println("slideDown.hasStarted() 01-1: ${a.hasStarted()}")
println("slideDown.hasEnded() 01-2: ${a.hasEnded()}")
}
override fun onAnimationEnd(a: Animation) {
println("slideDown.hasStarted() 02-1: ${a.hasStarted()}")
println("slideDown.hasEnded() 02-2: ${a.hasEnded()}")
}
})
cardView.setOnClickListener {
textBox.startAnimation(slideDownFadeOut)
}
Logcat
I/System.out: slideDown.hasStarted() 00-1: false
I/System.out: slideDown.hasEnded() 00-2: false
I/System.out: slideDown.hasStarted() 02-1: true
I/System.out: slideDown.hasEnded() 02-2: false