Android coordinates onTouchEvent Skip

In my application, I have an element that requires the user to move it around the screen, and for this I use RelativeLayout and onTouchListener.

The problem I am facing is that inside my onTouchListener I get some weird results for getX() and getY() .

I added the following line of code to my onTouch() ACTION_MOVE :

Log.v("example", "Touch: {x:" + event.getX() + " } {y:" + event.getY() + "}");

Below are my LogCat results.

04-29 03: 16: 11.000: V / myApp (24188): Touch Down!
04-29 03: 16: 11.057: V / myApp (24188): Touch: {x: 69.78699} {y: 107.774216}
04-29 03: 16: 11.103: V / myApp (24188): Touch: {x: 77.86926} {y: 173.37648}
04-29 03: 16: 11.158: V / myApp (24188): Touch: {x: 69.78699} {y: 108.781845}
04-29 03: 16: 11.205: V / myApp (24188): Touch: {x: 77.86926} {y: 174.38597}
04-29 03: 16: 11.463: V / myApp (24188): Touch: {x: 69.78699} {y: 109.64778}
04-29 03: 16: 11.502: V / myApp (24188): Touch: {x: 77.86926} {y: 175.62172}
04-29 03: 16: 11.596: V / myApp (24188): Touch: {x: 69.23099} {y: 109.40666}
04-29 03: 16: 11.654: V / myApp (24188): Touch Up!

As you can see, moving my finger very slowly in a straight line, I get a clumsy result, where the correct coordinates are virtually every other line.

This causes my image to jump backward and the fourth between the desired position and the offset position when I drag it around the screen.

I added to the checks to make sure that I create only one element, and this this listener is not called anywhere except for this single view.

Has anyone else seen this problem before, and is there any idea what I can do to fix this?

EDIT

 View.OnTouchListener onTouch = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.d("example", "Touch down"); break; case MotionEvent.ACTION_UP: Log.d("example", "Touch up"); break; case MotionEvent.ACTION_MOVE: Log.v("example", "Touch: {x:" + event.getX() + " } {y:" + event.getY() + "}"); } return true; } }; 
+7
source share
3 answers

I was able to solve this problem using event.getRawX() and event.getRawY() .

+7
source

Are you returning true inside onTouch () after handling the ACTION_MOVE event? Moreover, as the official Android reference explains, β€œFor events with motion efficiency, you can combine multiple motion samples within the same object.” Therefore, you should use getX () and getY () only to get the latest coordinates. To access earlier coordinates, you can use the getHistoricalX (int) and getHistoricalY (int) methods. For more information, read the official link.

+3
source

MotionEvent sometimes returns the absolute X and Y coordinates relative to the view, and sometimes the relative coordinates to the previous motion event.

 getRawX() and getRawY() that is guaranteed to return absolute coordinates, relative to the device screen. While getX() and getY(), should return you coordinates, relative to the View, that dispatched them. 

cause

@ Reply Matt Clark

+2
source

All Articles