1. 通过Layout控件捕捉onTouch事件,所以要实现OnTouchListener接口;
  2. 当用户触摸屏幕的时候,会产生许多手势,这里就包括滑动效果。通过GestureDetector类,我们可以识别很多的手势。所以要实现GestureDetector.OnGestureListener接口(或者GestureDetector.SimpleOnGestureListener),将Touch事件传入GestureDetector对象进行处理。




  • 实现两个接口


public class MyActivity extends Activity implements View.OnTouchListener,GestureDetector.OnGest- ureListener {
 
  
    private RelativeLayout mLinearLayout;
 
  
    private GestureDetector mGestureDetector;
 
  

 
  
    @Override
 
  
    protected void onCreate(Bundle savedInstanceState) {
 
  
        super.onCreate(savedInstanceState);
 
  
        setContentView(R.layout.activity_face_idcard_identify);
 
  
        findView();
 
  
    }
 
  
• 给控件注册OnTouch事件,并且允许响应长点击事件
 
  
    
   private void findView() {
 
  
        mLinearLayout = (RelativeLayout) findViewById(R.id.Face_IDCard_Layout);
 
  
        mLinearLayout.setOnTouchListener(this);
 
  
        mLinearLayout.setLongClickable(true);
 
  
        mGestureDetector = new GestureDetector(this, this);
 
  
    }
 
  
• 实现手势识别接口中的方法

 
         
    //用户按下屏幕就会触发: 
  
    @Override
 
  
    public boolean onDown(MotionEvent e) {
 
  
        return false;
 
  
    }
 
  
      
   //短按触摸屏
 
  
    @Override
 
  
    public void onShowPress(MotionEvent e) {
 
  

 
  
    }
 
  
      
   //点击屏幕后抬起时触发该事件
 
  
    @Override
 
  
    public boolean onSingleTapUp(MotionEvent e) {
 
  
        return false;
 
  
    }
 
  
      
   //在屏幕上拖动控件
 
  
    @Override
 
  
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
 
  
        return false;
 
  
    }
 
  
      //长按触摸屏
 
  
    @Override
 
  
    public void onLongPress(MotionEvent e) {
 
  

 
  
    }
 
  
    
     //滑屏,用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个        //ACTION_UP触发;参数分别表示:按下事件、抬起事件、x方向移动速度、y方向移动速度。  
    
 
  
    @Override
 
  
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
 
  
        final int FLING_MIN_DISTANCE = 100;
 
  
        final int FLING_MIN_VELOCITY = 200;
 
  
        if (Math.abs((int) (e1.getX() - e2.getX())) > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { 
   //左滑右滑皆可
 
  
            Intent intent = new Intent(this, VideoMatchIdentifyGlassesActivity.class);
 
  
            startActivity(intent);
 
  
            finish();
 
  
        }
 
  
        return false;
 
  
    }
 
  
• 实现OnTouchListener接口中的方法
 
  
    
   @Override
 
  
    public boolean onTouch(View v, MotionEvent event) {
 
  
        return mGestureDetector.onTouchEvent(event);
 
  
    }
 
  
}