如何在AnimatorSet动画停止后执行动作?

问题描述 投票:0回答:1

我已将以下代码放在for循环中:

set8.playTogether(
       ObjectAnimator.ofFloat(ball4, "translationX", x1, xn),
       ObjectAnimator.ofFloat(ball4, "translationY", y1, yn),
       ObjectAnimator.ofFloat(ball8, "translationX", xn, x1),
       ObjectAnimator.ofFloat(ball8, "translationY", yn, y1)
);
set8.setDuration(t).start();

在for循环的每次迭代之前,我想等待上一次迭代的动画完成。有没有办法做到这一点?

在我的项目中,我在图像上有一个onclick监听器。我还希望onclicklistener无效,直到在上面的代码中完成动画。

有没有办法做这些事情?

android animation onclicklistener nineoldandroids
1个回答
0
投票

来自:http://developer.android.com/reference/android/animation/Animator.AnimatorListener.html

您可以在动画中添加Animator.AnimatorListener:

set8.addListener(new AnimatorListenerAdapter() {
    public void onAnimationEnd(Animator animation) {
        // animation is done, handle it here
    }

你可以在这里找到另一个例子:How to position a view so the click works after an animation

编辑:您正在寻找的完整示例:

int totalSteps = 8;
    for (int i=0; i<totalSteps; i++) {
        // .. create the animation set
        set8.addListener(new MyAnimatorListenerAdapter(i, new onAnimationStpeDoneListener(){
            @Override
            public void onAnimationStepDone(int step) {
                if (step < totalSteps) {
                    // start the next animation
                } else {
                    // all steps are done, enable the clicks... 
                }
            }
        }));
    }
    public interface onAnimationStpeDoneListener {
        public void onAnimationStepDone(int step);
    }

    public class MyAnimatorListenerAdapter extends AnimatorListenerAdapter {
        private int mStep;
        private onAnimationStpeDoneListener mDelegate;

        public MyAnimatorListenerAdapter(int step, onAnimationStpeDoneListener listener) {
            mStep = step;
            mDelegate = listener;
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            if (mDelegate != null) {
                mDelegate.onAnimationStepDone(mStep);
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.