Ido Ikar
Ido Ikar

Reputation: 151

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:

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.

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.

Upvotes: 0

Views: 262

Answers (1)

Luud van Keulen
Luud van Keulen

Reputation: 1254

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;

Upvotes: 1

Related Questions