android可以识别用户的手势(即用户用手指滑动的方向),通过用户不同的手势,从而做出不同的处理

需要使用OnGestureListener

比如说看电子书的时候翻页,或者要滑动一些其他内容

直接上代码

界面文件

main.xml

 

view plain copy to clipboard print ?
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. "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.        
  9.     android:layout_width="fill_parent"    
  10.     android:layout_height="wrap_content"    
  11.     android:text="@string/hello"  
  12.     />   
  13.   

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

 

主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.     /** Called when the activity is first created. */  
  19.     @Override   
  20.     public void onCreate(Bundle savedInstanceState) {   
  21.         super.onCreate(savedInstanceState);   
  22.         setContentView(R.layout.main);   
  23.         mGestureDetector = new GestureDetector(this);   
  24.         LinearLayout ll=(LinearLayout)findViewById(R.id.ll);   
  25.         ll.setOnTouchListener(this);   
  26.         ll.setLongClickable(true);   
  27.     }   
  28.     @Override   
  29.     public boolean onTouch(View v, MotionEvent event) {   
  30.         // TODO Auto-generated method stub   
  31.         Log.i("touch","touch");   
  32.          return mGestureDetector.onTouchEvent(event);    
  33.     }   
  34.     @Override   
  35.     public boolean onDown(MotionEvent e) {   
  36.         // TODO Auto-generated method stub   
  37.         return false;   
  38.     }   
  39.     @Override   
  40.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,   
  41.             float velocityY) {   
  42.         // TODO Auto-generated method stub   
  43.          if (e1.getX()-e2.getX() > FLING_MIN_DISTANCE    
  44.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  45.                 // Fling left    
  46.                 Toast.makeText(this"向左手势", Toast.LENGTH_SHORT).show();    
  47.             } else if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE    
  48.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  49.                 // Fling right    
  50.                 Toast.makeText(this"向右手势", Toast.LENGTH_SHORT).show();    
  51.             }    
  52.             return false;    
  53.     }   
  54.     @Override   
  55.     public void onLongPress(MotionEvent e) {   
  56.         // TODO Auto-generated method stub   
  57.     }   
  58.     @Override   
  59.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,   
  60.             float distanceY) {   
  61.         // TODO Auto-generated method stub   
  62.         return false;   
  63.     }   
  64.     @Override   
  65.     public void onShowPress(MotionEvent e) {   
  66.         // TODO Auto-generated method stub   
  67.     }   
  68.     @Override   
  69.     public boolean onSingleTapUp(MotionEvent e) {   
  70.         // TODO Auto-generated method stub   
  71.         return false;   
  72.     }   
  73. }  

更多相关文章

  1. android用户界面之按钮(Button)教程实例汇
  2. Android之应用程序基础
  3. android用户界面-组件Widget-地图视图MapView
  4. android用户界面-组件Widget-画廊视图Gallery
  5. SlidingMenu和ActionBarSherlock结合做出出色的App布局,Facebook
  6. Android(安卓)View的介绍和使用
  7. Android(安卓)中文 API (27) —— SeekBar.OnSeekBarChangeListene
  8. android Manifest.xml选项-android:ConfigChanges
  9. Android解决父控件拦截子控件手势滑动事件的问题

随机推荐

  1. 阿里云服务器手动实现mysql双机热备的两
  2. 详解MySQL8.0+常用命令
  3. MySQL InnoDB如何保证事务特性示例详解
  4. mysql实现多表关联统计(子查询统计)示例
  5. mysql多版本并发控制MVCC的实现
  6. mysql group_concat 实现把分组字段写成
  7. MySQL借助DB实现分布式锁思路详解
  8. django2.2版本连接mysql数据库的方法
  9. WINDOWS下安装MYSQL教程详解
  10. Win中安装mysql的详细步骤