Search code examples
androidandroid-animationandroid-event

How to Stop Event's While Animation is in process?


I am working on Animating my view where i animate the translation and scaling of View.

Problem:

If my animation duration is for 2000 ms (2 Sec) i don't want any user event's to interfere in between animation.

Example if Double Tap on View Trigger's the Scaling Animation and scrolling trigger's Translation Animation.Both animation duration is 2 seconds,But if i double tap and scroll one after another it create's weird result.

So i want to stop event's when animation is going on.

Is there any easy solution without maintaining the state of OnGoing animation and overriding the onTouchEvent to disable events?


Solution

  • Solution that i used:

    • Created a State of Animation

       private var isAnimationOnGoing: Boolean = false
      
    • Setting the State in Animation Listener

      translationAnimation.setAnimationListener(object : Animation.AnimationListener {
          override fun onAnimationRepeat(animation: Animation?) {
          }
      
          override fun onAnimationEnd(animation: Animation?) {
              isAnimationOnGoing = false
          }
      
          override fun onAnimationStart(animation: Animation?) {
              isAnimationOnGoing = true
          }
      })
      
    • Use dispatchTouchEvent(ev: MotionEvent?) . to prevent event's to be received by the ViewGroup or by children's of ViewGroup

      override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
          val dispatchTouchEvent = super.dispatchTouchEvent(ev)
          if (isAnimationOnGoing) {
              return false
          }
          return dispatchTouchEvent
        }