Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture finger movement direction in android phone?

Tags:

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.

like image 519
understack Avatar asked Jun 30 '10 11:06

understack


People also ask

How do I get touch coordinates on Android?

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.

What is pointer in Android?

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.

Which method you should override to control your touch action in Android?

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.


1 Answers

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";         }     } } 
like image 83
cstack Avatar answered Sep 28 '22 09:09

cstack