Reputation: 37
I'm trying to create an accurate ruler on Android for any device and I've run into a problem. Basically, my measurements are too far off for my two different devices. I expected this to be the case but not for the degree that it's happening.
Currently for onTouchEvent I just print the x location in inches with the following code
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float x = event.getX()/metrics.xdpi;
System.out.println(x);
return true;
and I'm getting the following results
https://i.sstatic.net/y54Lo.jpg (3.01 should be 3.10)
Basically, my emulator is constantly .1 inchs off and my galaxy s5 continuously goes up by about .5 inchs
For these results, I'm using a marker width of 1dp and a marker separation of 8 dp. This gives me the most accurate results on my s5.
10dp is 0.625 inchs which is how far an inch should be separated between each marker.
Despite 1dp width and 8dp separation giving me accurate results on my S5, 1dp width and 9dp separation(what it should probably be) gives me perfect results on the android emulator. However, on my S5 the measurements are off by 0.16 inchs per inch.
I'm honestly lost on what to do. I want this to be as accurate as possible between all devices.
Upvotes: 0
Views: 46
Reputation: 93569
Trust the real device on all things over an emulator. But as for exact measurements- you won't get them perfectly. A dp is 1/160th of an inch, but real dpis aren't that clean- a galaxy s5 is 432. 1 dp would be 2.7 pixels That means 8 dp is 21.6 pixels. So you'd be off by .6/21.6 or 2.7%.
If you're seeing increasing error over the course of your ruler, that can be fixed- its likely in how you're doing your math allowing additive error. But you'd need to post your onDraw to fix it.
Upvotes: 0