是否可以从另一个继承anim xml资源并覆盖属性?

时间:2015-04-16 11:24:21

标签: android xml android-animation

我在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属性或类似内容。

感谢。

2 个答案:

答案 0 :(得分:1)

据我所知,在xml中创建动画时你不能继承其他动画,就像你可以&#34;包含&#34;创建布局时的布局。

您最好的选择是以编程方式创建动画(例如,在AnimationUtils类中),当您调用它时,将持续时间作为参数提供。 e.g。

MyAnim myAnim = new MyAnim(1000);

答案 1 :(得分:1)

一点简报:XML资源无法继承。 xml方法使您能够创建简单快速的资源,如drawable,动画和布局,但无法继承它们。 Google添加了fragments,允许您以某种方式扩展和重用布局功能,但这不是继承。在drawable / animation资源中,您可以重用其他资源,但不能扩展它们。因此,如果您想重用某些逻辑,请尝试设计您的资源,以便您可以在其他资源中重复使用它们。

现在针对您的情况:不,没有办法通过xml执行此操作。使用xml,您只能描述您的资源。您可以使用编程方式java代码更改持续时间。

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