获取用户触摸的真实 x 和 y 坐标
Get real x and y coordinates of users touch
我需要将 onTouch 事件的 X、Y 坐标映射到 ImageView 内的位图 X、Y 坐标来执行此操作,我使用以下方法。
然而,这种方法似乎只有在我执行以下任一操作时才有效:
a) 完全缩放图像(一直放大)
b) 如果我让我的应用程序全屏显示,无论如何都可以工作
final int index = event.getActionIndex();
touchLocation = new float[] {
event.getX(index), event.getY(index)
};
Matrix matrix = new Matrix();
ImageView view = getImageView();
view.getImageMatrix().invert(matrix);
matrix.postTranslate(view.getScrollX(), view.getScrollY());
matrix.mapPoints(touchLocation);
// touchLocation[0] is real x and [1] is real y
但是我的 activity 是一个 ActionBar activity 所以我在 Y 轴上的位置有点错误。我尝试扣除 ActionBar 和 StatusBar 的高度,但这并不总是有效。
奇怪的是,在全屏模式下,无论我完全放大或缩小图像都没有关系,我总是计算出正确的坐标,但是对于任何其他 Activity 类型,这将无法正确映射点。
我无法从粘贴的代码中判断出确切的问题是什么,但看起来您应该使用:http://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[]) 并在您的计算中考虑到这一点。
试试这个方法
int[] viewCoords = new int[2];
imageView.getLocationOnScreen(viewCoords);
int touchX = (int) event.getX();
int touchY = (int) event.getY();
int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate
int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate
您将使用这种方式获得 x 和 y 坐标
覆盖 onTouchEvent(MotionEvent event) 然后调用 event.getX() 和 event.getY() 来获取用户触摸位置的坐标位置 [它们将是浮动的]。 (作者:@antonyt)
第 1 步:在您的应用中创建一个扩展 FrameLayout
的自定义 CustomFrameLayout,它可以在构造函数中采用一个接口对象。 (接口将在步骤 3 中创建)
第 2 步:Write/OverrideonInterceptTouchEvent
方法。
第 3 步:为 send/receive 所需参数创建一个接口。
第 4 步:在 activity/fragment
中实现第 3 步中创建的接口和方法
第 5 步:在您的 activity/fragment 中使用实现接口创建 CustomFrameLayout
的对象。
第6步:在自定义布局的重写方法中,通过接口
传值给activity
希望这能奏效!
制作演示会花费更长的时间,宁愿 post 作为评论,但仍然 post 作为答案,以便有一天我可以通过一些示例代码进行改进。
我使用此代码获取屏幕上的实际触摸轴:
PointF p = new PointF(event.getX(), event.getY());
View v = this;// your view
View root = v.getRootView();
while (v.getParent() instanceof View && v.getParent() != root) {
p.y += v.getTop();
p.x += v.getLeft();
v = (View) v.getParent();
}
希望这对你也有用
确保您的 Actionbar 和状态高度以像素为单位而不是 dp
你也可以尝试使用 getRawX() 和 getRawY() 而不是 getX(), getY()
我需要将 onTouch 事件的 X、Y 坐标映射到 ImageView 内的位图 X、Y 坐标来执行此操作,我使用以下方法。
然而,这种方法似乎只有在我执行以下任一操作时才有效:
a) 完全缩放图像(一直放大)
b) 如果我让我的应用程序全屏显示,无论如何都可以工作
final int index = event.getActionIndex();
touchLocation = new float[] {
event.getX(index), event.getY(index)
};
Matrix matrix = new Matrix();
ImageView view = getImageView();
view.getImageMatrix().invert(matrix);
matrix.postTranslate(view.getScrollX(), view.getScrollY());
matrix.mapPoints(touchLocation);
// touchLocation[0] is real x and [1] is real y
但是我的 activity 是一个 ActionBar activity 所以我在 Y 轴上的位置有点错误。我尝试扣除 ActionBar 和 StatusBar 的高度,但这并不总是有效。
奇怪的是,在全屏模式下,无论我完全放大或缩小图像都没有关系,我总是计算出正确的坐标,但是对于任何其他 Activity 类型,这将无法正确映射点。
我无法从粘贴的代码中判断出确切的问题是什么,但看起来您应该使用:http://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[]) 并在您的计算中考虑到这一点。
试试这个方法
int[] viewCoords = new int[2];
imageView.getLocationOnScreen(viewCoords);
int touchX = (int) event.getX();
int touchY = (int) event.getY();
int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate
int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate
您将使用这种方式获得 x 和 y 坐标
覆盖 onTouchEvent(MotionEvent event) 然后调用 event.getX() 和 event.getY() 来获取用户触摸位置的坐标位置 [它们将是浮动的]。 (作者:@antonyt)
第 1 步:在您的应用中创建一个扩展 FrameLayout
的自定义 CustomFrameLayout,它可以在构造函数中采用一个接口对象。 (接口将在步骤 3 中创建)
第 2 步:Write/OverrideonInterceptTouchEvent
方法。
第 3 步:为 send/receive 所需参数创建一个接口。
第 4 步:在 activity/fragment
中实现第 3 步中创建的接口和方法第 5 步:在您的 activity/fragment 中使用实现接口创建 CustomFrameLayout
的对象。
第6步:在自定义布局的重写方法中,通过接口
传值给activity希望这能奏效!
制作演示会花费更长的时间,宁愿 post 作为评论,但仍然 post 作为答案,以便有一天我可以通过一些示例代码进行改进。
我使用此代码获取屏幕上的实际触摸轴:
PointF p = new PointF(event.getX(), event.getY());
View v = this;// your view
View root = v.getRootView();
while (v.getParent() instanceof View && v.getParent() != root) {
p.y += v.getTop();
p.x += v.getLeft();
v = (View) v.getParent();
}
希望这对你也有用
确保您的 Actionbar 和状态高度以像素为单位而不是 dp 你也可以尝试使用 getRawX() 和 getRawY() 而不是 getX(), getY()