用户触摸

时间:2017-12-06 05:40:11

标签: android android-canvas

我试图在网上查看很多相关信息,但找不到任何解决方案。我想在用户拖动rect角落(图片中指向)时调整bottom-rightrotation的中心应与rect的中心相同

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用画布旋转。

//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 检查此链接。你可以override onTouchEvent()

答案 1 :(得分:0)

确定旋转角度的一般方法:

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

请注意,必须在仿射矩阵计算中使用旋转中心坐标才能正确绘制旋转的矩形:

 canvas.save();
 canvas.rotate(alpha, xc, yc);
 canvas.drawRect(.....);
 canvas.restore();