2015-04-16 59 views
0

我在res/anim文件夾這兩個文件:是否有可能從另一個繼承anim xml資源並覆蓋屬性?

my_anim.xml

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

    <rotate 
     android:duration="3000" 
     android:interpolator="@android:anim/linear_interpolator" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:repeatCount="infinite" 
     android:repeatMode="restart" 
     android:toDegrees="360" /> 

</set> 

my_anim_faster.xml

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

    <rotate 
     android:duration="1000" 
     android:interpolator="@android:anim/linear_interpolator" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:repeatCount="infinite" 
     android:repeatMode="restart" 
     android:toDegrees="360" /> 
</set> 

兩者都是相同的動畫,只有速度(android:duration ) 變化。有沒有辦法縮短這段代碼?例如,由my_anim_faster繼承自my_anim並覆蓋android:duration屬性或類似的東西。

謝謝。

回答

1

有點介紹:XML資源不能被繼承。 xml方法使您能夠創建簡便快速的資源,如可繪製,動畫和佈局,但無法繼承它們。 Google添加了fragments,它允許您以某種方式擴展和重用佈局功能,但這不是繼承。在可繪製/動畫資源中,您可以重用其他資源,但不能擴展它們。所以如果你想重用一些邏輯,試着設計你的資源,這樣你可以在另一個邏輯中重用它們。

現在你的情況:沒有,沒有辦法通過xml做到這一點。使用xml您只能描述您的資源。您可以通過編程方式使用java代碼更改持續時間。

Animation myAnimation = ...; 
... 
myAnimation.setDuration(1000); 
+0

謝謝。我雖然有一種像'style' /'theme'資源中的'parent'屬性的方式。我會通過代碼來完成。 –

+1

歡迎,高興地看到我幫助:) –

1

據我所知,在創建xml動畫時不能繼承其他動畫,如創建佈局時如何「包含」佈局。

最好的辦法是以編程方式創建動畫(例如在一個AnimationUtils類中),當您調用它時將持續時間作爲參數提供。例如

MyAnim myAnim = new MyAnim(1000);