How to use "RotateDrawable"?

Can someone tell me how they have a “RotateDrawable” to work, be it code or XML, or both? The documentation for Drawables animation is pretty poor, and the animation seems to work only for images. I want to be able to revive all the drawings. When I tried to get RotateDrawble from XML, it just throws an exception. What is the correct RotateDrawable search function from XML?

Thank you very much

Kerubu

+10
android android-drawable
May 03 '11 at 15:57
source share
7 answers

You need to revive the "level" property, where 0 is the initial value, and 10000 is the final value.

Below is an example of animation from start to finish, you can easily change the animation using this method.

final RotateDrawable rotateDrawable = ... ObjectAnimator.ofInt(rotateDrawable, "level", 0, 10000).start(); 
+7
Jun 17 '15 at 20:34
source share

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

 <?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 that the drawable will rotate:

 final ImageView image = (ImageView)findViewById(R.id.imageView1); final RotateDrawable drawable = (RotateDrawable)image.getDrawable(); drawable.setLevel(500); 
+2
Dec 07 2018-11-11T00:
source share

This is a good working example. The duration of the parameter 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> 
+2
Dec 30 '15 at 13:10
source share

I did not work with RotateDrawable, but if you are just trying to animate rotation on a chart, you do not need it. Drawables with a “level” like RotateDrawable are designed to convey information, not animation.

The following code rotates the ImageView around its center:

 ImageView myImageView = (ImageView)findViewById(R.id.my_imageview); AnimationSet animSet = new AnimationSet(true); animSet.setInterpolator(new DecelerateInterpolator()); animSet.setFillAfter(true); animSet.setFillEnabled(true); final RotateAnimation animRotate = new RotateAnimation(0.0f, -90.0f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); animRotate.setDuration(1500); animRotate.setFillAfter(true); animSet.addAnimation(animRotate); myImageView.startAnimation(animSet); 
+1
May 3 '11 at 16:19
source share

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

 Drawable rotateDrawable(Drawable d, final float angle) { // Use LayerDrawable, because it 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(); } }; } 
+1
Jun 15 '13 at 12:46 on
source share

You can manually call RotatedDrawable.setLevel () to rotate the drawable, or you can read the ProgressBar code, the undefined 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 rotation animation was controlled by the OnDraw ProgressBar method.

+1
Aug 21 '13 at 11:37
source share

I would like to add a complete example of the animation of the progress icon in ImageView, it is based on Mark Hetherington's answer.

So my animation looks like this:

 <?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 the code, when I need to show the animation:

  RotateDrawable rotateDrawable = ((RotateDrawable)progressImage.getBackground()); ObjectAnimator anim = ObjectAnimator.ofInt(rotateDrawable, "level", 0, 10000); anim.setDuration(1000); anim.setRepeatCount(ValueAnimator.INFINITE); anim.start(); 
+1
Jun 15 '17 at 20:26
source share



All Articles