【问题标题】:Animating all RotateDrawables inside LayerDrawable为 LayerDrawable 中的所有 RotateDrawables 设置动画
【发布时间】:2013-07-25 09:54:04
【问题描述】:

我在 layer-list 中有两个可旋转的可绘制对象,我正在尝试将它们都设置为按钮上的 drawableleft 动画。我正在显示可绘制对象,但没有动画。

这是我在 xml (button_progress_bar_small.xml) 中的可绘制布局:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <rotate
         android:drawable="@drawable/ic_spinner_small_outer"
         android:pivotX="50%"
         android:pivotY="50%"
         android:fromDegrees="0"
         android:toDegrees="1080" />
</item>
<item>
    <rotate
         android:drawable="@drawable/ic_spinner_small_inner"
         android:pivotX="50%"
         android:pivotY="50%"
         android:fromDegrees="720"
         android:toDegrees="0" />
</item>

这是我正在运行的代码:

    button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.button_progress_bar_small, 0, 0, 0);
    LayerDrawable progressAnimationLeft = (LayerDrawable) button.getCompoundDrawables()[0];
    ((RotateDrawable) progressAnimationLeft.getDrawable(0)).setLevel(500);
    ((RotateDrawable) progressAnimationLeft.getDrawable(1)).setLevel(500);

我不确定启动动画的触发器是什么,或者我是否应该在每个RotateDrawable 上执行某些操作(与LayerDrawable 上的一个操作相反)才能做到这一点。

【问题讨论】:

    标签: android android-animation android-drawable


    【解决方案1】:

    好的,我找到了解决问题的方法。

    我将布局更改为:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:drawable="@drawable/ic_spinner_small_outer"
            android:pivotX="50%"
            android:pivotY="50%"
            android:fromDegrees="0"
            android:toDegrees="1080"
            android:interpolator="@android:anim/linear_interpolator"
            android:repeatCount="infinite"/>        
    </item>
    <item>
        <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:drawable="@drawable/ic_spinner_small_inner"
            android:pivotX="50%"
            android:pivotY="50%"
            android:fromDegrees="720"
            android:toDegrees="0"
            android:interpolator="@android:anim/linear_interpolator"
            android:repeatCount="infinite"/>        
    </item>
    

    并将代码更改为:

        button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.button_progress_bar_small, 0, 0, 0);
        LayerDrawable progressAnimationLeft = (LayerDrawable) button.getCompoundDrawables()[0];
        ((Animatable) progressAnimationLeft.getDrawable(0)).start();
        ((Animatable) progressAnimationLeft.getDrawable(1)).start();
    

    它并没有真正回答这个问题,但它对我来说是一个很好的解决方法,具有相同的动画效果。

    【讨论】:

    • 疯了,没有人对这个问题有任何话要说!
    • 发生在这件事上,很想知道您是否能够为每个项目获得独立的动画属性;我无法
    • 如果你的意思是“让每个动画独立于另一个旋转”而不是。我只看到一个动画。这是 SO 中的一个已知问题。
    • 这是一个很好的答案,帮我解决如何使搜索栏拇指旋转动画的问题。只需要将动画旋转设置为 thumb ,然后获取 thumb 并将其转换为 Animatable 和 start()
    【解决方案2】:

    您必须为 0 到 10000 范围内的级别值设置动画。

    button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.button_progress_bar_small, 0, 0, 0);  
    LayerDrawable progressAnimationLeft = (LayerDrawable) button.getCompoundDrawables()[0];
    ObjectAnimator.ofInt((RotateDrawable) progressAnimationLeft.getDrawable(0), "level", 0, 10000).start();
    ObjectAnimator.ofInt((RotateDrawable) progressAnimationLeft.getDrawable(1), "level", 0, 10000).start();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-03
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      相关资源
      最近更新 更多