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; } };
Matt clark
source share