Sumit Shukla
Sumit Shukla

Reputation: 4494

Sequential animations doesn't start

MyCode:

    ActivityWelcomeNewBinding binding;
    Animation rotate1, rotate2;
    AnimationSet animationSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_welcome_new);
        startAnim();
    }

    private void startAnim() {
        animationSet = new AnimationSet(true);
        animationSet.setFillEnabled(true);

        rotate1 = AnimationUtils.loadAnimation(this, R.anim.rotate_1);
        rotate1.setDuration(2000);
        rotate1.setStartOffset(1000);

        rotate2 = AnimationUtils.loadAnimation(this, R.anim.rotate_2);
        rotate2.setDuration(2000);
        rotate2.setStartOffset(1000);

        animationSet.addAnimation(rotate1);
        animationSet.addAnimation(rotate2);
        animationSet.setRepeatMode(Animation.INFINITE);

        binding.myView.startAnimation(animationSet);
    }
}

rotate_1.xml:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1200"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="0"
    android:toDegrees="360" />

rotate_2.xml:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1200"
    android:fromDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="0"
    android:toDegrees="0" />

Upvotes: 0

Views: 42

Answers (1)

CodeWithVikas
CodeWithVikas

Reputation: 1443

AnimationSet is meant to play all animation together.

AnimationSet Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. AnimationSet

So if you want to play child animations sequentially you have to start 2nd animation after finishing 1st one like :

rotate2.setStartOffset(duration_of_rotate1)

As i understood, you want to animate your object from 0 to 360 and 360 to 0 infinite times.

You can do this without AnimationSet with AnimationListener. Set AnimationListener on each child.

Give a try of something like this :

rotate1 = AnimationUtils.loadAnimation(this, R.anim.rotate_1);
rotate1.setDuration(2000);

rotate2 = AnimationUtils.loadAnimation(this, R.anim.rotate_2);
rotate2.setDuration(2000);

    rotate1.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    }
    @Override
    public void onAnimationEnd(Animation animation) {
        imageView.startAnimation(rotate2);
    }
    @Override
    public void onAnimationRepeat(Animation animation) {
    }
});

rotate2.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    }
    @Override
    public void onAnimationEnd(Animation animation) {
            imageView.startAnimation(rotate1);
    }
    @Override
    public void onAnimationRepeat(Animation animation) {
    }
});

imageView.startAnimation(rotate1);

Upvotes: 1

Related Questions