Swipt探测器手势android

时间:2014-04-18 17:08:58

标签: android gesture

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
        return true;
    }
    return super.onTouchEvent(event);
}
private void onLeftSwipe() {
// Do something
    System.out.println("left swipe");
}

private void onRightSwipe() {
// Do something
    System.out.println("right swipe");
}

// Private class for gestures
private class SwipeGestureDetector extends SimpleOnGestureListener {
    // Swipe properties, you can change it to make the swipe
    // longer or shorter and speed
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 200;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            float diffAbs = Math.abs(e1.getY() - e2.getY());
            float diff = e1.getX() - e2.getX();

            if (diffAbs > SWIPE_MAX_OFF_PATH)
            return false;

            // Left swipe
            if (diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                SlidingMenuActivity.this.onLeftSwipe();

                // Right swipe
            } else if (-diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                SlidingMenuActivity.this.onRightSwipe();
            }
        } catch (Exception e) {
            Log.e("YourActivity", "Error on gestures");
        }
        return false;
    }
}

我有,但它不起作用,我无法检测向左或向右滑动。我在课堂上和private GestureDetector gestureDetector;活动方法中声明了onCreate

gestureDetector = new GestureDetector(new SwipeGestureDetector());

但它不起作用,没有打印出来?我正在使用kitkat nexus 4.

3 个答案:

答案 0 :(得分:0)

您必须将侦听器应用于您希望使用的视图,例如:

myView.setOnTouchListener(new SwipeGestureDetector(this)....

答案 1 :(得分:0)

  

尝试更新代码

public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
int SWIPE_THRESHOLD = 200;
int SWIPE_VELOCITY_THRESHOLD = 200;

public OnSwipeTouchListener(Context context) {
    gestureDetector = new GestureDetector(context, new CustomGestureListenerClass());
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class CustomGestureListenerClass extends SimpleOnGestureListener {


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

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        singleClicked(e);
        return super.onSingleTapUp(e);
    }

    @Override
    public boolean onScroll(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float distanceX, float distanceY) {
        return super.onScroll(startMotionEvent, endMotionEvent, distanceX, distanceY);
    }


    @Override
    public boolean onFling(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float velocityX, float velocityY) {

        boolean result = false;
        try {
            float diffY = endMotionEvent.getY() - startMotionEvent.getY();
            float diffX = endMotionEvent.getX() - startMotionEvent.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            }
            result = true;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
    // if you want done some portion before call this method then write here
}

public void onSwipeLeft() {
    // if you want done some portion before call this method then write here
}

public void singleClicked(MotionEvent e) {
}
}

答案 2 :(得分:0)

将OnSwipeTouchListener.java创建为外部类,以实现重用和其他行为

 public class OnSwipeTouchListener implements View.OnTouchListener {

 private final GestureDetector gestureDetector;
 Context context;

 public OnSwipeTouchListener(Context ctx,View mainView) {
gestureDetector = new GestureDetector(ctx, new GestureListener());
mainView.setOnTouchListener(this);
context = ctx;
 }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
     return gestureDetector.onTouchEvent(event);
 }

 public class GestureListener extends GestureDetector.SimpleOnGestureListener {

private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

@Override
public boolean onDown(MotionEvent e) {
    return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    boolean result = false;
    try {
        float diffY = e2.getY() - e1.getY();
        float diffX = e2.getX() - e1.getX();
        if (Math.abs(diffX) > Math.abs(diffY)) {
            if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) >      SWIPE_VELOCITY_THRESHOLD) {
                if (diffX > 0) {
                    onSwipeRight();
                } else {
                    onSwipeLeft();
                }
                result = true;
            }
        } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
            if (diffY > 0) {
                onSwipeBottom();
            } else {
                onSwipeTop();
            }
            result = true;
        }
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return result;
     }
 }

 public void onSwipeRight() {
Toast.makeText(context, "Right", Toast.LENGTH_SHORT).show();
this.onSwipe.swipeRight();
 }

 public void onSwipeLeft() {
Toast.makeText(context, "Left", Toast.LENGTH_SHORT).show();
this.onSwipe.swipeLeft();
 }

 public void onSwipeTop() {
 }

 public void onSwipeBottom() {
 }

 interface onSwipeListener {
void swipeRight();

void swipeLeft();
 }

 ;
 onSwipeListener onSwipe;

 public void setOnSwipeListener(onSwipeListener onSwipeListener) {
this.onSwipe = onSwipeListener;
 }
 }

并在您的主要活动中使用它

  OnSwipeTouchListener onSwipeTouchListener = new OnSwipeTouchListener(this,           findViewById(R.id.main_view));
     onSwipeTouchListener.setOnSwipeListener(new OnSwipeTouchListener.onSwipeListener() {
         @Override
    public void swipeRight() {

    }

    @Override
    public void swipeLeft() {

    }
     });

在activity_main布局中,为主布局设置一个ID,例如main_view

编码不错...