简体   繁体   English

如何在Android屏幕上获取所有触摸点的坐标

[英]How to get coordinates of all touch points on Android screen

Is there any way to get all touch points on Android screen? 有什么办法可以在Android屏幕上获取所有接触点? I am using MotionEvent to get coordinates when I touch parent layout and onTouchEvents when I touch some of the child elements. 当我触摸父级布局时,我正在使用MotionEvent获取坐标;当我触摸某些子元素时,我正在使用onTouchEvents But I would like to use some third party library (if exist), that can return all touchings on screen without knowing what activity contains. 但是我想使用一些第三方库(如果存在),该库可以在不知道活动内容的情况下返回屏幕上的所有内容。 I only need to get coordinates of all touchings on screen, but that library should impact the existing application functionalities. 我只需要获取屏幕上所有触摸的坐标,但是该库应该会影响现有的应用程序功能。 Is there any way to achieve this. 有什么办法可以做到这一点。 Thanks!!! 谢谢!!!

You don't need any toolkit or library to get the coordinates. 您不需要任何工具包或库即可获取坐标。 To get the coordinates of touch point on the screen, all you have to do is- getting X and Y value using MotionEvent object. 要获取屏幕上触摸点的坐标,您要做的就是使用MotionEvent对象获取X和Y值。 I have used this code in my project for Camera focus and zooming purpose. 我已在我的项目中将此代码用于相机聚焦和缩放目的。

Just copy and paste below code in your activity code (Outside of onCreate() method) and check logcat: 只需将以下代码复制并粘贴到您的活动代码中(onCreate()方法之外)并检查logcat:

@Override
public void onTouchEvent(MotionEvent event) {
    int pointerId = event.getPointerId(0);
    int pointerIndex = event.findPointerIndex(pointerId);
    // Get the pointer's current position
    float x = event.getX(pointerIndex);
    float y = event.getY(pointerIndex);
    System.out.println("X:"+x);
    System.out.println("Y:"+y);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM