Android手势识别和双击事件

来源:互联网 发布:网络抓包原理 编辑:程序博客网 时间:2024/05/19 20:22

安卓触摸屏的手势识别在很多时候会使用户操作更加方便,实现原理主要是将该界面的onTouchEvent设置为GestureDetector的onTouchEvent。

具体实现代码:

1.在Activity中响应事件

MainActivity.java

 

Java代码  收藏代码
  1. package com.hu.gesturedemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.GestureDetector;  
  6. import android.view.GestureDetector.SimpleOnGestureListener;  
  7. import android.view.MotionEvent;  
  8. import android.widget.Toast;  
  9.   
  10. public class MainActivity extends Activity  {  
  11.   
  12.     GestureDetector gestureDetector = null;  
  13.       
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.           
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         gestureDetector = new GestureDetector(thisnew SimpleOnGestureListener(){  
  20.               
  21.             @Override  
  22.             public boolean onFling(MotionEvent e1, MotionEvent e2,      //滑动事件  
  23.                     float velocityX, float velocityY) {  
  24.                 if(Math.abs(velocityX) > Math.abs(velocityY)){   //如果X偏移量大于Y偏移量  
  25.                     if(velocityX > 0){  
  26.                         Toast.makeText(MainActivity.this"Right Fling", Toast.LENGTH_SHORT).show();  
  27.                     }else{  
  28.                         Toast.makeText(MainActivity.this"Left Fling", Toast.LENGTH_SHORT).show();  
  29.                     }  
  30.                 }else{  
  31.                     if(velocityY > 0){  
  32.                         Toast.makeText(MainActivity.this"Down Fling", Toast.LENGTH_SHORT).show();  
  33.                     }else{  
  34.                         Toast.makeText(MainActivity.this"Up Fling", Toast.LENGTH_SHORT).show();  
  35.                     }  
  36.                 }  
  37.                 return super.onFling(e1, e2, velocityX, velocityY);  
  38.             }  
  39.               
  40.             @Override  
  41.             public boolean onDoubleTap(MotionEvent e) {     //双击事件  
  42.                 Toast.makeText(MainActivity.this"On double Tap", Toast.LENGTH_SHORT).show();  
  43.                 return super.onDoubleTap(e);  
  44.             }  
  45.         });  
  46.     }  
  47.       
  48.     @Override  
  49.     public boolean onTouchEvent(MotionEvent event) {  
  50.         return gestureDetector.onTouchEvent(event);  
  51.     }  
  52.   
  53. }  

 

 2.在View中响应事件

 MyView.java

Java代码  收藏代码
  1. package com.hu.gesturedemo;  
  2.   
  3. import android.content.Context;  
  4. import android.view.GestureDetector;  
  5. import android.view.GestureDetector.SimpleOnGestureListener;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.widget.Toast;  
  9.   
  10. public class MyView extends View{  
  11.   
  12.     GestureDetector gestureDetector null;  
  13.       
  14.     public MyView(Context context) {  
  15.         super(context);  
  16.         gestureDetector = new GestureDetector(context, new SimpleOnGestureListener(){  
  17.             @Override  
  18.             public boolean onFling(MotionEvent e1, MotionEvent e2,      //滑动事件  
  19.                     float velocityX, float velocityY) {  
  20.                 if(Math.abs(velocityX) > Math.abs(velocityY)){   //如果X偏移量大于Y偏移量  
  21.                     if(velocityX > 0){  
  22.                         Toast.makeText(getContext(), "Right Fling", Toast.LENGTH_SHORT).show();  
  23.                     }else{  
  24.                         Toast.makeText(getContext(), "Left Fling", Toast.LENGTH_SHORT).show();  
  25.                     }  
  26.                 }else{  
  27.                     if(velocityY > 0){  
  28.                         Toast.makeText(getContext(), "Down Fling", Toast.LENGTH_SHORT).show();  
  29.                     }else{  
  30.                         Toast.makeText(getContext(), "Up Fling", Toast.LENGTH_SHORT).show();  
  31.                     }  
  32.                 }  
  33.                 return super.onFling(e1, e2, velocityX, velocityY);  
  34.             }  
  35.               
  36.             @Override  
  37.             public boolean onDoubleTap(MotionEvent e) {     //双击事件  
  38.                 Toast.makeText(getContext(), "On double Tap", Toast.LENGTH_SHORT).show();  
  39.                 return super.onDoubleTap(e);  
  40.             }  
  41.               
  42.         });  
  43.           
  44.         this.setClickable(true);  
  45.         this.setOnTouchListener(new OnTouchListener() {  
  46.               
  47.             @Override  
  48.             public boolean onTouch(View v, MotionEvent event) {  
  49.                 return gestureDetector.onTouchEvent(event);  
  50.             }  
  51.         });  
  52.     }  
  53. }  

 



 

  • gestureDemo.zip (1012.6 KB)
  • 下载次数: 12
0 0
原创粉丝点击