簡體   English   中英

是否可以從另一個繼承XML XML資源並覆蓋屬性?

[英]Is it possible to inherit an anim xml resource from another and overwrite attributes?

我在res/anim文件夾中有這兩個文件:

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屬性或類似的東西。

謝謝。

據我所知,在xml中創建動畫時無法繼承其他動畫,例如在創建布局時如何“包含”布局。

最好的選擇是以編程方式創建動畫(例如,在AnimationUtils類中),並在調用動畫時提供持續時間作為參數。 例如

MyAnim myAnim = new MyAnim(1000);

簡要介紹一下: XML資源無法繼承。 xml方法使您能夠創建便捷的資源,例如可繪制對象,動畫和布局,但無法繼承它們。 Google添加了一些fragments ,使您可以通過某種方式擴展和重用布局功能,但這不是繼承。 在可繪制/動畫資源中,您可以重用其他資源,但不能擴展它們。 因此,如果您想重用某些邏輯,請嘗試設計資源,以便可以在其他資源中重用它們。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM