Urthas
Urthas

Reputation: 31

Android canvas rotation: need to rotate *by* x degrees not *to* x degrees

The documentation for the rotate methods in Canvas states that the degrees parameter is "the amount to rotate, in degrees". However, in my experience this is misleading. For example, if I call rotate(45) when my bitmap is in the canonically upright orientation (i.e. theta = 0, which is at 12:00) then the bitmap dutifully rotates 45 degrees with respect to 0. If I make a second call to rotate(45), the image does not [appear to] move. If I then make a call to rotate(-45), the bitmap does not return to its original orientation but is instead rotated -45 degrees with respect to 0. In other words, the degrees parameter is absolute ("rotate to 45 degrees"), not relative ("rotate by 45 degrees") as the documentation implies. This is precisely what I don't want.

From a given orientation, I want to be able to rotate my bitmap BY +/- x degrees, where + is of course a clockwise rotation. One way to fake this, I suppose, would be to make wherever I rotate to the "new 0" (i.e. it effectively occupies 12:00) for the purposes of the next rotation. My question is: how do I do this, or, is there a better way to accomplish what I want?

Many thanks.

Upvotes: 3

Views: 1877

Answers (2)

entonio
entonio

Reputation: 2173

I think canvas.setMatrix(canvas.getMatrix()) will 'save' all the changes you have made thereby making wherever you've rotated the 'new 0'. But dbryson's suggestion is the economical one for the purpose you mention.

Upvotes: 1

dbryson
dbryson

Reputation: 6127

One simple approach would be to store the last rotation as a variable and +/- next rotation to it - watching of course you don't go beyond 360.

Upvotes: 2

Related Questions