1

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

Community
  • 1
  • 1
kformeck
  • 1,703
  • 5
  • 22
  • 43
  • bitmapDrawable.getBitmap(); Then use the obtained bitmap and rotate it. – Mr.Rao Sep 01 '16 at 07:59
  • @Mr.Rao I have tried that and it doesn't seem to work. I will edit the question above with an example – kformeck Sep 01 '16 at 13:49
  • Does this answer your question? [Rotating Image on A canvas in android](https://stackoverflow.com/questions/8712652/rotating-image-on-a-canvas-in-android) – Mahozad Jun 28 '21 at 13:36

1 Answers1

0

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).

kformeck
  • 1,703
  • 5
  • 22
  • 43