0

Can one undo the changes he made on View properties using animate() on it?

In particular, how to undo changes made using animate().yBy(x)?

Note that I tried using animate().yBy(-x) and it works most of the times, but there are times that for some reason animate().yBy(x) seem not to be completed correctly (especially when the fragment pauses and then resumed) so animate().yBy(-x) is over-moving the view.

I'm looking for a way to make the View reset its properties to the way they were before I changed them using animate().

Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
  • 2
    Instead of `view.animate().yBy(-x)`, try `view.animate().yBy(-1.0f*view.getTranslationY())`. – CommonsWare Nov 29 '15 at 19:45
  • @CommonsWare Although this isnt answering the question in general, This seem to be in the right direction for what i need, please post an answer so i can mark it. Thank you. – Ofek Ron Nov 29 '15 at 19:56

2 Answers2

3

xBy() and yBy() animations affect the translationX and translationY properties. You can get the current values of those properties via getTranslationX() and getTranslationY(). So, to undo the previous animations, multiply the current property values by -1 and animate those. Or if you are seeking a "smash cut" jump (no animation), just call setTranslationX(0) or setTranslationY(0).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

By using interpolator we can inverse the animation:

public class InverAnim implements Interpolator {
    @Override
    public float getInterpolation(float paramFloat) {
        return Math.abs(paramFloat -1f);
    }
}

On your animation you can set, new interpolator:

myAnimation.setInterpolator(new InverAnim());
Androider
  • 3,833
  • 2
  • 14
  • 24