1

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?

Anmol
  • 8,110
  • 9
  • 38
  • 63
  • just keep everything disabled when you are animating and enable them again when your animation ends – Vivek Mishra Dec 19 '18 at 06:46
  • @VivekMishra you mean disabling event's of the view based on the state of the animation? Is that the only approach? – Anmol Dec 19 '18 at 09:16

2 Answers2

1

Lock UI from events:

private void lockUI() {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);   
}

Unlock UI:

private void unlockUI() {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
Liar
  • 1,235
  • 1
  • 9
  • 19
  • I am trying to figure out solution from inside the View and Don't really prefer to go to activity/Fragment for each animation.Want to handle this case inside view itself. As i am in Single activity architecture this logic will complicate the flow. @Liar – Anmol Dec 19 '18 at 09:15
0

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
      }
    
Anmol
  • 8,110
  • 9
  • 38
  • 63