2013-02-12 42 views
20

所以,我想同時旋轉一些視圖,所有視圖都使用相同的旋轉規格。問題在於,由於某些原因,第二個元素的旋轉行爲不同。顯然這與動畫對象實際上改變這兩行代碼之間的狀態有關。很顯然,我可以只創建一個單獨的動畫對象,並應用它,但我覺得還有一個更簡單的方法(我有大約15次)將一個動畫同時應用於多個視圖

僅使第一種觀點正確:

Animation rotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotationtoportrait); 
target.startAnimation(rotateAnim); 
lightBtn.startAnimation(rotateAnim); 

旋轉都能夠正確

Animation rotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotationtoportrait); 
Animation rotateAnim2 = AnimationUtils.loadAnimation(this, R.anim.rotationtoportrait); 
target.startAnimation(rotateAnim); 
lightBtn.startAnimation(rotateAnim2); 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="-90" 
    android:toDegrees="0" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="500" android:fillAfter="true"> 

一有沒有任何想法?

+1

嘗試使用新的動畫API(向後兼容性使用NineOldAndroids)。另外我建議觀看此視頻:http://www.youtube.com/watch?v = _UWXqFBF86U – Leandros 2013-02-12 20:59:45

+0

喜歡視頻!但是據我所知,什麼新的API是視頻中的人做的?(至少在最後) – Jameo 2013-02-12 21:08:01

+1

錯誤的視頻,對不起。 ;)http://www.youtube.com/watch?v=3UbJhmkeSig – Leandros 2013-02-12 21:10:16

回答

5

所以我想這恰恰是不可能的,所以我創建了一個輔助方法,只是將相同的動畫視圖列表:

public void doRotations(ArrayList<View> views, int start, int end, int xprop, float xscale, int yprop, float yscale, int duration, Boolean fillAfter){ 

    for(int i = 0; i < views.size(); i++){ 
     RotateAnimation temp = new RotateAnimation(start, end, xprop, xscale, yprop, yscale); 
     temp.setDuration(duration); 
     temp.setFillAfter(fillAfter); 
     views.get(i).startAnimation(temp); 
    } 
} 

絕對是一個黑客,但我想這就是所有我米能夠馬上做

+0

這的確可以並行運行動畫,但如果仔細觀察(因爲您始終觸發動畫),則可以在動畫中看到略微延遲。 – Mercury 2017-02-12 00:22:50

7

做這樣的:

ObjectAnimator anim = ObjectAnimator.ofFloat(view, "y", 100f); 
arrayListObjectAnimators.add(anim); 

ObjectAnimator anim1 = ObjectAnimator.ofFloat(view, "x", 0f); 
arrayListObjectAnimators.add(anim1); 

ObjectAnimator[] objectAnimators = arrayListObjectAnimators.toArray(new ObjectAnimator[arrayListObjectAnimators.size()]); 
AnimatorSet animSetXY = new AnimatorSet(); 
animSetXY.playTogether(objectAnimators); 
animSetXY.duration(1000); 
animSetXY.start(); 
+3

這並沒有回答這個問題,如果我是正確的,你正在對同一視圖應用2個動畫。原始帖子想要將相同的動畫應用於2個視圖 – 2014-09-25 14:10:51

相關問題