如何使用AnimatorSet播放多个动画? [英] How to play multiple Animations using AnimatorSet?
问题描述
我正在使用 Sceneform
创建一个可播放动画的android应用.我正在尝试使用 AnimatorSet
顺序启动多个Animator.尝试播放两个动画时,代码可以完美运行,但是每当我添加第三个动画时,前两个动画就会播放,然后应用崩溃.
I'm using Sceneform
to create an android app that plays animation. I'm trying to start multiple Animators sequentially using AnimatorSet
. The code works perfectly when trying to play two animations, but whenever I add a 3rd animation, the first two animations play then the app crashes.
下面是一段代码:
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个动画?
How can I let it play more than just 2 animations?
推荐答案
AnimatorSet
公共类(扩展了Animator
类)提供了一种将动画分组在一起的机制,以使它们彼此相对运行.您可以设置动画在指定的延迟时间后同时,顺序或播放.
AnimatorSet
public class, which extendsAnimator
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();
在此代码中,
squashAnim1
剪辑与squashAnim2
,stretchAnim1
和stretchAnim2
同时播放.
这个示例说明了它的工作方式:我们设置了一个 AnimatorSet
进行播放:
And this example explains how it works: we set up an AnimatorSet
to play:
-
anim1
和anim2
同时 -
anim3
在anim2
完成时播放, -
和
anim4
在anim3
完成时播放:
anim1
andanim2
at the same time,anim3
to play whenanim2
finishes,and
anim4
to play whenanim3
finishes:
AnimatorSet clips = new AnimatorSet();
clips.play(anim1).with(anim2);
clips.play(anim2).before(anim3);
clips.play(anim4).after(anim3);
此外,您可以使用动画侦听器
.他们可以在动画播放期间收听重要事件.这是一个看起来像这样的代码:
Also, you might use Animation listeners
. They can listen for important events during an animation's duration. Here's a code how it looks like:
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());
}
}
希望这会有所帮助.
这篇关于如何使用AnimatorSet播放多个动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!