如何在动画之间添加延迟

4
我在安卓中遇到了动画问题。我有一个名为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>

这样可以,但是在我的MainActivity中,我想一个接一个地开始动画。因此,我创建了一个方法使其更加简单,只需更改ImageView即可。
public void animation(ImageView imageView){
    animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation_char);
    imageView.startAnimation(animation);
}

为了连续播放动画,我试图使用AnimatorSet。但是我读到AnimatorSet只能与Animator一起使用,不能与Animation一起使用。所以我的问题是:是否有办法在animator中加载一个animation?还是说我应该用另一种方法来实现我想做的事情?提前感谢!

编辑 我改变了我的方法,现在我尝试这样做,但问题是所有的图像同时出现,如何在动画之间添加一些延迟?

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

            w.startAnimation(animation);
            a.startAnimation(animation);
            r.startAnimation(animation);     
}
4个回答

2

实际上,我在这个问题的回答中已经解释过了。你应该在第一个动画的AnimationListener的onAnimationEnd中开始你的第二个动画,第二个动画同理。


0

如果你使用 Kotlin,你可以使用以下方法:

doOnEnd {startDelay = 1000L
                    start() }

repeatCount = 1时,请看下面的示例:

 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()

0

你应该使用AnimationSet类,而不是AnimatorSet。

例如

AnimationSet as = new AnimationSet(true);
as.setFillEnabled(true);
as.setInterpolator(new BounceInterpolator());
as.addAnimation(firstAnim);
as.addAnimation(secondAnim);
as.setDuration(1000);
imageView.startAnimation(as);

但是AnimationSet没有任何playSequentially方法。 - Sourabh
它们是用于AnimatorSet而不是AnimationSet - Sourabh
尝试使用 firstAnim 和 secondAnim 的 setStartOffset 和 setDuration 方法来进行操作。 - karvoynistas
@karvoynistas,不起作用。我有三张图片,想一个接一个地显示。我卡在这一点上了!我编辑了我的问题。 - CodeAlo

0

我把这个放在这里,也许能帮到某些人...

我做了类似于这样的事情。我有3张图片,我想让它们一个接一个地掉落。

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);

设置初始可见性为不显示

接着创建3个动画

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);

下一步是添加@Divers添加的事件监听器:
 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) {

            }
        });

就是这样了

我的动画xml大概长这样

<?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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接