我的按钮有一个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="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>
现在我的按钮(以xml声明)将此文件设置为drawableTop。
通常我会在res / anim中声明旋转部分,然后我可以用
开始动画Animation a = AnimationUtils.loadAnimation(getContext(), R.anim.spinner);
view.startAnimation(a);
但是,如果动画在状态列表中可绘制以使其启动,我该如何访问动画?我尝试过这样的事情,但现在我被困了:
StateListDrawable background = (StateListDrawable) mRecommendButton.getBackground();
Drawable drawable = background.getCurrent();
这给了我一个可绘制但不是动画。
**编辑**
显然它返回一个RotateDrawable,所以我提示我的代码,但它仍然没有旋转。我也使用getCompoundDrawables(),因为我将我的drawable xml设置为Top。仅供记录。它确实输入了&#34; if语句&#34;。
StateListDrawable background = (StateListDrawable) mRecommendButton.getCompoundDrawables()[1];
Drawable drawable = background.getCurrent();
if (drawable instanceof RotateDrawable) {
((RotateDrawable) drawable).setLevel(500);
}
答案 0 :(得分:0)
固定。我现在使用动画旋转
<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();
}