天天看點

Android OnGestureListener用法 識别使用者手勢 左右滑動

android可以識别使用者的手勢(即使用者用手指滑動的方向),通過使用者不同的手勢,進而做出不同的處理

需要使用OnGestureListener

比如說看電子書的時候翻頁,或者要滑動一些其他内容

直接上代碼

界面檔案

main.xml

view plain copy to clipboard print ?

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:id="@+id/ll"  
  7.     >   
  8. <TextView     
  9.     android:layout_width="fill_parent"    
  10.     android:layout_height="wrap_content"    
  11.     android:text="@string/hello"  
  12.     />   
  13. </LinearLayout>  

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/ll"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

</LinearLayout>

主Activity

view plain copy to clipboard print ?

  1. package zy.lucifer.testGesture;   
  2. import android.app.Activity;   
  3. import android.os.Bundle;   
  4. import android.util.Log;   
  5. import android.view.GestureDetector;   
  6. import android.view.MotionEvent;   
  7. import android.view.View;   
  8. import android.view.GestureDetector.OnGestureListener;   
  9. import android.view.View.OnTouchListener;   
  10. import android.widget.LinearLayout;   
  11. import android.widget.TextView;   
  12. import android.widget.Toast;   
  13. public class testGesture extends Activity implements OnTouchListener,   
  14.         OnGestureListener {   
  15.     GestureDetector mGestureDetector;   
  16.     private static final int FLING_MIN_DISTANCE = 50;   
  17.     private static final int FLING_MIN_VELOCITY = 0;   
  18.     @Override   
  19.     public void onCreate(Bundle savedInstanceState) {   
  20.         super.onCreate(savedInstanceState);   
  21.         setContentView(R.layout.main);   
  22.         mGestureDetector = new GestureDetector(this);   
  23.         LinearLayout ll=(LinearLayout)findViewById(R.id.ll);   
  24.         ll.setOnTouchListener(this);   
  25.         ll.setLongClickable(true);   
  26.     }   
  27.     @Override   
  28.     public boolean onTouch(View v, MotionEvent event) {   
  29.         // TODO Auto-generated method stub   
  30.         Log.i("touch","touch");   
  31.          return mGestureDetector.onTouchEvent(event);    
  32.     }   
  33.     @Override   
  34.     public boolean onDown(MotionEvent e) {   
  35.         // TODO Auto-generated method stub   
  36.         return false;   
  37.     }   
  38.     @Override   
  39.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,   
  40.             float velocityY) {   
  41.         // TODO Auto-generated method stub   
  42.          if (e1.getX()-e2.getX() > FLING_MIN_DISTANCE    
  43.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  44.                 // Fling left    
  45.                 Toast.makeText(this, "向左手勢", Toast.LENGTH_SHORT).show();    
  46.             } else if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE    
  47.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  48.                 // Fling right    
  49.                 Toast.makeText(this, "向右手勢", Toast.LENGTH_SHORT).show();    
  50.             }    
  51.             return false;    
  52.     }   
  53.     @Override   
  54.     public void onLongPress(MotionEvent e) {   
  55.         // TODO Auto-generated method stub   
  56.     }   
  57.     @Override   
  58.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,   
  59.             float distanceY) {   
  60.         // TODO Auto-generated method stub   
  61.         return false;   
  62.     }   
  63.     @Override   
  64.     public void onShowPress(MotionEvent e) {   
  65.         // TODO Auto-generated method stub   
  66.     }   
  67.     @Override   
  68.     public boolean onSingleTapUp(MotionEvent e) {   
  69.         // TODO Auto-generated method stub   
  70.         return false;   
  71.     }   
  72. }