Animation.hasStarted()和Animation.hasEnded()方法无法正常工作

时间:2019-11-19 09:31:34

标签: xml kotlin android-animation

我尝试将setAnimationListener类实现为从anim xml加载的animationSet,但发现一些奇怪的事情:

  
      
  • Animation.hasStarted(),应该在true

  • 中调用时返回onAnimationStart   
  • Animation.hasEnded(),应该在true

  • 中调用时返回onAnimationEnd   

发生了什么事,我的Animation.hasStarted()Animation.hasEnded()falseonAnimationStart中分别返回了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

0 个答案:

没有答案