How to add delay between animations
Asked Answered
C

4

4

I'm having a trouble with animations in android. I have my animation_char.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
    android:duration="300"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="1.0"/>
</set>

That is ok, but in my MainActivity I want to start an animation one after one. So I created a method to make it more easy and just change the ImageView

public void animation(ImageView imageView){
    animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation_char);
    imageView.startAnimation(animation);
}

And for make consecutives animations, I'm trying to use AnimatorSet. But as I read AnimatorSet works with Animator, not with Animation. So my question is: is there a way to load an animation in a animator?? Or should I use another way in order to achieve what I want to do? Thanks in advance!

EDIT I changed my method and now Im trying with this but the problem is that all the images appear at the same time, how can I add some delay between animations?

public void animation() {
    animation= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation_char);

            w.startAnimation(animation);
            a.startAnimation(animation);
            r.startAnimation(animation);     
}
Cunning answered 29/11, 2015 at 20:44 Comment(0)
M
2

Actually I've answered on this question here. You should start your second animation in onAnimationEnd of AnimationListener of first animations. The same for second one.

Morten answered 1/12, 2015 at 19:36 Comment(0)
L
0

You should use AnimationSet class instead of AnimatorSet.

For instance

AnimationSet as = new AnimationSet(true);
as.setFillEnabled(true);
as.setInterpolator(new BounceInterpolator());
as.addAnimation(firstAnim);
as.addAnimation(secondAnim);
as.setDuration(1000);
imageView.startAnimation(as);
Leavitt answered 29/11, 2015 at 20:49 Comment(4)
But AnimationSet doesn't have any playSequentially methodLianeliang
They are for AnimatorSet not AnimationSetLianeliang
so,try to play with setStartOffset and setDuration methods of firstAnim and secondAnimLeavitt
Doesnt work @karvoynistas, I have three images and I want to display one after one. I'm stuck in this point! I edited my questionCunning
P
0

I put this here maybe it helps someone...

I did something like this. I have 3 images that I want to fall one after the other.

note1 = findViewById(R.id.note1);
note1.setVisibility(View.INVISIBLE);
note2 = findViewById(R.id.note2);
note2.setVisibility(View.INVISIBLE);
note3 = findViewById(R.id.note3);
note3.setVisibility(View.INVISIBLE);

Setting visibility so that they are not shown initially

And then create 3 animations

Animation slideDown = AnimationUtils.loadAnimation(this, R.anim.slide_down);
        note1.startAnimation(slideDown);

final Animation slideDown2 = AnimationUtils.loadAnimation(SplashScreen.this, R.anim.slide_down);
    
final Animation slideDown3 = AnimationUtils.loadAnimation(SplashScreen.this, R.anim.slide_down);

Next step is to add the event listeners that @Divers added:

 slideDown.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                note1.setVisibility(View.VISIBLE);
                note2.startAnimation(slideDown2);
                slideDown2.setAnimationListener(new Animation.AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        note3.startAnimation(slideDown3);
                        note2.setVisibility(View.VISIBLE);
                        slideDown3.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                note3.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

And that's all

My animation xml is something like

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="800"
        android:fromXDelta="0%"
        android:fromYDelta="-50%p"
        />

    <alpha
        android:duration="500"
        android:fromAlpha="0.1"
        android:toAlpha="1.0"
        />
</set>
Purvis answered 21/4, 2021 at 22:51 Comment(0)
R
0

If you use Kotlin, you can use this way:

doOnEnd {startDelay = 1000L
                    start() }

Where repeatCount = 1 look at this example:

 val animations = arrayOf(-140f).map { translation ->
            ObjectAnimator.ofFloat(binding.viewYatch, "translationX", translation).apply {
                     //Update
                duration = 800
                repeatCount = 1
                repeatMode = ObjectAnimator.RESTART
                doOnEnd {
                    startDelay = 1000L
                    start()
                }
            }
        }

        val set = AnimatorSet()
        set.playTogether(animations)
        set.start()
Roos answered 11/4, 2022 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.