【发布时间】:2014-06-19 17:26:56
【问题描述】:
我的按钮有一个 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() 因为我将我的可绘制 xml 设置为 Top。只是为了记录。它确实进入了“if 语句”。
StateListDrawable background = (StateListDrawable) mRecommendButton.getCompoundDrawables()[1];
Drawable drawable = background.getCurrent();
if (drawable instanceof RotateDrawable) {
((RotateDrawable) drawable).setLevel(500);
}
【问题讨论】:
-
rotate 标签定义了RotateDrawable,而不是任何Animation
-
谢谢。那么我应该从 setLevel 开始吗?因为它不旋转
-
试试 background.setLevel
-
没有仍然无法工作。但我确实找到了另一种获取 RotateDrawable 的方法。编辑了我的问题,但对于 RotateDrawable,没有什么比 start() 更好的了?
-
因为 RotateDrawable 是 Drawable,它只是将子 Drawable 旋转了某个角度,它不会以任何方式对其进行动画处理
标签: java android xml animation