简体   繁体   中英

Decide if touch position is left, top, right or down from specific point

How do I decide if touch position of user input is (more likely) left, top, right or bottom from specific point? I know how to get position of touch, but don't know how to decide which "way" it is.

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        //...
    }
    return false;
}

Demonstration image:

在此处输入图片说明

Assuming those are 45 degree angles, then

dy = pointY- currentY
dx = pointX- currentX
if(abs(dy) > abs (dx) && dy <0 ) return bottom
else if(abs(dy) - abs (dx)) return top
else if(dx < 0) return right
else if (dx > 0) return left
else return same point

If those aren't 45 degree angles, you're going to have to use trig to figure out where the boundaries are.

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