1、当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(View v, MotionEvent event)方法,我们可以处理一些touch事件,但是这个方法太过简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦(因为我们要自己根据用户触摸的轨迹去判断是什么手势)Android sdk给我们提供了GestureDetector(Gesture:手势Detector:识别)类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。虽然他能识别手势,但是不同的手势要怎么处理,应该是提供给程序员实现的,因此这个类对外提供了两个接口:OnGestureListener,OnDoubleTapListener,还有一个内部类SimpleOnGestureListener,SimpleOnGestureListener类是GestureDetector提供给我们的一个更方便的响应不同手势的类,这个类实现了上述两个接口(但是所有的方法体都是空的),该类是static class,也就是说它实际上是一个外部类。程序员可以在外部继承这个类,重写里面的手势处理方法。
通过GestureDetector的构造方法可以将SimpleOnGestureListener对象传递进去,这样GestureDetector能处理不同的手势了。
2.、具体用法:
- public class MySurfaceView extends SurfaceView implements OnGestureListener,
- OnTouchListener, Callback {
- GestureDetector gd;
- Context context;
-
- public MySurfaceView(Context context) {
- super(context);
- this.context = context;
- setFocusable(true);
- requestFocus();
- this.setLongClickable(true);
- this.setOnTouchListener(this);
- setFocusable(true);
- gd = new GestureDetector(new MySimpleGesture());
-
-
- gd.setIsLongpressEnabled(true);
-
- }
-
-
- @Override
- public boolean onDown(MotionEvent e) {
-
- System.out.println("onDown");
- return false;
- }
-
-
- @Override
- public void onShowPress(MotionEvent e) {
-
- System.out.println("onShowPress");
- }
-
-
- @Override
- public boolean onSingleTapUp(MotionEvent e) {
-
- System.out.println("onSingleTopUp");
- return false;
- }
-
-
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
- float distanceY) {
- System.out.println("onScroll");
-
- return false;
- }
-
-
- @Override
- public void onLongPress(MotionEvent e) {
-
- System.out.println("onLongPress");
- }
-
-
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
-
- System.out.println("onFling");
-
-
-
-
-
-
-
-
- final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;
- if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
- && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
-
- Log.i("MyGesture", "Fling left");
- Toast.makeText(context, "Fling Left", Toast.LENGTH_SHORT).show();
- } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
- && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
-
- Log.i("MyGesture", "Fling right");
- Toast.makeText(context, "Fling Right", Toast.LENGTH_SHORT).show();
- }
- return true;
- }
-
-
- @Override
- public boolean onTouch(View v, MotionEvent event) {
-
- System.out.println("onTouch");
- return gd.onTouchEvent(event);
- }
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
-
-
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
-
-
- }
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
-
-
- }
-
-
- private class MySimpleGesture extends SimpleOnGestureListener {
-
- public boolean onDoubleTap(MotionEvent e) {
- Log.i("MyGesture", "onDoubleTap");
- return super.onDoubleTap(e);
- }
-
-
- public boolean onDoubleTapEvent(MotionEvent e) {
- Log.i("MyGesture", "onDoubleTapEvent");
- return super.onDoubleTapEvent(e);
- }
-
-
- public boolean onDown(MotionEvent e) {
- Log.i("MyGesture", "onDown");
- return super.onDown(e);
- }
-
-
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- Log.i("MyGesture", "onFling");
- return super.onFling(e1, e2, velocityX, velocityY);
- }
-
-
- public void onLongPress(MotionEvent e) {
- Log.i("MyGesture", "onLongPress");
- super.onLongPress(e);
- }
-
-
- public boolean onScroll(MotionEvent e1, MotionEvent e2,
- float distanceX, float distanceY) {
- Log.i("MyGesture", "onScroll");
- return super.onScroll(e1, e2, distanceX, distanceY);
- }
-
-
- public void onShowPress(MotionEvent e) {
- Log.i("MyGesture", "onShowPress");
- super.onShowPress(e);
- }
-
-
- public boolean onSingleTapConfirmed(MotionEvent e) {
- Log.i("MyGesture", "onSingleTapConfirmed");
- return super.onSingleTapConfirmed(e);
- }
-
- public boolean onSingleTapUp(MotionEvent e) {
- Log.i("MyGesture", "onSingleTapUp");
- return super.onSingleTapUp(e);
- }
- }
- }
结果:
1、当单击屏幕的时候,输出:
2、当单击等一会再松开时候输出(
3、当用户按住不放的时候,输出:
4、当用户按住不放,拖动鼠标的时候输出:
5、当用户按住不放,快速拖动鼠标的时候:
6、当用户双击的时候(观察与单击的区别---onDubleTab在第二次按下Down的时候才出发,说明 是双击事件在onDown事件之前;):
本文转自莫水千流博客园博客,原文链接:http://www.cnblogs.com/zhoug2020/p/6593094.html,如需转载请自行联系原作者