简体   繁体   中英

Co-ordinates of the ACTION_UP and ACTION_POINTER_UP event

I am trying to check what is touching the screen whenever it is pressed, a pointer is moved, or a touch is released.

The problem is that when a touch is released, getPointerCount() includes the pointer that just left the screen in its count. And so when I try to gather all the current X and Y values, it includes an x/y pair that has left the screen.

I am currently doing something like:

int count = e.getPointerCount();
for(i = 0; i < count; i++) {
    x = e.getX(i); 
    y = e.getY(i); 
    /* do stuff with them */ 
}

Is there a way of getting the X and Y coordinates of that pointer that left the screen, or remove the pointer that left the screen and get only coordinates that are touching the screen?

Thanks

Have you tried using the onTouchEvent in your application?

import android.view.MotionEvent;

...

private int mActivePointerId;

protected void onCreate(Bundle savedInstanceState){
    ...
}

public boolean onTouchEvent(MotionEvent event){

    // Get the pointer ID
    mActivePointerId = event.getPointerId(0);

    // ... Many touch events later...

    // Use the pointer ID to find the index of the active pointer 
    // and fetch its position

    int pointerIndex = event.findPointerIndex(mActivePointerId);

    // Get the pointer's current position
    float x = event.getX(pointerIndex);
    float y = event.getY(pointerIndex);

    return true;

}

I think the method you want is getActionIndex() . From the documentation, it seems that when getActionMasked() returns ACTION_POINTER_UP , this method will return the corresponding pointer index and you can then ignore it in your loop (or do something else with it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM