Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End animation event android

I have a fadeout animation in a view (which is inside a fragment), and everytime the animation happens, after it finishes the view redraws itself again. I found a work around doing view.SetVisibility(View.GONE) . But it doesn't wait for the animation to finish. I would like to execute this setVisibility code only after the animation has finished. What is the best way to do that?

like image 986
Paulo Barros Avatar asked Sep 30 '11 06:09

Paulo Barros


4 Answers

You can add Animation listener to your animation object like

anim.setAnimationListener(new Animation.AnimationListener(){
    @Override
    public void onAnimationStart(Animation arg0) {
    }           
    @Override
    public void onAnimationRepeat(Animation arg0) {
    }           
    @Override
    public void onAnimationEnd(Animation arg0) {
    }
});
like image 147
blessanm86 Avatar answered Nov 03 '22 07:11

blessanm86


Functionally the same as the accepted answer but in a much more concise way:

// Add/Remove any animation parameter
theView.animate()
        .alpha(0)
        .setDuration(2000)
        .withEndAction(new Runnable() {
            @Override
            public void run() {
                theView.setVisibility(View.GONE);
            }
        });

Enjoy :)

like image 20
Antzi Avatar answered Nov 03 '22 05:11

Antzi


Simply take your animation object and add animation listener to it. Here is the example code :

rotateAnimation.setAnimationListener(new 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) {
                // TODO Auto-generated method stub

**// WRITE HERE WHATEVER YOU WANT ON THE COMPLETION OF THE ANIMATION**


            }
        });
like image 10
Gaurav Arora Avatar answered Nov 03 '22 05:11

Gaurav Arora


You can also achieve this using Animation.setFillAfter

like image 9
Pavan Avatar answered Nov 03 '22 06:11

Pavan