package com.example.testanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
public class MainActivity extends Activity implements
GestureDetector.OnGestureListener {
GestureDetector detector;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 构造GestureDetector;Context context, OnGestureListener listener
detector = new GestureDetector(this, this);
detector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
// 短快的点击算一次单击
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
System.out.println("onSingleTapConfirmed");
return false;
}
// 双击时产生一次
@Override
public boolean onDoubleTap(MotionEvent e) {
System.out.println("onDoubleTap");
return false;
}
// 双击时产生两次
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
System.out.println("onDoubleTapEvent");
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return detector.onTouchEvent(event);
}
// 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发
@Override
public boolean onDown(MotionEvent e) {
System.out.println("onDown");
return false;
}
// 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发
// 注意和onDown()的区别,强调的是没有松开或者拖动的状态
@Override
public void onShowPress(MotionEvent e) {
}
// 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发
@Override
public boolean onSingleTapUp(MotionEvent e) {
System.out.println("onSingleTapUp");
return false;
}
// 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
System.out.println("onScroll");
return false;
}
// 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发
@Override
public void onLongPress(MotionEvent e) {
System.out.println("onLongPress" + e.getEventTime());
}
// 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
System.out.println("onFling: " + (e1.getX() - e2.getX()) + " "
+ (e1.getY() - e2.getY()) + " " + velocityX + " " + velocityY);
System.out.println(e1.getY());
return false;
}
}
利用触摸屏的Fling、Scroll等Gesture(手势)操作来操作会使得应用程序的用户体验大大提升,比如用Scroll手势在 浏览器中滚屏,用Fling在阅读器中翻页等。在Android系统中,手势的识别是通过 GestureDetector.OnGestureListener接口来实现的,举例说明:
对于触摸屏,其原生的消息无非按下、抬起、移动这几种,我们只需要简单重载onTouch或者设置触摸侦听器setOnTouchListener即可进行处理。不过,为了提高我们的APP的用户体验,有时候我们需要识别用户的手势,Android给我们提供的手势识别工具GestureDetector就可以帮上大忙了。
基础
GestureDetector的工作原理是,当我们接收到用户触摸消息时,将这个消息交给GestureDetector去加工,我们通过设置侦听器获得GestureDetector处理后的手势。
GestureDetector提供了两个侦听器接口,OnGestureListener处理单击类消息,OnDoubleTapListener处理双击类消息。
OnGestureListener的接口有这几个:
// 单击,触摸屏按下时立刻触发
1 | abstract boolean onDown(MotionEvent e); |
// 抬起,手指离开触摸屏时触发(长按、滚动、滑动时,不会触发这个手势)
1 | abstract boolean onSingleTapUp(MotionEvent e); |
// 短按,触摸屏按下后片刻后抬起,会触发这个手势,如果迅速抬起则不会
1 | abstract void onShowPress(MotionEvent e); |
// 长按,触摸屏按下后既不抬起也不移动,过一段时间后触发
1 | abstract void onLongPress(MotionEvent e); |
// 滚动,触摸屏按下后移动
1 | abstract boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY); |
// 滑动,触摸屏按下后快速移动并抬起,会先触发滚动手势,跟着触发一个滑动手势
1 | abstract boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY); |
OnDoubleTapListener的接口有这几个:
// 双击,手指在触摸屏上迅速点击第二下时触发
1 | abstract boolean onDoubleTap(MotionEvent e); |
// 双击的按下跟抬起各触发一次
1 | abstract boolean onDoubleTapEvent(MotionEvent e); |
// 单击确认,即很快的按下并抬起,但并不连续点击第二下
1 | abstract boolean onSingleTapConfirmed(MotionEvent e); |
有时候我们并不需要处理上面所有手势,方便起见,Android提供了另外一个类SimpleOnGestureListener实现了如上接口,我们只需要继承SimpleOnGestureListener然后重载感兴趣的手势即可。
应用
STEP 1: 创建手势侦听对象
001 | package noodies.blog.csdn.net; |
003 | import android.content.Context; |
005 | import android.view.MotionEvent; |
007 | import android.view.GestureDetector.SimpleOnGestureListener; |
009 | import android.widget.Toast; |
011 | public class MyGestureListener extends SimpleOnGestureListener { |
013 | private Context mContext; |
015 | MyGestureListener(Context context) { |
025 | public boolean onDown(MotionEvent e) { |
027 | Toast.makeText(mContext, "DOWN " + e.getAction(), Toast.LENGTH_SHORT).show(); |
037 | public void onShowPress(MotionEvent e) { |
039 | Toast.makeText(mContext, "SHOW " + e.getAction(), Toast.LENGTH_SHORT).show(); |
047 | public boolean onSingleTapUp(MotionEvent e) { |
049 | Toast.makeText(mContext, "SINGLE UP " + e.getAction(), Toast.LENGTH_SHORT).show(); |
059 | public boolean onScroll(MotionEvent e1, MotionEvent e2, |
061 | float distanceX, float distanceY) { |
063 | Toast.makeText(mContext, "SCROLL " + e2.getAction(), Toast.LENGTH_SHORT).show(); |
073 | public void onLongPress(MotionEvent e) { |
075 | Toast.makeText(mContext, "LONG " + e.getAction(), Toast.LENGTH_SHORT).show(); |
083 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, |
087 | Toast.makeText(mContext, "FLING " + e2.getAction(), Toast.LENGTH_SHORT).show(); |
097 | public boolean onDoubleTap(MotionEvent e) { |
099 | Toast.makeText(mContext, "DOUBLE " + e.getAction(), Toast.LENGTH_SHORT).show(); |
109 | public boolean onDoubleTapEvent(MotionEvent e) { |
111 | Toast.makeText(mContext, "DOUBLE EVENT " + e.getAction(), Toast.LENGTH_SHORT).show(); |
121 | public boolean onSingleTapConfirmed(MotionEvent e) { |
123 | Toast.makeText(mContext, "SINGLE CONF " + e.getAction(), Toast.LENGTH_SHORT).show(); |
STEP 2: 设置手势识别
我们可以在Activity里设置手势识别:
01 | package noodies.blog.csdn.net; |
03 | import android.app.Activity; |
05 | import android.os.Bundle; |
07 | import android.view.GestureDetector; |
09 | import android.view.MotionEvent; |
11 | public class GestureTestActivity extends Activity { |
13 | private GestureDetector mGestureDetector; |
17 | public void onCreate(Bundle savedInstanceState) { |
19 | super .onCreate(savedInstanceState); |
21 | setContentView(R.layout.main); |
23 | mGestureDetector = new GestureDetector( this , new MyGestureListener( this )); |
31 | public boolean onTouchEvent(MotionEvent event) { |
33 | return mGestureDetector.onTouchEvent(event); |
也可以在自定义的View里面设置手势识别:
01 | package noodies.blog.csdn.net; |
03 | import android.content.Context; |
04 | import android.util.AttributeSet; |
05 | import android.view.GestureDetector; |
06 | import android.view.MotionEvent; |
07 | import android.view.View; |
10 | public class MyView extends View { |
12 | private GestureDetector mGestureDetector; |
14 | public MyView(Context context, AttributeSet attrs) { |
15 | super (context, attrs); |
16 | mGestureDetector = new GestureDetector(context, new MyGestureListener(context)); |
17 | setLongClickable( true ); |
19 | this .setOnTouchListener( new OnTouchListener() { |
20 | public boolean onTouch(View v, MotionEvent event) { |
21 | return mGestureDetector.onTouchEvent(event); |
陷阱
对于自定义View,使用手势识别有两处陷阱可能会浪费你的不少时间。
1:View必须设置longClickable为true,否则手势识别无法正确工作,只会返回Down, Show, Long三种手势
2:必须在View的onTouchListener中调用手势识别,而不能像Activity一样重载onTouchEvent,否则同样手势识别无法正确工作
测试结果
下面是各种操作返回的手势序列,数值0表示触摸屏按下,1表示抬起
单击:down 0, single up 1, single conf 0
短按:down 0, show 0, single up 1
长按:down 0, show 0, long 0
双击:down 0, single up 1, double 0, double event 0, down 0, double event 1
滚动:down 0, (show 0), scrool 2...
滑动:down 0, (show 0), scrool 2..., fling 1