Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset view to original position after animation

Tags:

android

I'm animating a view and I want to reset the view to the original position after animation ended.

This is what I have:

rl2 is a relativeLayout

rl2.animate().translationX(-60).translationY(117).setDuration(2000);

I tried setting this but its not working:

rl2.clearAnimation();
like image 746
Jorge Requez Avatar asked Nov 08 '18 19:11

Jorge Requez


3 Answers

clearAnimation(); does not reset your animations, it just stops them and removes them from the animation queue. To undo your animations you need to actually undo them. So, for your code block you will need to call rl2.animate().translationX(0).translationY(0).setDuration(2000); to move the view back to its original position.

like image 128
Chris Stillwell Avatar answered Nov 19 '22 03:11

Chris Stillwell


As @Chris Stillwell mentioned on his answer, But you can move View back to it's original position after translation animation by

rl2.animate().translationX(0).translationY(0);
like image 9
Khaled Lela Avatar answered Nov 19 '22 01:11

Khaled Lela


I know this question was asked long time ago but someone may still look for answer. I use the Animation class for this kind of thing; there is a setFillAfter method in this class, so you can set if you want the animation to apply its transformation after it ends.

Animation anim = new ScaleAnimation(
            1f, 1f, // Start and end values for the X axis scaling
            1f, 2f, // Start and end values for the Y axis scaling
            Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
            Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
anim.setFillAfter(false); // no needed to keep the result of the animation
anim.setDuration(1000);
anim.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
                    
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});
gangsterImage.startAnimation(anim);
like image 1
Dawid Kubinkiewicz Avatar answered Nov 19 '22 02:11

Dawid Kubinkiewicz