手势关闭页面GestureDetector

final int RIGHT = 0;
private GestureDetector gestureDetector;
final int LEFT = 1;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);
    gestureDetector = new GestureDetector(Web.this, onGestureListener);
}
private GestureDetector.OnGestureListener onGestureListener=new GestureDetector.SimpleOnGestureListener(){
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        float x = e2.getX() - e1.getX();//滑动后的x值减去滑动前的x值 就是滑动的横向水平距离(x)
        float y = e2.getY() - e1.getY();//滑动后的y值减去滑动前的y值 就是滑动的纵向垂直距离(y)

        if (x > 100) {
            doResult(RIGHT);
            Log.w("tag", "RIGHT>" + x);
        }
        if (x < -100) {
            Log.w("tag", "LEFT>" + x);
            doResult(LEFT);
        }

        return true;
    }
};
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            System.out.println(" ACTION_DOWN");//手指在屏幕上按下
            break;
        case MotionEvent.ACTION_MOVE:
            overridePendingTransition(R.anim.you, R.anim.zuo);
            System.out.println(" ACTION_MOVE");//手指正在屏幕上滑动
            break;
        case MotionEvent.ACTION_UP:

            System.out.println(" ACTION_UP");//手指从屏幕抬起了
            break;
        default:
            break;
    }

    return gestureDetector.onTouchEvent(event);
}

@Override
//注意这里不能用ONTOUCHEVENT方法,不然无效的
  public boolean dispatchTouchEvent(MotionEvent ev) { gestureDetector.onTouchEvent(ev) ;
//这几行代码也要执行,将webview载入MotionEvent对象
  webView.onTouchEvent(ev) ; return super.dispatchTouchEvent(ev) ;} public void doResult( int action) { switch (action) { case RIGHT: System. out.println( "go right") ; finish() ; break; case LEFT: System. out.println( "go left") ; break; }}
或者参考这个网址
http://www.cnblogs.com/yejiurui/p/3803658.html

你可能感兴趣的:(手势关闭页面GestureDetector)