android中的手势探测

来源:互联网 发布:汪正扬看的编程书 编辑:程序博客网 时间:2024/03/29 09:46

我们常说的手势包括:单击、长按、双击、滑动、缩放, android提供了几个手势探测类:GestureDetector(点击类探测),ScaleGestureDetector(缩放探测),

如何用这几种探测类来捕捉用户的手势呢?

  1. 首先构建一个GestureDetector对象,创建GestureDetector对象时会传入一个回调对象(实现GestureDetector.OnGestureListener)
  2. view有一个虚拟函数onTouchEvent(MotionEvent), 只要重载这个函数,并将MotionEvent对象丢给GestureDetector对象就可以了
  3. GestureDetector类中会自己判断MotionEvent的事件类型,并回调
  4. 问题来了,怎么回调业务的接口?
  5. 回到1中,传入的回调对象其实是我们需要实现的,并继承GestureDetector中的接口来实现手势的回调
  6. 如果是缩放,将GestureDetector换成ScaleGestureDetector对象即可
来看一下5中,继承GestureDetector/ScaleGestureDetecto的接口指的哪些?
GestureDetector.OnGestureListener:单击相关事件判断
Public Methodsabstract booleanonDown(MotionEvent e)
Notified when a tap occurs with the down MotionEvent that triggered it.
abstract booleanonFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent.
abstract voidonLongPress(MotionEvent e)
Notified when a long press occurs with the initial on down MotionEvent that trigged it.
abstract booleanonScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent.
abstract voidonShowPress(MotionEvent e)
The user has performed a down MotionEvent and not performed a move or up yet.
abstract booleanonSingleTapUp(MotionEvent e)
Notified when a tap occurs with the up MotionEvent that triggered it.
GestureDetector.OnDoubleTapListener:双击事件判断
Public Methodsabstract booleanonDoubleTap(MotionEvent e)
Notified when a double-tap occurs.
abstract booleanonDoubleTapEvent(MotionEvent e)
Notified when an event within a double-tap gesture occurs, including the down, move, and up events.
abstract booleanonSingleTapConfirmed(MotionEvent e)
Notified when a single-tap occurs.

ScaleGestureDetector.OnScaleGestureListener:缩放事件判断
Public Methodsabstract booleanonScale(ScaleGestureDetector detector)
Responds to scaling events for a gesture in progress.
abstract booleanonScaleBegin(ScaleGestureDetector detector)
Responds to the beginning of a scaling gesture.
abstract voidonScaleEnd(ScaleGestureDetector detector)
Responds to the end of a scale gesture.

注意这些都是接口, 如果实现这些接口则所有函数都要实现,比较麻烦。幸运的是android系统已经为你考虑好了,你只需要继承这几个类,重载你需要实现的函数就可以了,如下:
GestureDetector.SimpleOnGestureListener(实现了GestureDetector.OnGestureListener&GestureDetector.OnDoubleTapListener两个接口
ScaleGestureDetector.SimpleOnScaleGestureListener(实现了ScaleGestureDetector.OnScaleGestureListener
分析一下android源码中SimpleOnGestureListener和SimpleOnScaleGestureListener的实现就可以知道,它们其实只是实现的空函数,只是为了方便你继承的。

回过头来,6中的具体做法可以描述为:继承GestureDetector.SimpleOnGestureListener、ScaleGestureDetector.SimpleOnScaleGestureListener中的你关注的函数就可以了。

继续分析android的源码:最重要的关于手势的分析是函数:GestureDetector::onTouchEvent/ScaleGestureDetector::onTouchEvent, 它具体了分析手势然后回调相关的接口,理论上你完全可以自己写一套类似的机制来实现手势的分析。


示例代码:


import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;


public class MyView extends View {

GestureDetector mGesture = null; 


public MyView(Context context)
{
super(context);

mGesture = new GestureDetector(this.getContext(), new MyGestureDetector());


                Log.d("MyGestureDetector", "MyView()");  
}


protected void onDraw(Canvas canvas)
{
android.util.Log.d("MyGestureDetector", "onDraw");
}

@Override
public boolean onTouchEvent(MotionEvent event) {
        Log.d("MyGestureDetector", "onTouch");  


   // TODO Auto-generated method stub  
   return mGesture.onTouchEvent(event);  
}

class MyGestureDetector implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener
{


@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}


@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}


@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
}


@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}


@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}


@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}


@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}


@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

}


/*或者直接这种继承也为常见
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener
{
        @Override  
        public void onLongPress(MotionEvent e)  
        {  
            Log.d("MyGestureDetector", "onLongPress");  
            super.onLongPress(e);  
        }  
}
*/

}