简体   繁体   English

Android-将触摸坐标发送到另一台设备

[英]Android - send touch coordinates to another device

I want to draw something in my device, and the paint will drawn on another device. 我想在我的设备上绘制一些东西,然后绘画将在另一个设备上绘制。

For that, I wrote a server that take the coordinates of the onTouchEvent and send them to the other device: 为此,我编写了一个服务器,该服务器获取onTouchEvent的坐标并将其发送到另一台设备:

public boolean onTouchEvent(MotionEvent event) {
    float touchX = event.getX();
    float touchY = event.getY();

    client.sendMessage(touchX+this.getLeft() + " " + touchY+this.getTop() + " " + event.getAction());
    return true;
}

But, because the size of the screens is differnt, the paint is not relative. 但是,由于屏幕的尺寸不同,因此涂料不是相对的。

What can I do about that? 我该怎么办?

tnx and sorry for my bad English. tnx,抱歉我的英语不好。

Solution: 解:

This is the new code: 这是新代码:

public boolean onTouchEvent(MotionEvent event) {
    float touchX = event.getX();
    float touchY = event.getY();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;

    client.sendMessage((touchX)/width + " " + (touchY)/height + " " + event.getAction());
    return true;
}

Dont forget to multiply in the other device. 不要忘记在另一个设备中进行乘法运算。

To make it relative you will have to divide the coordinates by the screen height/width. 为了使其相对,必须将坐标除以屏幕的高度/宽度。

float touchX = event.getX();
float touchY = event.getY();

float relX = touchX / screenWidth;
float relY = touchY / screenHeight;

Then on the other device you will have to multiply is by screen size 然后在另一台设备上,您需要乘以屏幕尺寸

float corX = relX * screenWidth;
float corY = relY * screenHeight;

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

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