I want to capture the finger movement direction on Android touch phone. If a user slides his finger in up/down/left/right direction, I want to capture this direction. How can I find this? Thanks.
setOnTouchListener(handleTouch); This gives you the touch event coordinates relative to the view that has the touch listener assigned to it. The top left corner of the view is (0, 0) . If you move your finger above the view, then y will be negative.
Pointer capture is a feature available in Android 8.0 (API level 26) and later that provides such control by delivering all mouse events to a focused view in your app.
1.1. You can react to touch events in your custom views and your activities. Android supports multiple pointers, e.g. fingers which are interacting with the screen. The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method. you override the onTouchEvent() method.
Implement onTouchEvent(), and calculate dx and dy by where the user presses down and lifts up. You can use these values to figure out the direction of the move.
float x1, x2, y1, y2, dx, dy; String direction; switch(event.getAction()) { case(MotionEvent.ACTION_DOWN): x1 = event.getX(); y1 = event.getY(); break; case(MotionEvent.ACTION_UP): { x2 = event.getX(); y2 = event.getY(); dx = x2-x1; dy = y2-y1; // Use dx and dy to determine the direction of the move if(Math.abs(dx) > Math.abs(dy)) { if(dx>0) direction = "right"; else direction = "left"; } else { if(dy>0) direction = "down"; else direction = "up"; } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With