How to chain animation in android to the same view?

Lukap picture Lukap · Jun 7, 2011 · Viewed 10.5k times · Source

I got some text views and I want to make the buzz effect of MSN.

My plan is:

  • take the view, let say 10dip to the left,
  • take it back to its start position
  • after that take it 10dip up
  • then back
  • down back
  • left... and so on.

My point is, I have some sequence of movements that I want to set to one view and that needs to execute one after another.

How can I do that?

Answer

pxsx picture pxsx · Jan 12, 2016

You probably mean AnimatorSet (not AnimationSet). As written in documentation:

This class plays a set of Animator objects in the specified order. Animations can be set up to play together, in sequence, or after a specified delay.

There are two different approaches to adding animations to a AnimatorSet: either the playTogether() or playSequentially() methods can be called to add a set of animations all at once, or the play(Animator) can be used in conjunction with methods in the Builder class to add animations one by one.

Animation which moves view by -100px for 700ms and then disappears during 300ms:

final View view = findViewById(R.id.my_view);

final Animator translationAnimator = ObjectAnimator
        .ofFloat(view, View.TRANSLATION_Y, 0f, -100f)
        .setDuration(700);

final Animator alphaAnimator = ObjectAnimator
        .ofFloat(view, View.ALPHA, 1f, 0f)
        .setDuration(300);

final AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(
        translationAnimator,
        alphaAnimator
);
animatorSet.start()