android中的手势探测

本文介绍了Android中使用GestureDetector和ScaleGestureDetector进行手势识别的方法,包括如何捕捉单击、长按、双击、滑动和缩放等手势事件,并通过实现特定的接口来响应这些事件。文章还提供了示例代码,演示了如何在实际应用中应用这些手势识别功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们常说的手势包括:单击、长按、双击、滑动、缩放, 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 Methods
abstract boolean onDown( MotionEvent e)
Notified when a tap occurs with the down  MotionEvent that triggered it.
abstract boolean onFling( 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 void onLongPress( MotionEvent e)
Notified when a long press occurs with the initial on down  MotionEvent that trigged it.
abstract boolean onScroll( 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 void onShowPress( MotionEvent e)
The user has performed a down  MotionEvent and not performed a move or up yet.
abstract boolean onSingleTapUp( MotionEvent e)
Notified when a tap occurs with the up  MotionEvent that triggered it.
GestureDetector.OnDoubleTapListener:双击事件判断
Public Methods
abstract boolean onDoubleTap( MotionEvent e)
Notified when a double-tap occurs.
abstract boolean onDoubleTapEvent( MotionEvent e)
Notified when an event within a double-tap gesture occurs, including the down, move, and up events.
abstract boolean onSingleTapConfirmed( MotionEvent e)
Notified when a single-tap occurs.

ScaleGestureDetector.OnScaleGestureListener:缩放事件判断
Public Methods
abstract boolean onScale( ScaleGestureDetector detector)
Responds to scaling events for a gesture in progress.
abstract boolean onScaleBegin( ScaleGestureDetector detector)
Responds to the beginning of a scaling gesture.
abstract void onScaleEnd( 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);  
        }  
}
*/

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值