0

I have placed the following code in a for loop:

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();

Before every iteration of the for loop, I want to wait for the animation of previous iteration to complete. Is there any way to do that?

In my project, I have an onclick listener on an image. I also want that onclicklistener to be nonfunctional until the animation is completed in the above code.

Is there any method to do these things?

4

2 回答 2

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);
            }
        }
    }
于 2014-09-28T07:26:06.667 回答
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);
于 2020-09-03T04:36:19.253 回答