【问题标题】:How to perform an action just after animation of AnimatorSet stops?如何在 AnimatorSet 的动画停止后立即执行操作?
【发布时间】:2014-11-22 19:42:57
【问题描述】:

我已将以下代码放入 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】:

    发件人: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);
                }
            }
        }
    

    【讨论】:

    • 但是,如何使用这种方法进行下一次迭代呢?你能简化一下吗.. @ziv
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      相关资源
      最近更新 更多