我在layer-list
中有两个旋转绘图,我试图将它们设置为按钮上的drawableleft
。我正在显示drawables但没有动画。
这是我在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
上的一个操作相反)才能执行此操作。
答案 0 :(得分:3)
我将布局更改为:
<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();
它并没有真正回答这个问题,但对我来说这是一个很好的解决方法,同样的动画效果。
答案 1 :(得分:0)
您必须为级别值设置动画,范围从0到10000。
button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.button_progress_bar_small, 0, 0, 0);
LayerDrawable progressAnimationLeft = (LayerDrawable) button.getCompoundDrawables()[0];
ObjectAnimator.ofInt((RotateDrawable) progressAnimationLeft.getDrawable(0), "level", 0, 10000).start();
ObjectAnimator.ofInt((RotateDrawable) progressAnimationLeft.getDrawable(1), "level", 0, 10000).start();