简体   繁体   中英

Rotate a Drawable in Android

How do I rotate a Drawable in android?

I have a class that inherits from Drawable and overrides the Draw(canvas) method in order to create an arrow head that I need to place on a line and rotate it to match the slope of the line.

I cannot figure out how to rotate the Drawable; everything I've seen on this has been how to rotate a Bitmap, not a Drawable.

I have tried rotating the canvas first but that causes odd results.

Any suggestions?

The last thing I have tried that I have seen a lot of people discuss is this:

how to rotate a bitmap 90 degrees

So I figured it out. It seems there is a pretty specific way you must rotate the canvas in order to rotate a drawable on it:

canvas.Save(SaveFlags.Matrix);
canvas.Rotate(_degrees, _location.X, _location.Y);
canvas.DrawPath(path, _paint);
canvas.Restore();

So basically, you save the canvas' current state, rotate it (here, I'm using _location which holds a point in which the user lifted their finger from the screen), then draw on the rotated canvas and then restore it to it's previous state (with the drawable drawn still drawn).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM