android animation is not finished in onAnimationEnd

AndroidAnimationBackgroundAndroid AnimationDrawable

Android Problem Overview


It seems that an android animation is not truly finished when the onAnimationEnd event is fired although animation.hasEnded is set to true.

I want my view to change it's background drawable on the end of it's ScaleAnimation which it does, but you can clearly see that it is changed some miliseconds before it finishes. The problem is, that it flickers because the new background appears (=is) scaled for a short time until the animation really finishes.

Is there a way to get either the real end of the animation or just prevent the new background from beeing scaled this short period of time?

Thank you!


//EDIT: I'm using an AnimationListener to get the following call:

	@Override
public void onAnimationEnd(Animation animation)
{
	View view = (MyView) ((ExtendedScaleAnimation) animation).getView();

	view.clearAnimation();
	view.requestLayout();
	view.refreshBackground(); // <-- this is where the background gets changed
}

Android Solutions


Solution 1 - Android

Here is the actual bug related to this issue http://code.google.com/p/android-misc-widgets/issues/detail?id=8

This basically states that the onAnimationEnd method doesn't really work well when an AnimationListener is attached to an Animation

The workaround is to listen for the animation events in the view to which you were applying the animation to For example if initially you were attaching the animation listener to the animation like this

mAnimation.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationEnd(Animation arg0) {
        //Functionality here
    }
});

and then applying to the animation to a ImageView like this

mImageView.startAnimation(mAnimation);

To work around this issue, you must now create a custom ImageView

public class MyImageView extends ImageView {

and then override the onAnimationEnd method of the View class and provide all the functionality there

@Override
protected void onAnimationEnd() {
    super.onAnimationEnd();
    //Functionality here
}

This is the proper workaround for this issue, provide the functionality in the over-riden View -> onAnimationEnd method as opposed to the onAnimationEnd method of the AnimationListener attached to the Animation.

This works properly and there is no longer any flicker towards the end of the animation. Hope this helps.

Solution 2 - Android

I was abe to resolve this by calling clearAnimation() on the view being animated inside onAnimationEnd, that took away the flicker Its weird why would anyone have to do that, as onAnimationEnd callback should have been called only if the animation has already ended. But I guess the answer lies in the depth of Framework on how view/layout handles animation callback. For now take it as a hack-free solution, that just works.

        animation.setAnimationListener(new AnimationListener() {

        public void onAnimationEnd(Animation anim) {
        	innerView.clearAnimation();   // to get rid of flicker at end of animation
        	
        	RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
            (innerBlockContainer.getWidth(), innerBlockContainer.getHeight());
        	
            /* Update lp margin, left/top to update layout after end of Translation */
            ViewGroup parent_ofInnerView = (ViewGroup)innerView.getParent();
            vp.updateViewLayout(innerBlockContainer, lp);

        }

        public void onAnimationRepeat(Animation arg0) {}
        
        public void onAnimationStart(Animation arg0) {
        }
        
    });
  
     innerView.startAnimation(animation);

Solution 3 - Android

I had a similar problem and I used Soham's solution with custom view class.

It worked fine, but at the end, I've found a simpler solution that worked for me.

After calling the view.StartAnimation(animation), and before the next step in my program, I've added a short delay that will be long enough to let the animation finish, but short enough to be unnoticeable by the user:

new Handler().postDelayed(new Runnable() {
       @Override
       public void run() {
    	   nextStepInMyProgram();
       }
     }, 200);// delay in milliseconds (200)

Solution 4 - Android

I had same issue and solved it using

view.clearAnimation();

before

view.startAnimation(anim);

Solution 5 - Android

For some reason the onAnimationStart works properly, and the onAnimationEnd doesnt. So heres how I originally did it and what I changed:

Attempt 1 (flicker): a) Move image from 0px to 80px b) In onAnimationEnd, set the image's location to 80px

Attempt 2 (no flicker): a) In onAnimationStart, set the image's location to 80px b) Move the image from -80px to 0px

Hope that made sense. Basically I flipped the way I did it

Solution 6 - Android

Try to use getAnimation() from your object:

public void onShowListBtnClick(View view)
    {
        rightPanel.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left));

        rightPanel.getAnimation().setAnimationListener(new Animation.AnimationListener() {    
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // write your code here
            }
        });
}

Solution 7 - Android

annimation can be also stopped on screen rotation. in this case onAnimationEnd() is not being called. my workaround:

 animation.setDuration(animationDuration);
 animation.setAnimationListener(new AnimationListenerAdapter() {...});
 view.startAnimation(animation);
 handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(!animation.hasEnded()) {
                    // here you can handle this case
                }
            }
        }, animationDuration + 100);
 

Solution 8 - Android

An easy fix is to add one line to AnimationListener.onAnimationEnd():

@Override 
public void onAnimationEnd(Animation a) {
    a.setAnimationListener(null);
    …
}

Solution 9 - Android

I had this issue because my Animation was not started in the main thread. This resulted in a duration of 0. However , the Animation did play correctly - it just called onAnimationEnd() immediately after execution.

Solution 10 - Android

If you are using repeat count as infinite, then onAnimationEnd would not get called. Check the documentation link

Solution 11 - Android

You can also use setUpdateListener, then check the current fraction of the animation progress and act accordingly.

Here's a Kotlin example for a fade-out animation which eventually makes the view gone from the layout:

view.animate()
        .alpha(0f)
        .setDuration(duration)
        .setUpdateListener { animation ->
            if (animation.animatedFraction > 0.99f) {
                view.visibility = View.GONE
            }
        }
        .start()

Solution 12 - Android

This worked for me:

@Override
public void onAnimationUpdate(ValueAnimator animation) {
    if (Float.compare(animation.getAnimatedFraction(),1.0f)) {
    // animation ended;
        //do stuff post animation
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShadowMareView Question on Stackoverflow
Solution 1 - AndroidSohamView Answer on Stackoverflow
Solution 2 - Androidlucknow_rocksView Answer on Stackoverflow
Solution 3 - AndroidDoigenView Answer on Stackoverflow
Solution 4 - AndroidsavepopulationView Answer on Stackoverflow
Solution 5 - AndroidMattView Answer on Stackoverflow
Solution 6 - AndroidButsatyView Answer on Stackoverflow
Solution 7 - AndroidOleg KhalidovView Answer on Stackoverflow
Solution 8 - AndroidAlex CohnView Answer on Stackoverflow
Solution 9 - AndroidByte WelderView Answer on Stackoverflow
Solution 10 - AndroidsanjeebView Answer on Stackoverflow
Solution 11 - Androiddavee44View Answer on Stackoverflow
Solution 12 - AndroidchochimView Answer on Stackoverflow