0

I'm building an Android app where the user has to touch a circle on the screen, and I need to record how far away the user's touch was from the center of the circle.

Thing is, I want to record the physical distance, not the pixel distance, because I'm going to run this app on many devices and I want all the results to be uniform. For example, if the user's touch distance from the center of the circle is 15px, this could be a physical distance of 0.5cm on one device, and 1cm on another (just made up those numbers, but you get the idea). So I can only rely on physical measurements, not pixels.

So how should I record the touch distances? The View.onTouchEvent method gives you the x and y of a touch event in pixels (I'm assuming), so do I just get the distance in pixels from the center of the circle to the touch, and then convert that to dp (and store all results in dp units)? Or should I convert the pixel distance to pt, which is supposed to be the same physical size on any device? If so, how does one convert from px to pt (I can't seem to find the answer to this anywhere)?

XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151

1 Answers1

0

You could use DisplayMetrics to get the screen size in pixels and the DPI of the screen and then divide the width and height by the dpi to get the number of inches wide and high, and use that to convert the pixel distance to real world units...

If that makes sense.

Link to DisplayMetrics: http://developer.android.com/reference/android/util/DisplayMetrics.html

jbenscoter
  • 171
  • 1
  • 2
  • 11