android左右上下手势判断

在这个例子中,我们只为演示对手势的检测,对于检测出的手势不做特殊处理,只在日志打印出检测到的结果。

1.activity_main.xml

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context="cn.sehzh.gesture.MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="@string/hello_world" />  
  15.   
  16. </RelativeLayout>  

2.MainActivity

[java]  view plain  copy
  1. package cn.sehzh.gesture;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.GestureDetector;  
  7. import android.view.GestureDetector.OnGestureListener;  
  8. import android.view.MotionEvent;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     protected static final float FLIP_DISTANCE = 50;  
  12.     GestureDetector mDetector;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         mDetector = new GestureDetector(thisnew OnGestureListener() {  
  20.   
  21.             @Override  
  22.             public boolean onSingleTapUp(MotionEvent e) {  
  23.                 // TODO Auto-generated method stub  
  24.                 return false;  
  25.             }  
  26.   
  27.             @Override  
  28.             public void onShowPress(MotionEvent e) {  
  29.                 // TODO Auto-generated method stub  
  30.   
  31.             }  
  32.   
  33.             @Override  
  34.             public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
  35.                 // TODO Auto-generated method stub  
  36.                 return false;  
  37.             }  
  38.   
  39.             @Override  
  40.             public void onLongPress(MotionEvent e) {  
  41.                 // TODO Auto-generated method stub  
  42.   
  43.             }  
  44.   
  45.             /** 
  46.              *  
  47.              * e1 The first down motion event that started the fling. e2 The 
  48.              * move motion event that triggered the current onFling. 
  49.              */  
  50.             @Override  
  51.             public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
  52.                 if (e1.getX() - e2.getX() > FLIP_DISTANCE) {  
  53.                     Log.i("MYTAG""向左滑...");  
  54.                     return true;  
  55.                 }  
  56.                 if (e2.getX() - e1.getX() > FLIP_DISTANCE) {  
  57.                     Log.i("MYTAG""向右滑...");  
  58.                     return true;  
  59.                 }  
  60.                 if (e1.getY() - e2.getY() > FLIP_DISTANCE) {  
  61.                     Log.i("MYTAG""向上滑...");  
  62.                     return true;  
  63.                 }  
  64.                 if (e2.getY() - e1.getY() > FLIP_DISTANCE) {  
  65.                     Log.i("MYTAG""向下滑...");  
  66.                     return true;  
  67.                 }  
  68.   
  69.                 Log.d("TAG", e2.getX() + " " + e2.getY());  
  70.   
  71.                 return false;  
  72.             }  
  73.   
  74.             @Override  
  75.             public boolean onDown(MotionEvent e) {  
  76.                 // TODO Auto-generated method stub  
  77.                 return false;  
  78.             }  
  79.         });  
  80.     }  
  81.   
  82.     @Override  
  83.     public boolean onTouchEvent(MotionEvent event) {  
  84.         return mDetector.onTouchEvent(event);  
  85.     }  
  86. }  

3.日志结果



猜你喜欢

转载自blog.csdn.net/lu_ca/article/details/79419644