Android 像多米诺骨牌一样的动画

Android animation like domino

我想制作类似多米诺骨牌效应的动画。 但是当我使用动画 xml 和带有 for 循环的处理程序时,它们会同时一起工作。

如何制作多米诺骨牌效果动画,例如在第一个按钮 (mon) 启动后 100 毫秒开始下一个按钮 (tue) 的动画?

这里是我的处理方法

private void startAnimation(final ArrayList<View> vArr)
{
    mon = (Button)findViewById(R.id.mon);
    tue = (Button)findViewById(R.id.tue);
    wed = (Button)findViewById(R.id.wed);
    thu = (Button)findViewById(R.id.thu);
    fri = (Button)findViewById(R.id.fri);
    sat = (Button)findViewById(R.id.sat);
    sun = (Button)findViewById(R.id.sun);

    final Button[] weekDayList = {mon, tue, wed, thu, fri, sat, sun};

    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);

            switch (msg.what)
            {
                case 3:
                    for (int i = 0; i < weekDayList.length; i++)
                        weekDayList[i].startAnimation(buttonDown);

                    sendEmptyMessageDelayed(4, 100);

                    break;

                case 4:

                    break;
            }
        }
    };

    handler.sendEmptyMessage(3);
}

我的动画xml在这里

<set xmlns:android="http://schemas.android.com/apk/res/android">

<scale
    android:fromXScale="1.0"
    android:toXScale="1.0"
    android:fromYScale="1.0"
    android:toYScale="0.0"
    android:pivotX="50%"
    android:pivotY="100%"
    android:duration="500" />
</set>

为动画设置AnimationListener。使用 onAnimationEnd() 开始下一个动画。

您可以使用 ObjectAnimator(自 API 11 起)执行此操作,并通过将动画全部添加到 AnimatorSet.

来顺序播放动画
  public void dominoAnimations() {
    ObjectAnimator monAnimation = ObjectAnimator.ofFloat(mon, "scaleY", 0f);
    ObjectAnimator tueAnimation = ObjectAnimator.ofFloat(tue, "scaleY", 0f);
    ObjectAnimator wedAnimation = ObjectAnimator.ofFloat(wed, "scaleY", 0f);
    ObjectAnimator thuAnimation = ObjectAnimator.ofFloat(thu, "scaleY", 0f);
    ObjectAnimator friAnimation = ObjectAnimator.ofFloat(fri, "scaleY", 0f);
    ObjectAnimator satAnimation = ObjectAnimator.ofFloat(sat, "scaleY", 0f);
    ObjectAnimator sunAnimation = ObjectAnimator.ofFloat(sun, "scaleY", 0f);

    AnimatorSet dominoes = new AnimatorSet();
    dominoes.playSequentially(
      monAnimation, tueAnimation, wedAnimation,
      thuAnimation, friAnimation, satAnimation, sunAnimation
    );

    dominoes.setDuration(500);
    dominoes.start();
  }