将 touchListener 添加到路径对象
Adding touchListener to path object
我有一个canvas,我在上面设置了一个路径
Path path = new Path();
path.moveTo(1, 1);
path.lineTo(90, 1);
path.lineTo(90, 60);
path.lineTo(1, 60);
canvas.drawPath(path,p);
现在我想为这个路径对象实现一个触摸监听器。
我已经在自定义 ImageView 上实现了缩放选项,并在此自定义 ImageView 的 onDraw 方法中在 canvas 上绘制了这条线。所以我不能通过检查用户触摸的坐标来做到这一点。
我知道路径对象不是 View 的子对象 Class,因此我无法在其中实现 touchListner。但我真正需要的是
path.setOnTouchListener(myTouchListner);
有人知道如何实施吗?任何帮助表示赞赏。
你能超越 onTouchEvent()
吗?
找到你触摸过的坐标,检查坐标是否与你的路径相匹配,如果确实触发了你的功能..
详情请查看下方
REFERENCE
没有这样的事情。
Path 只是一个数据结构,你如何使用它(绘制,剪辑,......)与它无关。触摸事件也是如此。
你只需要用触摸坐标做一些数学运算。这是一个使用矩阵的二维变换。你可以阅读这个 on wikipedia.
首先你应该将触摸点映射到缩放/平移坐标,看看here, here and here。
我没有测试过,但如果你有一个 ImageView,这个方法应该可以:
final float[] getPointerCoords(ImageView view, MotionEvent e)
{
final int index = e.getActionIndex();
final float[] coords = new float[] { e.getX(index), e.getY(index) };
Matrix matrix = new Matrix();
// invert compute the inverse transformation matrix
view.getImageMatrix().invert(matrix);
// this adjust the panning
matrix.postTranslate(view.getScrollX(), view.getScrollY());
// this apply the inverse transformation to your touch points
// which should give you the coordinates on your imageview
matrix.mapPoints(coords);
return coords;
}
我不能告诉你这是否对你有用,因为我不知道你的 Path 有什么用,我只能假设你用它来绘制你的图像。如果您在绘制路径之前应用任何其他变换,您应该使用应用于路径的变换。
如果您对 canvas 进行这些转换,您可以像这样提取矩阵:
Matrix matrix = canvas.getMatrix()
另一种方法是将矩阵值提取到数组中并自己进行计算:
// Get the values of the matrix
// create this array in a field an reuse it for performances
float[] values = new float[9];
matrix.getValues(values);
values[2]
和 values[5]
是转换元素左上角的 x,y
坐标,与缩放因子无关
values[0]
和 values[4]
分别是转换元素的宽度和高度的缩放因子。如果你放大相同的因子,它们应该是相同的值。
当您最终将触摸点转换为 Path
坐标系时,您可以使用 this 其他人已经在您的问题评论中建议的方法检查它是否在路径内。
if (path.contains(coordX, coordY)) {
// inside
} else {
// outside
}
您是唯一知道您正在使用的代码以及路径坐标系在您的视图中如何转换的人,因此也是唯一知道如何将其正确转换回来的人。所以不要将此答案视为插入式代码。我只是给你指明了方向。打印一些触摸坐标/转换的日志可能有助于在您开发时进行调试。
祝你好运。
我有一个canvas,我在上面设置了一个路径
Path path = new Path();
path.moveTo(1, 1);
path.lineTo(90, 1);
path.lineTo(90, 60);
path.lineTo(1, 60);
canvas.drawPath(path,p);
现在我想为这个路径对象实现一个触摸监听器。
我已经在自定义 ImageView 上实现了缩放选项,并在此自定义 ImageView 的 onDraw 方法中在 canvas 上绘制了这条线。所以我不能通过检查用户触摸的坐标来做到这一点。
我知道路径对象不是 View 的子对象 Class,因此我无法在其中实现 touchListner。但我真正需要的是
path.setOnTouchListener(myTouchListner);
有人知道如何实施吗?任何帮助表示赞赏。
你能超越 onTouchEvent()
吗?
找到你触摸过的坐标,检查坐标是否与你的路径相匹配,如果确实触发了你的功能..
详情请查看下方 REFERENCE
没有这样的事情。 Path 只是一个数据结构,你如何使用它(绘制,剪辑,......)与它无关。触摸事件也是如此。
你只需要用触摸坐标做一些数学运算。这是一个使用矩阵的二维变换。你可以阅读这个 on wikipedia.
首先你应该将触摸点映射到缩放/平移坐标,看看here, here and here。
我没有测试过,但如果你有一个 ImageView,这个方法应该可以:
final float[] getPointerCoords(ImageView view, MotionEvent e)
{
final int index = e.getActionIndex();
final float[] coords = new float[] { e.getX(index), e.getY(index) };
Matrix matrix = new Matrix();
// invert compute the inverse transformation matrix
view.getImageMatrix().invert(matrix);
// this adjust the panning
matrix.postTranslate(view.getScrollX(), view.getScrollY());
// this apply the inverse transformation to your touch points
// which should give you the coordinates on your imageview
matrix.mapPoints(coords);
return coords;
}
我不能告诉你这是否对你有用,因为我不知道你的 Path 有什么用,我只能假设你用它来绘制你的图像。如果您在绘制路径之前应用任何其他变换,您应该使用应用于路径的变换。
如果您对 canvas 进行这些转换,您可以像这样提取矩阵:
Matrix matrix = canvas.getMatrix()
另一种方法是将矩阵值提取到数组中并自己进行计算:
// Get the values of the matrix
// create this array in a field an reuse it for performances
float[] values = new float[9];
matrix.getValues(values);
values[2]
和values[5]
是转换元素左上角的x,y
坐标,与缩放因子无关values[0]
和values[4]
分别是转换元素的宽度和高度的缩放因子。如果你放大相同的因子,它们应该是相同的值。
当您最终将触摸点转换为 Path
坐标系时,您可以使用 this 其他人已经在您的问题评论中建议的方法检查它是否在路径内。
if (path.contains(coordX, coordY)) {
// inside
} else {
// outside
}
您是唯一知道您正在使用的代码以及路径坐标系在您的视图中如何转换的人,因此也是唯一知道如何将其正确转换回来的人。所以不要将此答案视为插入式代码。我只是给你指明了方向。打印一些触摸坐标/转换的日志可能有助于在您开发时进行调试。
祝你好运。