Java 手势检测器-方法位于下方,但位于左侧

Java 手势检测器-方法位于下方,但位于左侧,java,android,Java,Android,我有一个用文本显示土司的方法。 我在方法onDown()中从GetureDetector @Override public boolean onDown(MotionEvent motionEvent) { //here i have method for this toast return false; } 现在当我们触摸屏幕时,我们可以看到来自Toast的文本 现在我想做一些类似的事情,但只有当我们触摸并将你的手指向左滑动时。我在doc中找不到这样的方法。 我怎样才能做到这

我有一个用文本显示土司的方法。 我在方法
onDown()
中从
GetureDetector

@Override
public boolean onDown(MotionEvent motionEvent) {
    //here i have method for this toast
    return false;
}
现在当我们触摸屏幕时,我们可以看到来自Toast的文本

现在我想做一些类似的事情,但只有当我们触摸并将你的手指向左滑动时。我在doc中找不到这样的方法。

我怎样才能做到这一点?

您可以通过检查motionEvent中的坐标来做到这一点

view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = 0;
        switch (event.getAction()){

            case MotionEvent.ACTION_DOWN:
                x = event.getX();
                Log.e("x",x+"");
                return true;
            case MotionEvent.ACTION_MOVE:
                Log.e("newX",event.getX()+"");
                if (event.getX() < (x-20)){
                    // toast here
                    return false;
                }
                break;
        }
        return false;
    }
});
view.setOnTouchListener(新的view.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
浮动x=0;
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
x=event.getX();
Log.e(“x”,x+”);
返回true;
case MotionEvent.ACTION\u移动:
Log.e(“newX”,event.getX()+”);
if(event.getX()<(x-20)){
//敬酒
返回false;
}
打破
}
返回false;
}
});

要检测刷卡事件,您需要重写
SimpleOnGestureListener#onFling(MotionEvent,MotionEvent,float,float)
方法。以下是一个例子:

public class MyActivity extends Activity {
    private GestureDetector detector;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.swipe_tester);
        detector = new GestureDetector(this, new SwipeListener());
    }     

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        boolean consumed = detector.onTouchEvent(e);
        if (consumed) {return true;} 
        return super.onTouchEvent(e);
    }

    public class SwipeListener extends GestureDetector.SimpleOnGestureListener {
         public static final int MIN_SWIPE_DISTANCE = 40; 

        /**
         * Whether or not you use {@link GestureDetector.SimpleOnGestureListener},
         * you must always implement an {@link OnGestureListener#onDown(MotionEvent)}
         * method that returns {@code true}. This step is necessary because all gestures
         * begin with an {@code onDown()} message.
         *
         * For more details see
         * https://developer.android.com/training/custom-views/making-interactive.html#inputgesture
         */
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        /**
         * @return {@code true} if event is consumed by this method.
         */
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
           float xDiff = e1.getX() - e2.getX();
           float yDiff = e1.getY() - e2.getY();
           return resolveSwipe(xDiff, yDiff);
        }

        /**
         * @return {@code true} if swipe movement was detected.
         */
        private boolean resolveSwipe(float xDist, float yDist) {
            float yDistAbs = Math.abs(yDist);
            float xDistAbs = Math.abs(xDist);

            SwipeDirection swipeDirection;

            // movement along y axis is dominant = resolve as vertical movement
            if (yDistAbs > xDistAbs) {
                if (yDistAbs < MIN_SWIPE_DISTANCE) {return false;}
                swipeDirection = (yDist > 0) ? SwipeDirection.DOWN: SwipeDirection.UP;

            } else {
                if (xDistAbs < MIN_SWIPE_DISTANCE) {return false;}
                swipeDirection = (xDist > 0) ? SwipeDirection.RIGHT: SwipeDirection.LEFT;
            }

            onSwipeEvent(swipeDirection);
            return true;
        }

        private void onSwipeEvent(SwipeDirection swipeDirection) {
            if (swipeDirection == SwipeDirection.UP) {
                Log.d("[MATONI]", "[CLASS]: " + SwipeListener.class.getSimpleName() + ", [MESSAGE]: UP");
                Toast.makeText(TestActivity.this, "UP",  Toast.LENGTH_SHORT).show();
                return;
            }
            if (swipeDirection == SwipeDirection.DOWN) {
                Log.d("[MATONI]", "[CLASS]: " + SwipeListener.class.getSimpleName() + ", [MESSAGE]: DOWN");
                Toast.makeText(TestActivity.this, "DOWN",  Toast.LENGTH_SHORT).show();
                return;
            }
            if (swipeDirection == SwipeDirection.LEFT) {
                Log.d("[MATONI]", "[CLASS]: " + SwipeListener.class.getSimpleName() + ", [MESSAGE]: LEFT");
                Toast.makeText(TestActivity.this, "LEFT",  Toast.LENGTH_SHORT).show();
                return;
            }
            if (swipeDirection == SwipeDirection.RIGHT) {
                Log.d("[MATONI]", "[CLASS]: " + SwipeListener.class.getSimpleName() + ", [MESSAGE]: RIGHT");
                Toast.makeText(TestActivity.this, "RIGHT",  Toast.LENGTH_SHORT).show();
                return;
            }
            // or alternatively
            //switch (swipeDirection) {
            //    case UP: Toast.makeText(TestActivity.this, "UP", Toast.LENGTH_SHORT).show();
            //        break;
            //    case DOWN: Toast.makeText(TestActivity.this, "DOWN", Toast.LENGTH_SHORT).show();
            //        break;
            //    case LEFT: Toast.makeText(TestActivity.this, "LEFT", Toast.LENGTH_SHORT).show();
            //        break;
            //    case RIGHT: Toast.makeText(TestActivity.this, "RIGHT", Toast.LENGTH_SHORT).show();
            //        break;
            //    default: new AssertionError("Should not happen");
            //}
        }
    }

    public enum SwipeDirection {
        UP, DOWN, LEFT, RIGHT
    }
}
公共类MyActivity扩展活动{
私人手势检测器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.swipe_tester);
detector=新的GestureDetector(这是新的SwipeListener());
}     
@凌驾
公共事件(运动事件e){
布尔消耗=检测器。onTouchEvent(e);
如果(消耗){返回true;}
返回super.onTouchEvent(e);
}
公共类SwipeListener扩展了GestureDetector.SimpleOnGestureListener{
公共静态最终整数最小滑动距离=40;
/**
*无论是否使用{@link gesturedector.SimpleOnGestureListener},
*您必须始终实现一个{@link OnGestureListener#onDown(MotionEvent)}
*方法返回{@code true}。此步骤是必需的,因为所有手势
*以{@code onDown()}消息开始。
*
*有关更多详细信息,请参阅
* https://developer.android.com/training/custom-views/making-interactive.html#inputgesture
*/
@凌驾
公共布尔onDown(运动事件e){
返回true;
}
/**
*@return{@code true}如果此方法使用了事件。
*/
@凌驾
公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY){
float xDiff=e1.getX()-e2.getX();
float yDiff=e1.getY()-e2.getY();
返回resolveSwipe(xDiff、yDiff);
}
/**
*@return{@code true}如果检测到刷卡移动。
*/
专用布尔解析滑动(浮点xDist、浮点yDist){
float yDistAbs=Math.abs(yDist);
float xDistAbs=Math.abs(xDist);
SwipeDirection SwipeDirection;
//沿y轴的移动占主导地位=解析为垂直移动
如果(yDistAbs>xDistAbs){
如果(yDistAbs<最小滑动距离){返回false;}
swipeDirection=(yDist>0)?swipeDirection.DOWN:swipeDirection.UP;
}否则{
如果(xDistAbs<最小滑动距离){return false;}
swipeDirection=(xDist>0)?swipeDirection.RIGHT:swipeDirection.LEFT;
}
OnSwipedEvent(swipeDirection);
返回true;
}
私有无效onSwipeEvent(SwipeDirection SwipeDirection){
if(swipeDirection==swipeDirection.UP){
Log.d(“[MATONI]”,“[CLASS]:“+SwipeListener.CLASS.getSimpleName()+”,[MESSAGE]:UP”);
Toast.makeText(TestActivity.this,“UP”,Toast.LENGTH_SHORT.show();
返回;
}
if(swipeDirection==swipeDirection.DOWN){
Log.d(“[MATONI]”,“[CLASS]:“+SwipeListener.CLASS.getSimpleName()+”,[MESSAGE]:DOWN”);
Toast.makeText(TestActivity.this,“DOWN”,Toast.LENGTH_SHORT.show();
返回;
}
if(swipeDirection==swipeDirection.LEFT){
Log.d(“[MATONI]”,“[CLASS]:“+SwipeListener.CLASS.getSimpleName()+”,[MESSAGE]:左”);
Toast.makeText(TestActivity.this,“LEFT”,Toast.LENGTH_SHORT.show();
返回;
}
if(swipeDirection==swipeDirection.RIGHT){
Log.d(“[MATONI]”,“[CLASS]:“+SwipeListener.CLASS.getSimpleName()+”,[MESSAGE]:右”);
Toast.makeText(TestActivity.this,“RIGHT”,Toast.LENGTH_SHORT.show();
返回;
}
//或者
//开关(开关方向){
//大小写:Toast.makeText(TestActivity.this,“UP”,Toast.LENGTH_SHORT.show();
//中断;
//大小写向下:Toast.makeText(TestActivity.this,“DOWN”,Toast.LENGTH_SHORT.show();
//中断;
//案例左:Toast.makeText(TestActivity.this,“LEFT”,Toast.LENGTH_SHORT.show();
//中断;
//大小写右侧:Toast.makeText(TestActivity.this,“RIGHT”,Toast.LENGTH_SHORT.show();
//中断;
//默认:新断言错误(“不应该发生”);
//}
}
}
公共枚举开关定向{
上、下、左、右
}
}

有关
GestureDetector
的更多详细信息,请参阅。

androidStudio underline getX“非静态方法getX()不能从静态上下文引用”您是否可以将代码发布到此处,因为您一定犯了一些错误。它对我来说总是很好“y”从来不用,所以为什么我们需要y?代码太多了,我不能