我的纽扣有这样的状态图:
<?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(),因为我正在将可绘制的xml设置为Top。只是为了记录下。它确实输入了"if语句“。
StateListDrawable background = (StateListDrawable) mRecommendButton.getCompoundDrawables()[1];
Drawable drawable = background.getCurrent();
if (drawable instanceof RotateDrawable) {
((RotateDrawable) drawable).setLevel(500);
}发布于 2014-06-20 09:19:57
已修复。我现在用的是动画旋转
<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();
}https://stackoverflow.com/questions/24312770
复制相似问题