对 LayerDrawable 内的所有 RotateDrawable 进行动画处理

Asa*_*afK 3 android android-animation android-drawable

我在 a 中有两个旋转可绘制对象layer-list,我试图将它们作为drawableleft按钮上的 a 进行动画处理。我显示了可绘制对象,但没有动画。

这是我在 xml ( button_progress_bar_small.xml) 中的可绘制布局:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <rotate
         android:drawable="@drawable/ic_spinner_small_outer"
         android:pivotX="50%"
         android:pivotY="50%"
         android:fromDegrees="0"
         android:toDegrees="1080" />
</item>
<item>
    <rotate
         android:drawable="@drawable/ic_spinner_small_inner"
         android:pivotX="50%"
         android:pivotY="50%"
         android:fromDegrees="720"
         android:toDegrees="0" />
</item>
Run Code Online (Sandbox Code Playgroud)

这是我正在运行的代码:

    button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.button_progress_bar_small, 0, 0, 0);
    LayerDrawable progressAnimationLeft = (LayerDrawable) button.getCompoundDrawables()[0];
    ((RotateDrawable) progressAnimationLeft.getDrawable(0)).setLevel(500);
    ((RotateDrawable) progressAnimationLeft.getDrawable(1)).setLevel(500);
Run Code Online (Sandbox Code Playgroud)

我不太确定动画开始的触发器是什么,或者我是否应该对每个执行某些操作RotateDrawable(与 上的一个操作相反LayerDrawable)来执行此操作。

Asa*_*afK 5

好的,我找到了解决我的问题的方法。

我将布局更改为:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/ic_spinner_small_outer"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="1080"
        android:interpolator="@android:anim/linear_interpolator"
        android:repeatCount="infinite"/>        
</item>
<item>
    <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/ic_spinner_small_inner"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="720"
        android:toDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:repeatCount="infinite"/>        
</item>
Run Code Online (Sandbox Code Playgroud)

并将代码更改为:

    button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.button_progress_bar_small, 0, 0, 0);
    LayerDrawable progressAnimationLeft = (LayerDrawable) button.getCompoundDrawables()[0];
    ((Animatable) progressAnimationLeft.getDrawable(0)).start();
    ((Animatable) progressAnimationLeft.getDrawable(1)).start();
Run Code Online (Sandbox Code Playgroud)

它并没有真正回答问题,但对我来说这是一个很好的解决方法,具有相同的动画效果。