没有这样的事情。
Path 只是一种数据结构,与您如何使用它(绘制、剪辑、...)无关。触摸事件也是如此。
您只需要对触摸坐标进行一些数学运算。这是使用矩阵的二维变换。你可以阅读这个on wikipedia。
首先您应该将触摸点映射到缩放/平移的坐标,看看here、here 和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;
}
我不能告诉你这是否适合你,因为我不知道你有什么用的路径,我只能假设你用它来绘制你的图像。如果您在绘制路径之前应用任何其他转换,则应使用应用于路径的转换。
如果您在画布上进行这些转换,您可以像这样提取矩阵:
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 方法检查它是否在路径内,其他人已经在您的问题的 cmets 中提出了建议。
if (path.contains(coordX, coordY)) {
// inside
} else {
// outside
}
您是唯一知道您正在使用的代码以及路径坐标系如何在您的视图中转换的人,因此也是唯一知道如何正确将其转换回来的人。因此,不要将此答案视为插入代码。我只是给你指了个方向。打印一些触摸坐标/转换的日志以在开发时对其进行调试可能会有所帮助。
祝你好运。