刷卡左,右,上,下​​根据速度或其他变量 [英] swipe left, right, up and down depending on velocity or other variables

查看:189
本文介绍了刷卡左,右,上,下​​根据速度或其他变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了手势,从简单的扩展一个类,我与onfling方法工作:

i've got a class extended from simple on gesture and i'm working with the onfling method:

class MyGestureListener extends GestureDetector.SimpleOnGestureListener{
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        float e1_X = e1.getX();
        float e1_Y = e1.getY();
        float e2_X = e1.getX();
        float e2_Y = e2.getY();
        if(velocityX > 0 && velocityX > velocityY){
            text.setText("Swipe left");
        }else if(velocityX < 0 && velocityX < velocityY){
            text.setText("Swipe right");
        }else if(velocityY < 0 && velocityX > velocityY){
            text.setText("Swipe down");
        }else if(velocityY > 0 && velocityX < velocityY){
            text.setText("Swipe up");
        }
        return super.onFling(e1, e2, velocityX, velocityY);
    }
}

我知道这取决于某些角度,但我不能做到这一点,我试图与velocityX和velocityY,它的工作只是如果你这样做precisely。但我想要的是错误的角度:如果您例如对角线滑动向上和向右我需要选择这是很好的方式。

I know it depends on certain angles but i can't do it, i tried with the velocityX, and velocityY, it's working only if you do it precisely . But what i want is an angle of "mistake" : if you swipe diagonally for example up and right i need to choose which is the good way.

推荐答案

您应该检查的速度和距离。这里是水平滑动检测器的一个例子。您可以以同样的方式增加垂直检测。

You should check speed and distance. Here is an example of horizontal swipe detector. You can add vertical detection in the same manner.

public class HSwipeDetector extends SimpleOnGestureListener {
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {  return false;  }

        /* positive value means right to left direction */
        final float distance = e1.getX() - e2.getX();
        final boolean enoughSpeed = Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY;
        if(distance > SWIPE_MIN_DISTANCE && enoughSpeed) {
            // right to left swipe
            onSwipeLeft();
            return true;
        }  else if (distance < -SWIPE_MIN_DISTANCE && enoughSpeed) {
            // left to right swipe
            onSwipeRight();
            return true;
        } else {
            // oooou, it didn't qualify; do nothing
            return false;
        }
    }

    protected void onSwipeLeft() { 
        // do your stuff here
    }

    protected void onSwipeRight() {   
        // do your stuff here
    }
}

这篇关于刷卡左,右,上,下​​根据速度或其他变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆