如何使用nineoldandroid结束AnimatorSet?

时间:2014-07-04 11:47:40

标签: android nineoldandroids

我正在使用nineoldandroid进行动画制作。

我的代码如下:

Button mTarget;
Animator mTargetAnim;
AnimatorSet mTargetScaleAnimSet;

private void createAnimation() {
    if (mTargetAnim == null) {
        ObjectAnimator yAnim = ObjectAnimator.ofFloat(mTarget, "y",
                mTarget.getY(), mTarget.getY() + 500).setDuration(3500);
        yAnim.setRepeatCount(0);
        yAnim.setRepeatMode(ValueAnimator.REVERSE);
        yAnim.setInterpolator(new AccelerateInterpolator(2f));
        yAnim.addListener(this);

        ObjectAnimator xAnim = ObjectAnimator.ofFloat(mTarget, "x",
                mTarget.getX(), mTarget.getX() + 500).setDuration(3500);
        xAnim.setStartDelay(0);
        xAnim.setRepeatCount(0);
        xAnim.setRepeatMode(ValueAnimator.REVERSE);
        xAnim.setInterpolator(new AccelerateInterpolator(2f));
        // the first AnimatorSet
        AnimatorSet xyAnimSet = new AnimatorSet();
        xyAnimSet.playTogether(yAnim, xAnim);

        mTargetScaleAnimSet = new AnimatorSet();
        ObjectAnimator scaleXAnim = ObjectAnimator.ofFloat(mTarget,
                "scaleX", 1f, 0f, 0.2f, 0f);
        ObjectAnimator scaleYAnim = ObjectAnimator.ofFloat(mTarget,
                "scaleY", 1f, 0f, 0.2f, 0f);
        mTargetScaleAnimSet.playTogether(scaleXAnim,
                scaleYAnim);
        mTargetScaleAnimSet.setDuration(3000);
        mTargetScaleAnimSet.addListener(this);


        mTargetAnim = new AnimatorSet();
        ((AnimatorSet) mTargetAnim).playSequentially(xyAnimSet, mTargetScaleAnimSet);

        mTargetAnim.addListener(this);
    }
}

在第二个Animator(mTargetScaleAnimSet)开始之前,我调用end()方法来结束整个AnimatorSet。但是,我发现end()方法不能结束AnimatorSet中的所有子Animators。

问:如何让所有儿童动画师顺序播放?

1 个答案:

答案 0 :(得分:0)

经过一番头脑风暴,我找到了一个解决方案如下:

<强>首先

mScaleAnimSet.setStartDelay(3500);

,然后

mTargetAnimSet.playSequentially(mTransAnimSet, mScaleAnimSet);

更改为:

mTargetAnimSet.playTogether(mTransAnimSet, mScaleAnimSet );

我认为关键是如果动画未启动,则无法结束。