Reputation: 350
In my app, the user will put 2 fingers on the screen. I want to calculate the distance between those 2 touch points in centimeters. Currently, I'm able to calculate the distance using x and y values provided in onTouchEvent
. But that distance is not physical distance. Any suggestions?
Upvotes: 1
Views: 1380
Reputation: 4840
Using your calculated pixel distances x
and y
you can use the following to return distance in inches:
public double getScreenDistance(double x1, double y1, double x2, double y2) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double xDist = Math.pow(Math.abs(x1 - x2) / dm.xdpi, 2);
double yDist = Math.pow(Math.abs(y1 - y2) / dm.ydpi, 2);
return Math.sqrt(xDist + yDist);
}
Then to convert to centimeters just multiply by 2.54.
Upvotes: 1