Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverting animation done with AnimatorSet

I am creating an animation with AnimatorSet and when it ends I would like to leave the View where it was.

The code is something like this:

mLastAnimation = new AnimatorSet();
mLastAnimation.playTogether(
  ObjectAnimator.ofFloat(mImageView, "scaleX", 1.5f, 1f),
  ObjectAnimator.ofFloat(mImageView, "translationY", 40f, 0f));
mLastAnimation.setDuration(3000);
mLastAnimation.addListener(this);
mLastAnimation.start();

// The Activity implements the AnimatorListener interface
  @Override
  public void onAnimationEnd(Animator animator) {
    // Undo the animation changes to the view.
  }

EDIT:

I am using the new animation API so setFillAfter() will not work here.

like image 671
Macarse Avatar asked Jul 26 '12 13:07

Macarse


4 Answers

If you are using nineoldandroids there is a function called reverse. You can set the duration to be 0 and call reverse to reverse the animation.

like image 171
Josh Li Avatar answered Nov 15 '22 15:11

Josh Li


You have to set the properties of the View back to its original values.

For example if you translateY 40 pixels forward, you need to translateY 40 pixels backwards. This is because the actual properties of the View have changed during property animation, not just the way it is rendered.

http://developer.android.com/guide/topics/graphics/prop-animation.html#property-vs-view

like image 30
Che Jami Avatar answered Nov 15 '22 15:11

Che Jami


While using ObjectAnimator you can simply set

android:repeatMode="reverse"
like image 21
Datenshi Avatar answered Nov 15 '22 15:11

Datenshi


You can use this:

ObjectAnimator scaleX = ObjectAnimator.ofFloat(mImageView, "scaleX", 1.5f, 1f);
// here is the important part!
scaleX.setRepeatMode(ValueAnimator.REVERSE);
scaleX.setRepeatCount(1);
ObjectAnimator transl = ObjectAnimator.ofFloat(mImageView, "translationY", 40f, 0f));
// here is the important part!
transl.setRepeatMode(ValueAnimator.REVERSE);
transl.setRepeatCount(1);

mLastAnimation = new AnimatorSet();
mLastAnimation.playTogether(scaleX, transl);
// the rest is the same...
like image 27
nglauber Avatar answered Nov 15 '22 13:11

nglauber