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

时间:2014-09-28 07:11:15

标签: android animation onclicklistener nineoldandroids

我已将以下代码放在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不起作用,直到在上面的代码中完成动画。

有没有办法做这些事情?

2 个答案:

答案 0 :(得分: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);
            }
        }
    }

答案 1 :(得分:0)

只需使用特定持续时间的处理程序即可。
代码:

int i=n;

new Handler().postDelayed(new Runnable() {
   @Override
   public void run() {
      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.start();
      
      //Action

      i--;
      if(i!=0){
         new Handler().postDelayed(this,t);
      }
   }
},t);