简体   繁体   English

Android手势从右到左

[英]Android gesture from right to left

I need to catch the gesture motion from right to left and vice versa (swipe) Why is it not working? 我需要从右向左捕捉手势动作,反之亦然(滑动),为什么它不起作用?

public boolean onTouchEvent(MotionEvent event)
    {

        float x = event.getX();
        float y = event.getY();
        int action = event.getAction();
        int edgeFlags = event.getEdgeFlags();


        switch (edgeFlags)

        {
            case MotionEvent.EDGE_LEFT:
            sex=3;   break;


            case MotionEvent.EDGE_RIGHT:
            sex=1;
                break;

            case MotionEvent.EDGE_TOP:
                break;

            case MotionEvent.EDGE_BOTTOM:
                break;

            default:

                break;
        }

return true;
}

it does not suit me -> How to detect the swipe left or Right in Android? 它不适合我-> 如何在Android中检测向左或向右滑动?

Try with this approach: 尝试使用这种方法:

public abstract class CustomGestureListener extends GestureDetector.SimpleOnGestureListener implements Loggable{ 公共抽象类CustomGestureListener扩展了GestureDetector。SimpleOnGestureListener实现了Loggable {

private final View mView;

public CustomGestureListener(View view){
    mView = view;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    mView.onTouchEvent(e);
    return super.onSingleTapConfirmed(e);
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    onTouch();
    return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if (e1.getX() < e2.getX()) {
        return onSwipeRight();
    }

    if (e1.getX() > e2.getX()) {
        return onSwipeLeft();
    }

    return onTouch();
}

public abstract boolean onSwipeRight();
public abstract boolean onSwipeLeft();
public abstract boolean onTouch();

} }

Check this article for the full source example. 检查文章的完整源例子。

Because edge flags are only returned if the user takes their thumb all the way off the edge of the screen, which is a rarity. 因为仅当用户将拇指完全移开屏幕边缘时才返回边缘标志,因此很少。 You need to dump this approach. 您需要转储此方法。 Instead capture the point where they set their finger down, save the location and the time. 而是捕捉他们放下手指的点,保存位置和时间。 When you get the matching up event, compare the location and time and make sure it was a mostly horizontal move and in a quick enough time frame. 当您获得匹配事件时,请比较位置和时间,并确保这是一次水平移动,并在足够短的时间内完成。 That would be a swipe. 那将是一次轻扫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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