2013-10-03 223 views
1

我想知道是否有人可以幫助我,我一直在尋找有用的幫助,但什麼也沒找到。我有四個View元素(Button s),我需要按照隨機順序對它們進行一個接一個的動畫處理。我試過等待Animation.hasEnded(),這隻會凍結整個應用程序。此外,我試圖等待AnimationListeneronAnimationEnd()更改布爾值,但也凍結了應用程序。 Thread.sleep()SystemClock.sleep()等待都給出了相同的結果。請,有人可以幫我嗎?等待Android動畫完成

+0

你可以給我們更多關於如何等待動畫的方式的代碼嗎?我不認爲你需要在onAnimationEnd()中調用thread.sleep()或systemclock.sleep。 –

回答

2

以下是我會做:

首先,創建一個成員的隨機按鈕配置:

private Button[] mRandomButtonsOrder; 

然後,初始化隨機按鈕順序:

List<Button> myButtons = new ArrayList<Button>(); 

     myButtons.add(btn1); // Add all your buttons to this array. 
     myButtons.add(btn2); 
     myButtons.add(btn3); 
     myButtons.add(btn4); 

     mRandomButtonsOrder = new Button[myButtons.size()]; // This is a member of the activity! 

     Random random = new Random(); 
     int index; 

     for (int i = 0; i < myButtons.size(); i++) 
     { 
      do 
      { 
       index = random.nextInt() % mRandomButtonsOrder.length; 
      } while (mRandomButtonsOrder[index] != null); 

      mRandomButtonsOrder[index] = myButtons.get(0); 
      myButtons.remove(0); 
     } 

     initiateAnimationOnButton(0); 

現在,這裏是initateAnimationOnButton方法:

private void initiateAnimationOnButton(final int buttonIndex) 
    { 
     TranslateAnimation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta); // Just a sample using TranslateAnimation 
     animation.setDuration(1000); 

     if (buttonIndex < mRandomButtonsOrder.length - 1) 
     { 
      animation.setAnimationListener(new TranslateAnimation.AnimationListener() 
      { 

       @Override 
       public void onAnimationStart(Animation animation) { } 

       @Override 
       public void onAnimationRepeat(Animation animation) { } 

       @Override 
       public void onAnimationEnd(Animation animation) 
       { 
        initiateAnimationOnButton(buttonIndex + 1);       
       } 
      }); 
     } 

     Button btn = mRandomButtonsOrder[buttonIndex]; 
     btn.startAnimation(animation); 
    } 

希望這有助於:)

+0

完全解決了,謝謝! – drRoflol

+0

很高興我能幫忙:) –