Java 访问xml StateListDrawable中定义的动画
我的按钮有一个statelistdrawable,如下所示:Java 访问xml StateListDrawable中定义的动画,java,android,xml,animation,Java,Android,Xml,Animation,我的按钮有一个statelistdrawable,如下所示: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <rotate android:fromDegrees=
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<rotate
android:fromDegrees="90"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@drawable/spinner"
android:duration="1200"
android:repeatCount="infinite"/>
</item>
<item>
<rotate
android:fromDegrees="90"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@drawable/spinner"
android:duration="1200"
android:repeatCount="infinite"/>
</item>
</selector>
但是,如果动画位于可以启动的状态列表中,我如何访问它?我试过这样的方法,但现在我被卡住了:
StateListDrawable background = (StateListDrawable) mRecommendButton.getBackground();
Drawable drawable = background.getCurrent();
这给了我一个可绘制的,但不是动画
**编辑**
显然,它返回一个rotatedravable,所以我修改了代码,但它仍然没有旋转。我还使用getCompoundDrawables(),因为我正在将我的DrawableXML设置为Top。记录在案。它确实输入了“if语句”
固定的。我现在正在使用动画旋转
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/spinner_black_48"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="1080"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="infinite"/>
rotate标记定义RotatedRavable,而不是任何动画谢谢。那么我应该从setLevel开始吗?因为它没有旋转try background.setLevelNo仍然不工作。但我确实找到了另一种方法来让这个旋转的摇篮。编辑了我的问题,但对于RotatedRavable,没有什么比start()更好的了?因为RotatedRavable是一个可绘制的,它只是将可绘制的子对象旋转一个角度,它不会以任何方式设置动画
StateListDrawable background = (StateListDrawable) mRecommendButton.getCompoundDrawables()[1];
Drawable drawable = background.getCurrent();
if (drawable instanceof RotateDrawable) {
((RotateDrawable) drawable).setLevel(500);
}
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/spinner_black_48"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="1080"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="infinite"/>
mRecommendButton = (Button)v.findViewById(R.id.recommended_button);
StateListDrawable background = (StateListDrawable) mRecommendButton.getCompoundDrawables()[1]; // Drawable set as drawableTop in xml
Drawable drawable = background.getCurrent();
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}