12

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Lukap
  • 31,523
  • 64
  • 157
  • 244

3 Answers3

30

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()
floydheld
  • 314
  • 2
  • 11
pawel-schmidt
  • 1,125
  • 11
  • 15
0

I have the beginnings of an sdk 15 compatible class that can be used to build complex animation chains hope it helps someone. You should be able to follow the design pattern to add your own methods. If you do please comment them here and I will update the answer, Cheers!

package com.stuartclark45.magicmatt.util;

import java.util.LinkedList;
import java.util.List;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;

    /**
     * Used to build complex animations for a view. Example usage bellow makes view move out to the
     * right whilst rotating 45 degrees, then move out to the left.
     *
     * {@code
     *  int rotateDuration = 200;
     *  int rotation = 45;
     *  new AnimationBuilder(view)
     *    .translationX(100, rotateDuration)
     *    .rotateTo(rotation, rotateDuration)
     *    .then()
     *    .translationX(-200, rotateDuration)
     *    .start();
     * }
     *
     * @author Stuart Clark
     */

    public class AnimationBuilder {

      private View view;
      private List<Animator> setsList;
      private List<Animator> buildingList;

      public AnimationBuilder(View view) {
        this.view = view;
        this.setsList = new LinkedList<>();
        this.buildingList = new LinkedList<>();
      }

      public AnimationBuilder rotateTo(float deg, long duration) {
        buildingList.add(ObjectAnimator.ofFloat(view, "rotation", deg).setDuration(duration));
        return this;
      }

      public AnimationBuilder translationX(int deltaX, long duration) {
        buildingList.add(ObjectAnimator.ofFloat(view, "translationX", deltaX).setDuration(duration));
        return this;
      }

      public AnimationBuilder translationY(int deltaX, long duration) {
        buildingList.add(ObjectAnimator.ofFloat(view, "translationY", deltaX).setDuration(duration));
        return this;
      }

      public AnimationBuilder then() {
        createAniSet();
        // Reset the building list
        buildingList = new LinkedList<>();
        return this;
      }

      public void start() {
        createAniSet();
        AnimatorSet metaSet = new AnimatorSet();
        metaSet.playSequentially(setsList);
        metaSet.start();
      }

      private void createAniSet() {
        AnimatorSet aniSet = new AnimatorSet();
        aniSet.playTogether(buildingList);
        setsList.add(aniSet);
      }


    }
Stuart Clark
  • 611
  • 8
  • 13
-2

Use an AnimationSet:

AnimationSet set = new AnimationSet(true);

Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(100);
set.addAnimation(animation);

animation = new TranslateAnimation(
    Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
    Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(500);
set.addAnimation(animation);

view.startAnimation( set );
Pang
  • 9,564
  • 146
  • 81
  • 122
Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • 4
    What??? This is wrong, animationSet animates its child animations together. "AnimationSet: Represents a group of Animations that should be played together." – Simon Apr 01 '15 at 11:52
  • 8
    AnimationSet performs animations asynchronously. To achieve the desired effect, you need to use AnimationSet.setStartOffset() in the second animation – Aaron Dougherty Aug 29 '15 at 04:38
  • 1
    This answer is not correct. Animations will launch simulatneously. – angryITguy Jul 19 '16 at 00:55
  • 2
    I agree, this answer is just wrong. But I can't delete it as it has been accepted... @Lukap, please un-accept it. – Snicolas Jul 21 '16 at 18:19
  • Just tried AnimationSet and doesn't chain animation. They are executed at the same time. This answer is wrong. – enricmacias Dec 19 '16 at 05:24
  • Can you at least modify it? It's really confusing. – Cԃաԃ Dec 22 '17 at 00:46
  • I can't delete it neither as it has been accepted... @Lukap, please un-accept it and ping me. – Snicolas Jan 25 '18 at 15:38
  • This is indeed perfectly usable: ` animDown.duration = 100; animUp.duration = 300; animUp.startOffset = animDown.duration; val set = AnimationSet(true); set.addAnimation(animDown); set.addAnimation(animUp); view.startAnimation(set);` – Jeremy Cochoy May 01 '20 at 18:06