How Do I Use 'RotateDrawable'?

后端 未结 7 2139
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 19:34

Could anyone tell me how they have got \'RotateDrawable\' to work whether it be from code or XML or both? The documentation on animating Drawables is pretty poor and animati

相关标签:
7条回答
  • 2020-12-15 20:08

    You have to animate the "level" property, where 0 is the start value and 10000 is the end value.

    The below example animates from start to finish, you can reverse the animation easily with this method.

    final RotateDrawable rotateDrawable = ...
    ObjectAnimator.ofInt(rotateDrawable, "level", 0, 10000).start();
    
    0 讨论(0)
  • 2020-12-15 20:11

    The following code returns a Drawable wrapper that rotates another Drawable programmatically:

    Drawable rotateDrawable(Drawable d, final float angle) {
        // Use LayerDrawable, because it's simpler than RotateDrawable.
        Drawable[] arD = {
            d
        };
        return new LayerDrawable(arD) {
            @Override
            public void draw(Canvas canvas) {
                canvas.save();
                canvas.rotate(angle);
                super.draw(canvas);
                canvas.restore();
            }
        };
    }
    
    0 讨论(0)
  • 2020-12-15 20:13

    This is a good working example. Param duration is used to animate it.

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="4000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="720" >
    
        <shape
            android:innerRadius="20dp"
            android:shape="ring"
            android:thickness="4dp"
            android:useLevel="false" >
            <size
                android:height="48dp"
                android:width="48dp" />
    
            <gradient
                android:centerY="0.5"
                android:endColor="@android:color/white"
                android:startColor="#00ffffff"
                android:type="sweep"
                android:useLevel="false" />
        </shape>
    
    </rotate>
    
    0 讨论(0)
  • 2020-12-15 20:14

    RotateDrawable does not seem to be animated. Instead, you have to use setLevel to change the rotation of the drawable.

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/your_drawable"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
    

    And set the level will rotate the drawable:

    final ImageView image = (ImageView)findViewById(R.id.imageView1);
    final RotateDrawable drawable = (RotateDrawable)image.getDrawable();
    drawable.setLevel(500);
    
    0 讨论(0)
  • 2020-12-15 20:20

    You could manually call RotatedDrawable.setLevel() to rotate the drawable, or you could read the code of ProgressBar, the indeterminate drawable is a LayerDrawable whose children were RotatedDrawable, like this one:

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

    The rotate animation was driven by ProgressBar's onDraw method.

    0 讨论(0)
  • 2020-12-15 20:22

    I would like to add a full example of animating a progress icon on ImageView, it is based on Mark Hetherington answer.

    So my animation looks as follows:

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
                     android:pivotX="50%"
                     android:pivotY="50%"
                     android:fromDegrees="0"
                     android:toDegrees="-360"
                     android:duration="100"
                     android:drawable="@drawable/ic_loop_black_24dp"
        />
    

    icon comes from https://material.io/icons/

    then my layout contains an ImageView as follows:

            <ImageView
                android:id="@+id/progress"
                android:layout_marginTop="0dp"
                android:layout_marginLeft="-3dp"
                android:layout_width="30dp"
                android:layout_height="30dp"
    
                android:visibility="gone"
                android:scaleType="fitCenter"
                android:background="@drawable/progress_anim"
                android:layout_gravity="center_horizontal|center_vertical"
                />
    

    and finally in code when I need to show animation I do:

        RotateDrawable rotateDrawable = ((RotateDrawable)progressImage.getBackground());
        ObjectAnimator anim = ObjectAnimator.ofInt(rotateDrawable, "level", 0, 10000);
        anim.setDuration(1000);
        anim.setRepeatCount(ValueAnimator.INFINITE);
        anim.start();
    
    0 讨论(0)
提交回复
热议问题