views:

813

answers:

1

How can I rotate an Image eg. 180 degrees clockwise using the Matrix

I can use the following code to rotate the image 90 degrees, but it is incremental, meaing

var matrix:Matrix = new Matrix();

matrix.rotate(Math.PI/2);
matrix.tx = imgControl.content.height;

var bitmapData:BitmapData = new BitmapData(imgControl.content.height, imgControl.content.width);
bitmapData.draw(imgControl.content, matrix);
imgControl.source = new Bitmap( bitmapData);

Each time I run the code the image is rotated +90 degrees.

What I want is not to increment by 90 each time, but explicit say rotate 180, rotate90 and so on.

I am not familiar with the Matrix, but I guess it does real bitmapdata manipulation rather than just eg. rotate the Image component box (arrest me if I am wrong).

If so, I guess I have to reset the image each time I do the rotate command.

Am I missing something?

Thanks in advance for any advice and tips

Ran

+1  A: 

The matrix does no real bitmapdata manipulation.

It is the bitmap.draw call that draws the rotated image of the imgcontrol.content into the bitmap, after which your code overwrites imgcontrol.content with the rotated image.

So as your code is currently standing, yes, you either have to refresh the image from scratch before every rotation, or you will have to keep track of the rotations in a variable and calculate how many more times you have to rotate to get to the desired rotation.

If you need to do multiple 90 degrees rotation in one step, then replace

matrix.rotate(Math.PI/2);

with

matrix.rotate(Math.PI/2 * howmanytimesyouwanttorotateby90degrees);
Bandi-T
Spoken like a true Oracle. I think this is The One.
fireeyedboy
matrix.rotate(Math.PI/2 * 2);Makes the image disapear
Ran
oh right! that is what the `matrix.tx` line is there for! .rotate rotates around one corner of the image (probably lower left corner?); so you rotate the image "out" from the box/rectangle, so you have to translate (shift/push) the image back into the box. you can do that with .tx for x direction and I guess .ty for y direction. Take a piece of paper and draw it out for yourself, and experiment with code, to see which direction you have to translate and by how much.
Bandi-T