【问题标题】:Access animation defined in xml StateListDrawable访问 xml StateListDrawable 中定义的动画
【发布时间】: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


【解决方案1】:

已修复。我现在使用动画旋转

<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"/>

Java:

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();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 2011-09-22
    • 2020-06-08
    相关资源
    最近更新 更多