onAnimationEnd is not getting called, onAnimationStart works fine

后端 未结 14 1693
情书的邮戳
情书的邮戳 2020-12-23 19:51

I\'ve a ScrollView in the PopupWindow. I\'m animating ScrollView contents using TranslateAnimation.

When animation starts, the onAnimationStart listener is called bu

14条回答
  •  醉话见心
    2020-12-23 20:00

    When you start an animation command in a view that is partially offscreen, the animation start and the onStartListener is called, but the animation doesn't run completely (somehow it is interrupted in the middle). My guess is that, as the view is offscreen, it is canceled and therefore it's onFinish is not called. As a workaround for that i created my own animation listener that start's an handler and uses postDelayed to notify the user of the animation end event. In Kotlin:

    abstract class PartiallyOffScreenAnimationListener : Animation.AnimationListener, AnimationListener {
        override fun onAnimationRepeat(animation: Animation?) {
            onAnimationRepeat_(animation)
        }
        override fun onAnimationEnd(animation: Animation) {}
    
        override fun onAnimationStart(animation: Animation) {
            onAnimationStart_(animation)
            Handler().postDelayed({
                onAnimationEnd_(animation)
                animation.setAnimationListener(null)
            }, animation.duration + 50)
        }
    }
    

    Please notice that, in the case the animation doesn't run completely, the view may be left at an inconsistent state (for example, animating layout parameters may lead to a weird middle interpolator factor being dropped. For that reason, you should verify at the end callback that the view is in the wanted state. If not, just set it manually.

提交回复
热议问题