Rotating a drawable in Android
You need to use Bitmap and Canvas Class functions to prepare drawable:
Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.image2);
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult);
tempCanvas.rotate(90, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
mImageView.setImageBitmap(bmResult);
In this code sample rotation for 90 degrees over image center occurs.
Android RotateDrawable: Doesn't correctly work for Vector Drawables
Seems to be a bug. vectorDrawables.useSupportLibrary = true
solved the problem.
See "Vector drawables backward compatibility solution" section of below page for details:
https://developer.android.com/guide/topics/graphics/vector-drawable-resources
Rotate drawable has not effect
Try using imageView2.setRotation(180)
instead of rotation drawable.
Android rotate drawable in button
You can create a layer-list
which contains animation with a drawable.
ic_refresh_rotate.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:drawable="@drawable/ic_refresh"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
</item>
</layer-list>
Now set the drawable as compound drawable for the Button
or TextView
.
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="16sp"
app:drawableLeftCompat="@drawable/ic_refresh_rotate" />
Finally, in your java code, try to start the animation like the following:
Drawable[] compoundDrawables = textView.getCompoundDrawables();
for (Drawable drawable : compoundDrawables) {
if (drawable == null) continue;
ObjectAnimator anim = ObjectAnimator.ofInt(drawable, "level", 0, 10000);
anim.setDuration(1000);
anim.setRepeatCount(Animation.INFINITE);
anim.setInterpolator(new LinearInterpolator());
anim.start();
}
Result:
Is it possible to rotate a drawable in the xml description?
I could rotate in XML:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@drawable/mainmenu_background">
</rotate>
The fromDegrees
is important.
Basically this is a rotate animation defined in XML. With fromDegrees
you define the initial rotated state. The toDegrees
is the final rotated state of the drawable in the animation sequence but can be anything if you don't want to use animation.
I don't think it allocates resources for animation as it doesn't have to be loaded as animation. As a drawable it is rendered as it's initial state and should be put in the drawable
resource folder.
To use it as an animation you should put it in anim
resource folder and can start the animation like this (just an example):
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
rotation.setRepeatCount(Animation.INFINITE);
myView.startAnimation(rotation);
Related Topics
Android 'Debuggable' Default Value
Android Spinner Error:Android.View.Windowmanager$Badtokenexception: Unable to Add Window
How to Add Code Obfuscation for My Android Application
Should Accessing Sharedpreferences Be Done Off the UI Thread
How to Scroll to Top of Long Scrollview Layout
How to Have a Listview/Recyclerview Inside a Parent Recyclerview
How to Work with Android's In-App Update API
Configure Eclipse to Use Signed Keystore
Post on Facebook Wall Using Facebook Android Sdk Without Opening Dialog Box
Cordova - Error: Failed to Fetch Platform Android
How to Show Daily Offline Notifications in Android 10
How to Remove Extra Padding or Margin in Material Design Button
Failure [Install_Failed_Already_Exists] When I Tried to Update My Application
Android: What Is Better - Multiple Activities or Switching Views Manually
Android Recyclerview: Change Layout File List to Grid Onoptionitemselected