用户手势检测-GestureDetector使用详解
来源:互联网 发布:javascript 正则验证 编辑:程序博客网 时间:2024/05/30 22:50
一.概述
当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等。
一般情况下,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(View v, MotionEvent event)方法,我们可以处理一些touch事件,但是这个方法太过简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦(因为我们要自己根据用户触摸的轨迹去判断是什么手势)。
Android sdk给我们提供了GestureDetector(Gesture:手势Detector:识别)类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。虽然他能识别手势,但是不同的手势要怎么处理,应该是提供给程序员实现的。
GestureDetector提供了一个内部类SimpleOnGestureListener,可以很方便的实现手势检测,我们可以继承这个类,重写里面的方法。
二.实现
要使用GestureDetector,需要三步:
(1)创建OnGestureListener监听函数:
可以构造实例:
GestureDetector.SimpleOnGestureListener listener = new SimpleOnGestureListener(){}
也可以构造类:
class GestureListener extends SimpleOnGestureListener{}
(2)使用构造函数:
GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);
(3)拦截触摸事件:
public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); }
或者
@Override public boolean onTouchEvent(MotionEvent event) { gestureDetector.onTouchEvent(event); return super.onTouchEvent(event); }
三.例子
下面上给大家举一个应用的例子,使用onFling方法判断当前是向右还是向左划动,其他方法如果你有兴趣的话自己可以去研究一下,这里就不讲了。
先上布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.hecun.myapplication.MainActivity"> <TextView android:id="@+id/textview" android:textColor="#ffffff" android:textSize="25sp" android:background="#9e9e9e" android:gravity="center" android:text="请滑动" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="40dp" /></RelativeLayout>
public class MainActivity extends Activity /*implements OnTouchListener */{ private TextView textView; private GestureDetector gestureDetector; //滑动最小距离 public static final int FLING_MIN_DISTANCE = 50; //滑动最小速度 public static final int FLING_MIN_VELOCITY = 50; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textview); gestureDetector = new GestureDetector(this, new GestureListener ()); //必须设置,否则无法响应触摸,因为textview默认是不可点击的// textView.setClickable(true);// textView.setOnTouchListener(this); } class GestureListener extends SimpleOnGestureListener { /** * 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, * 多个ACTION_MOVE, 1个ACTION_UP触发 * 参数解释: e1:第1个ACTION_DOWN MotionEvent e2:最后一个ACTION_MOVE MotionEvent velocityX:X轴上的移动速度,像素/秒 velocityY:Y轴上的移动速度,像素/秒 */ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getRawX() - e2.getRawX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { textView.setText("你向左滑动"); } else if (e2.getRawX() - e1.getRawX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { textView.setText("你向右滑动"); } return super.onFling(e1, e2, velocityX, velocityY); } } /* @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); }*/ /** * 拦截触摸事件 */ @Override public boolean onTouchEvent(MotionEvent event) { gestureDetector.onTouchEvent(event); return super.onTouchEvent(event); }}
0 0
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 5.1 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- 用户手势检测-GestureDetector使用详解
- Linux学习之路——目录解释
- android 判断网络状态 (无网络 wifi 数据流量)
- 杂谈经验与未来
- 设计模式读书笔记-----简单工厂模式
- iOS如何在应用跳转至系统设置各个界面
- 用户手势检测-GestureDetector使用详解
- Java 7之基础 - 强引用、弱引用、软引用、虚引用
- C#操作服务
- LINUX netlink机制
- Linux曲径通幽:常用命令(Shell应用技巧)
- C. Pearls in a Row
- Qt之串口类QSerialPort使用笔记
- linuxshell中"2>&1"含义
- 拓扑规划辅助软件系统的设计与实现