I have a question concerning ObjectAnimator in Android. I am trying to model a Bounce effect whereby the View slides up (decreasing Y value) and back down after by the same amount 'n', followed by the View sliding up and down again but this time by 'n/2' (so half the distance).
So a bigger bounce, followed by a shallower bounce - i.e., the kinda thing a Mac icon does in the tray when it wants your attention.
Here is what I've tried so far (assume v
is a View
):
float y = v.getTranslationY(),distance = 20F;
AnimatorSet s = new AnimatorSet();
s.play(ObjectAnimator.ofFloat(v, "translationY", y- distance).setDuration(500))
.before(ObjectAnimator.ofFloat(v, "translationY", y).setDuration(500))
.before(ObjectAnimator.ofFloat(v, "translationY", y- (distance/2)).setDuration(500))
.before(ObjectAnimator.ofFloat(v, "translationY", y).setDuration(500));
s.start();
Ignore the code quality, it's a POC! I was hoping this would work, but it seems to only 'bounce' once as if its combined the animations despite the use of .before()
.
Could you please show me how I can create complex AnimatorSet chains that do not amalgamate in to one, as I seem to be missing something?
BONUS: For extra points, how can I set the repeat of an AnimatorSet?
Many thanks!