Failed to get the correct xy value in onTouch

I want to move the image while dragging around the screen, but the x and y values ​​in ACTION_MOVE are incorrect. I printed the value of x and y in ACTION_MOVE. In each alternative, it prints the correct x and y values.

public boolean onTouch(View v, MotionEvent event) { ImageView view = (ImageView) v; float x=0; float y=0; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_MOVE: x=event.getX(); y=event.getY(); System.out.println("x= "+x+" y="+y); int width = 100, height = 100; lp = new AbsoluteLayout.LayoutParams(width, height, (int) (x), (int) (y)); break; } mLayout.updateViewLayout(view, lp); return true; } 

This is my code. And printed output:

 01-17 10:31:11.721: I/System.out(3983): x= 71.0 y=91.0 01-17 10:31:11.746: I/System.out(3983): x= 102.0 y=24.0 01-17 10:31:11.776: I/System.out(3983): x= 72.0 y=91.0 01-17 10:31:11.806: I/System.out(3983): x= 103.0 y=24.0 01-17 10:31:11.826: I/System.out(3983): x= 73.0 y=91.0 01-17 10:31:11.851: I/System.out(3983): x= 104.0 y=24.0 01-17 10:31:11.871: I/System.out(3983): x= 74.0 y=91.0 01-17 10:31:11.896: I/System.out(3983): x= 105.0 y=24.0 01-17 10:31:11.921: I/System.out(3983): x= 75.0 y=91.0 01-17 10:31:11.941: I/System.out(3983): x= 107.0 y=24.0 01-17 10:31:11.957: I/System.out(3983): x= 76.0 y=90.0 01-17 10:31:11.976: I/System.out(3983): x= 109.0 y=24.0 01-17 10:31:11.996: I/System.out(3983): x= 77.0 y=90.0 01-17 10:31:12.026: I/System.out(3983): x= 110.0 y=24.0 01-17 10:31:12.056: I/System.out(3983): x= 78.0 y=90.0 01-17 10:31:12.101: I/System.out(3983): x= 111.0 y=24.0 01-17 10:31:12.131: I/System.out(3983): x= 79.0 y=89.0 01-17 10:31:12.186: I/System.out(3983): x= 112.0 y=24.0 01-17 10:31:12.231: I/System.out(3983): x= 80.0 y=89.0 01-17 10:31:12.276: I/System.out(3983): x= 113.0 y=24.0 01-17 10:31:12.296: I/System.out(3983): x= 80.0 y=88.0 01-17 10:31:12.316: I/System.out(3983): x= 114.0 y=24.0 01-17 10:31:12.336: I/System.out(3983): x= 81.0 y=88.0 

Alternative points are correct. But how is this going? How can I solve this problem?

+4
source share
1 answer

If you answer this question , he says that Views may have a built-in touch event that may interfere with your touch event. Check if you have one view.

+1
source

Source: https://habr.com/ru/post/1391411/


All Articles