GestureDetector手势识别的各个方法

package com.example.velocity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;

public class MainActivity extends ActionBarActivity implements OnGestureListener, OnDoubleTapListener {
    private RelativeLayout rl;
    private GestureDetector gd; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rl = (RelativeLayout) findViewById(R.id.rl);
        gd = new GestureDetector(this);
        gd.setOnDoubleTapListener(this);
        rl.setOnTouchListener(new OnTouchListener() {

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

    
    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        System.out.println("down");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub
        System.out.println("没松开或者拖动");
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        System.out.println("单击行为");
        return true;
    }
    //滑动中触发
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
//        System.out.println("onScroll");
//        if(e1.getRawX()-e2.getRawX()<0){
//            System.out.println("右滑");
//        }else if(e1.getRawX()-e2.getRawX()>0){
//            System.out.println("左滑");
//        }
        return false;
    }
    //长按
    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub
        
    }
    //滑动结束触发
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        if(e1.getRawX()-e2.getRawX()<-300){
            System.out.println("右滑");
        }else if(e1.getRawX()-e2.getRawX()>300){
            System.out.println("左滑");
        }
        return true;
    }

    
    //严格的单击行为
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
    //双击,不可能和onSingleTapConfirmed共同
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
    
    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}

 

posted @ 2016-05-04 10:10  aaddrrooiidd  阅读(214)  评论(0编辑  收藏  举报