onAnimationEnd is not getting called, onAnimationStart works fine

后端 未结 14 1604
情书的邮戳
情书的邮戳 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.

    0 讨论(0)
  • 2020-12-23 20:02

    There might be someone still having this issue and not found a solution -even though reads a lot of stackoverflow answers- like me!

    So my case was: I used animatorSet and

    1. there was not a single view that i could call clearAnimation on,
    2. I wasn't calling my animation from backgroundThread -which you should never do that, btw-

    As a solution, i did call animatorSet.end() right before animatorSet.start()

    0 讨论(0)
  • 2020-12-23 20:07

    I guess Its gets called after sometime becasue you are using setDuration for animation and passing 1000 ms in it. Just pass 0 and it won't take a while to get called.

    0 讨论(0)
  • 2020-12-23 20:12

    Are you not setting another animation before that one you are waiting ends?

    Without context is hard to understand if it's something like that... but it is probably one of the only things that would cause it.

    0 讨论(0)
  • 2020-12-23 20:14

    After I don't remember how may posts read and days spent finding out a solution for this issue I discovered that if the object to move is not on the Screen region (for example is positioned out of the screen coords) OnAnimationEnd callback is not getting called. Probably the animation fails just after started (start method is called, I coded a listener) but nothing is wrote into logcat. Maybe this is not exactly your case but this finally solved my problem and hope it can help you too.

    0 讨论(0)
  • 2020-12-23 20:16

    Try to use overridePendingAnimation(int,int). i.e. overridePendingAnimation(0,0)

    It will override your default animation and then you can define you own animation By calling the method startAnimation using the object of View.

    Here is my example code. Dont know whether it will be helpful to you or not.

    overridePendingTransition(0,0);
    //finish();
    v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fadeout));
    startActivity(new Intent(ContentManagerActivity.this,Mainmenu_activity.class));
    overridePendingTransition(R.anim.fadein,0);
    
    0 讨论(0)
提交回复
热议问题