Rotate and resize rectangle drawn in canvas in Android - android

In my Android application I have drawn rectangle in canvas, how can I rotate rectangle and resize the rectangle on touch events ?
canvas.drawRect(300,300,500,500,paint);
This code is used to draw the rectangle.

You can do using below
canvas.save(); // saving current canvas
resize
rect.set(x,y,newWidth, newHeight);
rotate
canvas.rotate(45);
Apply changes
canvas.drawRect(rect, paint);
canvas.restore();

Related

How to rotate a circle in android canvas

I drawn a circle like this
canvas.drawCircle(40, 40, 50, mPaint);
I want to rotate this circle, how can I do this?
Rather than rotating the circle, you should rotate the canvas by method of Canvas.rotate() . You could look docs for this method.

Android Canvas.drawBitmap(bmp, left, top, paint) after rotation

Is there a detailed documentation explaining the left/top parameter used in the canvas.drawBitmap method?
I thought it should always be the position relative to left-top corner of the canvas. For example, if I want to draw something at the top left corner, I just draw from origin (0,0).
However, I found this is not the case if the canvas is rotated 90 degree.
canvas.save();
canvas.rotate(90, canvas.getWidth()/2, canvas.getHeight()/2);
canvas.drawBitmap(bmp, 0, 0, null);
canvas.restore();
I was expecting the bitmap should be drawn from the left top corner on the rotated canvas, and displayed from right top corner after I restore the canvas, but it comes out different result.
I have to draw at
x= (canvas.getWidth()-canvas.getHeight())/2
y= (canvas.getHeight()-canvas.getWidth())/2
to get the expected result, why?

android how to rotate canvas rect

i create a rectangle in a specific size, and now i want rotate it to 45 degree, i used canvas.rotate, matrix, but not working. how is the proper way to rotate canvas in android? and i'm curios about Path.Direction.CW, is it used for rotation? but i don't see any rotation function in Path()
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setColor(Color.BLUE);
paint.setAlpha(75);
Path path = new Path();
path.addRect(166, 748, 314, 890, Path.Direction.CW);
canvas.rotate(45);
canvas.drawPath(path, paint);
To draw a rotated rectangle you need to rotate the canvas before drawing, (then rotate it back to right-side up if you're drawing anything else). Canvas.rotate() just alters the canvas's transformation matrix, which transforms shapes drawn after the call.
canvas.save();
canvas.rotate(45);
canvas.drawRect(166, 748, 314, 890, paint);
canvas.restore();
Path.Direction has nothing to do with rotation transforms. From the docs:
Specifies how closed shapes (e.g. rects, ovals) are oriented when they
are added to a path.
If you want to draw something from (x,y) point, you have to rotate the canvas around (x,y) point. For doing this you should use
canvas.rotate(45,x,y);
so,
canvas.save();
canvas.rotate(45,x,y);
//all drawing from (x,y) point
canvas.restore();
Proper way should be something like this:
Path path = new Path();
path.addRect(166, 748, 314, 890, Path.Direction.CW);
canvas.save(); // first save the state of the canvas
canvas.rotate(45); // rotate it
canvas.drawPath(path, paint); // draw on it
canvas.restore(); // restore previous state (rotate it back)

Android : canvas.drawoval() + rotation

I'd like to draw an oval, but I'd like to be able to rotate it. I know that I can use canvas.drawOval(...) and canvas.rotate(...). However, I want to just rotate my oval and not the whole canvas; that is, I want to rotate my oval before draw it to the canvas.
I have successfully rotated a rect by manipulating the coordinates before drawing it, but that approach isn't working for me with the oval.
There's canvas.save() and canvas.restore() to serve that purpose - i.e.
canvas.save();
canvas.rotate(90);
canvas.drawOval(....);
canvas.restore();
....//do other drawing

Rotating two seprate objects(paths) in Canvas

I have two arrows drawn on my canvas using the canvas.DrawPath(). I'm using canvas.rotate() to rotate, but it is obviously rotating both arrows by the same amount.
Id like to be able to rotate one arrow one way, and rotate the other arrow in a different direction.
Is this possible?
When you use canvas.rotate() you are altering the transformation matrix associated to the canvas, so all what you paint after that will be affected by the current state of the matrix, you have to do the following:
canvas.save(); //Save current canvas matrix state
canvas.rotate(angle);
canvas.DrawPath(); //Draw first arrow
canvas.restore(); //Restore canvas matrix to saved state
canvas.DrawPath(); //Draw second arrow without the rotation

Categories

Resources