如何在 android studio xml 中获取触摸像素的位置?

How to get position of pixel touched in android studio xml?

我正在尝试获取 android 工作室 xml 背景图像的实际触摸像素。当触摸位置在所需坐标的范围内时,我正在尝试启动一个新的 activity。谢谢!

您可以在带有背景图片的布局上设置 onTouchListener()。

RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
mainLayout.setOnTouchListener(this); // in this case activity should implements  View.OnTouchListener

然后使用类似的东西

public boolean onTouch(View v, MotionEvent event) {
    Point touchPlace = new Point();
    touchPlace.set(event.getX(), event.getY());
}