I tried to check on web a lot about this but couldn't find any solution. I want to rotate a rect
whenever a user drags it's bottom-right
corner (pointed in the image) and the center of the rotation
should be the same as center of the rect
.
Asked
Active
Viewed 529 times
-1

Zoe
- 27,060
- 21
- 118
- 148

Nirmal Prajapat
- 1,735
- 1
- 12
- 23
-
And what is your question? – MBo Dec 06 '17 at 05:44
-
How to rotate the rect ?? – Nirmal Prajapat Dec 06 '17 at 05:45
-
what about the file i sent you in chat. Have you checked that file@ pskink – Nirmal Prajapat Dec 06 '17 at 07:19
2 Answers
0
You can rotate using canvas.
//Example
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 Implement onTouchListener on path objects
check this link. You can override onTouchEvent()

Muhammad Saad
- 713
- 1
- 9
- 31
-
it'll always rotate the canvas by 45 degree and i want to rotate it on user basis – Nirmal Prajapat Dec 06 '17 at 05:55
-
-
-
@NirmalPrajapat you can change 45 to desired value like canvas.rotate(75); it will rotate 75 degrees – Tara Dec 06 '17 at 06:06
-
how to find that angle if user drags the bottom-right corner of the rect @WaleedAsim – Nirmal Prajapat Dec 06 '17 at 06:07
-
Check link that i've given in answer. You first find the X and Y values using touchEvent and stored in some memory. only first time, for your purpose. next time, check if user X and Y values == to your saved X Y values then print toast to rotate angle – Muhammad Saad Dec 06 '17 at 06:10
0
general approach to determine rotation angle:
rotation center coordinates xc, yc
on touch (mouse) down:
check whether touch point is in grab region
remember coordinates x0, y0
set flag "InRotation"
on touch move:
current coordinates x1, y1
check if (they are in allowed region) and (InRotation is set)
calculate rotation angle as
alpha = Math.atan2((x1-xc)*(y0-yc) - (x0-xc)*(y1-yc),
(x1-xc)*(x0-xc) + (y0-yc)*(y1-yc))
redraw image using alpha angle
on touch up:
clear InRotation flag
make actions after rotation if needed
Note that you have to use rotation center coordinates in affine matrix calculation to properly draw rotated rectangle:
canvas.save();
canvas.rotate(alpha, xc, yc);
canvas.drawRect(.....);
canvas.restore();

MBo
- 77,366
- 5
- 53
- 86
-
-
yes, it is atan2. And I don't know - it's result might be in radians (not degrees) in your language. – MBo Dec 06 '17 at 06:14
-
-
Yes, xc and yc is the center of rect, if you rotate rect about it''s center – MBo Dec 06 '17 at 06:17
-
((x1-xc)*(y0-yc) - (x0-xc)*(y1-yc), (x1-xc)*(x0-xc) + (y0-xc)*(y1-yc)) are you sure this formula is correct ???.. plz check this one more time. i have a doubt in this – Nirmal Prajapat Dec 06 '17 at 06:19
-
you said in your answer that "redraw image using alpha angle" but this is not an image it's a rect . how to redraw it with rotation? – Nirmal Prajapat Dec 06 '17 at 06:25
-
If initial rect has been drawn with angle `a0` (might be zero), new rectangle will be drawn with angle `a0 + alpha` – MBo Dec 06 '17 at 06:42
-
-
-
I am getting the value of alpha between 0 and 1 , this is not working – Nirmal Prajapat Dec 06 '17 at 07:00
-
-
-
-
atan2 result is in radians, canvas.rotate gets degrees, so you need to convert radians to degrees. – MBo Dec 06 '17 at 08:08
-
what if I don't want to rotate the whole canvas, i just want to rotate only rect because i'm moving the rect on touch inside the rect too. so after rotating the canvas i'm unable to move the rect in correct direction. – Nirmal Prajapat Dec 06 '17 at 08:32
-
@pskink but in that example you are drawing a bitmap , and i'm using rect. can you tell me how to rotate a rect and then redraw it?? and you're also rotating the canvas – Nirmal Prajapat Dec 06 '17 at 08:38