首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在xml StateListDrawable中定义的访问动画

在xml StateListDrawable中定义的访问动画
EN

Stack Overflow用户
提问于 2014-06-19 17:26:56
回答 1查看 978关注 0票数 0

我的纽扣有这样的状态图:

代码语言:javascript
复制
<?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中声明旋转部分,然后我可以用

代码语言:javascript
复制
Animation a = AnimationUtils.loadAnimation(getContext(), R.anim.spinner);
view.startAnimation(a);

但是,如果动画位于可绘制的状态列表中以使其启动,我如何访问它?我试过这样的方法,但现在我被困住了:

代码语言:javascript
复制
StateListDrawable background = (StateListDrawable) mRecommendButton.getBackground();
        Drawable drawable = background.getCurrent();

这给了我一个吸引人的,但不是一个动画。

编辑**

显然,它返回了一个RotateDrawable,所以我完成了我的代码,但它仍然没有旋转。我还使用getCompoundDrawables(),因为我正在将可绘制的xml设置为Top。只是为了记录下。它确实输入了"if语句“。

代码语言:javascript
复制
StateListDrawable background = (StateListDrawable) mRecommendButton.getCompoundDrawables()[1];
    Drawable drawable = background.getCurrent();
    if (drawable instanceof RotateDrawable) {
        ((RotateDrawable) drawable).setLevel(500);
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-20 09:19:57

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

代码语言:javascript
复制
<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"/>

爪哇:

代码语言:javascript
复制
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();
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24312770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档