Strange offset in getLocationInWindow/getLocationOnScreen/getHitRect

631 views Asked by At

I am working on an Android application that needs to handle taps on the screen and translate them to a geographic position on a map. For this I have a helper method that helps me adjust the point in which the tap was registered for the actual location on the screen. As such, I have this code:

int[] offset = new int[2];
getLocationInWindow(offset);
point.x += offset[0];
point.y += offset[1];

This bit of code does exactly what I want and has done for about 2-3 years. The app has correctly handled these translations during all this time. However, for some reason it is going wrong on the HTC One we recently tried to test it on. The way I figure it might be Android 4.3 related (as it's our only 4.3 device) or related to the fact that the One has a softbutton on the screen. The issue I'm having is that every touch that is registered, is registered about 20-30 pixels lower than it should be. The discrepancy is always to the bottom, but like I said the only device I can reproduce it on is this one device.

I have tried using getLocationOnScreen instead, and I've also had this code in place:

Rect r = new Rect();
getHitRect(r);
point.x += r.left;
point.y += r.top;

All of these result in the same problem. Any other adjustment I've tried to make seems to fix this problem, yet create the opposite problem on all other test devices. I'm running out of options.

0

There are 0 answers