-1

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.

enter image description here

Zoe
  • 27,060
  • 21
  • 118
  • 148
Nirmal Prajapat
  • 1,735
  • 1
  • 12
  • 23

2 Answers2

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