2012-06-17 63 views
1

我想爲28個不同的視圖分配28個不同的旋轉動畫,並在活動開始時啓動它們。動畫應該都有隨機的startOffset和Duration。 我試過這段代碼,但似乎所有的動畫都有相同的值。在多個ImageView上旋轉動畫

RotateAnimation rotate = new RotateAnimation(0.0f, 360.0f, 
     Animation.RELATIVE_TO_SELF, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f); 

ImageView imageView; 

Random r = new Random(); 
int delayOffset = 0; 
int rotationDuration = 200; 


for (int i = 0; i < ids.length; i++) { 

    rotate.reset(); 

    imageView = (ImageView) findViewById(ids[i]); 
    imageView.clearAnimation(); 

    delayOffset = r.nextInt(500 - 0); 
    rotationDuration = r.nextInt(10000 - 200) + 200; 


    rotate.setStartOffset(delayOffset); 
    rotate.setDuration(rotationDuration); 


    imageView.startAnimation(rotate); 
} 

我在做什麼錯?

+0

嘗試在您的for循環中創建一個RotateAnimation的新實例 – L7ColWinters

回答

0

您正在使用完全相同的動畫實例,因此所有視圖都以最新的值開始。

您應該爲每個視圖創建一個全新的動畫。

+0

聽起來像它會工作。我會在今天晚些時候嘗試。但對我來說,使用28個動畫實例似乎有點矯枉過正 - 當我只需要更改2個屬性時。 – Peter

+0

它只是純數據類,所以沒關係。如果您的目標僅限於API 11及以上版本,則可以使用新的動畫API,這可能更容易使用,儘管我從未嘗試過。它應該更快更平滑,因爲它使用GPU。 –