gpt4 book ai didi

java - 如何使用 AnimatorSet 播放多个动画?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:45:06 25 4
gpt4 key购买 nike

我正在使用 Sceneform 创建一个播放动画的 android 应用程序。我正在尝试使用 AnimatorSet 按顺序启动多个 Animator。该代码在尝试播放两个动画时完美运行,但每当我添加第三个动画时,前两个动画播放然后应用程序崩溃。

这是一段代码:

    List<Animator> animatorList = new ArrayList<Animator>();

AnimationData ad1 = JimRenderable.getAnimationData("Abm_09|A");

Animator a = new ModelAnimator(ad1, JimRenderable);
animatorList.add(a);

AnimationData ad2 = JimRenderable.getAnimationData("Abm_09|B");

a = new ModelAnimator(ad2, JimRenderable);
animatorList.add(a);

AnimationData ad3 = JimRenderable.getAnimationData("Abm_09|C");

a = new ModelAnimator(ad3, JimRenderable);
animatorList.add(a);

AnimatorSet as= new AnimatorSet();
as.playSequentially(animatorList);
as.start();

如何让它播放超过 2 个动画?

最佳答案

AnimatorSet public class, which extends Animator class, provides a mechanism to group animations together so that they run in relation to one another. You can set animations to play simultaneously, sequentially, or after a specified delay.

您的代码应如下所示:

AnimatorSet bouncer = new AnimatorSet();

bouncer.play(bounceAnim).before(squashAnim1);

// THESE FOUR ANIMATIONS PLAY SIMULTANEOUSLY
bouncer.play(squashAnim1).with(squashAnim2);
bouncer.play(squashAnim1).with(stretchAnim1);
bouncer.play(squashAnim1).with(stretchAnim2);

bouncer.play(bounceBackAnim).after(stretchAnim2);

ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
fadeAnim.setDuration(250);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(bouncer).before(fadeAnim);

animatorSet.start();

In this code squashAnim1 clip plays simultaneously with squashAnim2, stretchAnim1, and stretchAnim2.

这个例子解释了它是如何工作的:我们设置了一个 AnimatorSet 来播放:

  • anim1anim2 同时,
  • anim3anim2 完成时播放,
  • anim4anim3 完成时播放:

    AnimatorSet clips = new AnimatorSet();
    clips.play(anim1).with(anim2);
    clips.play(anim2).before(anim3);
    clips.play(anim4).after(anim3);

此外,您还可以使用动画监听器。他们可以在动画持续期间监听重要事件。这是一段代码:

ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
fadeAnim.setDuration(250);

fadeAnim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
balls.remove(((ObjectAnimator)animation).getTarget());
}
}

希望这对您有所帮助。

关于java - 如何使用 AnimatorSet 播放多个动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55681374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com