Search code examples
androidandroid-animationobjectanimator

Slide Right to Left using ObjectAnimation


I can do the slide left to right thanks to the sample code given in this YouTube link:

https://www.youtube.com/watch?v=3UbJhmkeSig

However I can't do the right to left animation. What I've tried is putting a '-' (negative) sign in the code below but it wont work and it gives me the error:

Operator '-' cannot be applied to 'android.util.Property

ObjectAnimator translateAnimation =
                ObjectAnimator.ofFloat(view, -View.TRANSLATION_X, 800);
        translateAnimation.start();
        translateAnimation.setRepeatCount(1);
        translateAnimation.setRepeatMode(ValueAnimator.REVERSE);

Any way to do this inside an Activity?


Solution

  • You should work on the value, while the property name should remain the same:

    ObjectAnimator translateAnimation =
                ObjectAnimator.ofFloat(view, View.TRANSLATION_X, -view.getWidth());
        translateAnimation.start();
        translateAnimation.setRepeatCount(1);
        translateAnimation.setRepeatMode(ValueAnimator.REVERSE);