Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does detecting touch position on android screen work?

We can get the coordinates of the touch position on the screen using the following functions:

public boolean onTouchEvent(MotionEvent event){
    float x_forOnTouch = event.getX();
    float y_forOnTouch = event.getY();  

What exactly is being returned by the getX() and getY() functions? Is it returning pixel values? When a finger touches a screen position, it will probably touch a number of pixels. So what exactly does the function return?

Again, let us suppose that in the area I touch with my finger, there are multiple pixels on the screen, as shown below:
enter image description here

Suppose I need to click and drag the point at pixel 1, but the area my finger touches covers pixel 1 and 2. How do I accurately detect which pixel has been clicked, so that I can click and drag on pixel 1 or 2 when I need it?

like image 260
user13267 Avatar asked Sep 14 '13 19:09

user13267


1 Answers

Well, that can be of help to you: http://developer.android.com/reference/android/view/MotionEvent.html

I suppose the values are the center of the blob or area of the touch. Remember that it can be a tool like a mouse or a stylus so the area will be more precise. It has methods and constants for the supposed area of the finger that is touching.

The link has so many methods about that question, that I will not copy and paste, as its pretty documented, so I think you need to read it.

Some of then: getPressure() getSize() getPointerCount() getXPrecision()

About the returned value, the section Device Types will give plenty of information.

Some relevant ones:

The interpretation of the contents of a MotionEvent varies significantly depending on the source class of the device.

On pointing devices with source class SOURCE_CLASS_POINTER such as touch screens, the pointer coordinates specify absolute positions such as view X/Y coordinates. Each complete gesture is represented by a sequence of motion events with actions that describe pointer state transitions and movements.

On joystick devices with source class SOURCE_CLASS_JOYSTICK, the pointer coordinates specify the absolute position of the joystick axes. The joystick axis values are normalized to a range of -1.0 to 1.0

like image 77
Diego C Nascimento Avatar answered Oct 28 '22 06:10

Diego C Nascimento