其他分享
首页 > 其他分享> > android-动画LayerDrawable中的所有RotateDrawables

android-动画LayerDrawable中的所有RotateDrawables

作者:互联网

我在图层列表中有两个旋转可绘制对象,并且试图将它们都设置为按钮上的可绘制对象的动画.我正在显示可绘制对象,但没有动画.

这是我在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>

这是我正在运行的代码:

    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);

我真的不确定动画启动的触发因素是什么,或者我是否应该在每个RotateDrawable上执行某些操作(与LayerDrawable上的一个操作相反)才能做到这一点.

解决方法:

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

我将布局更改为:

<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>

并将代码更改为:

    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();

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

标签:android-animation,android-drawable,android
来源: https://codeday.me/bug/20191123/2064108.html