概述:
一般情况下,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(View v, MotionEvent event)方法,我们可以处理一些touch事件,但是这个方法太过简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦(因为我们要自己根据用户触摸的轨迹去判断是什么手势)。
Android sdk给我们提供了GestureDetector(Gesture:手势Detector:识别)类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。虽然他能识别手势,但是不同的手势要怎么处理,应该是提供给程序员实现的。
GestureDetector这个类对外提供了两个接口:OnGestureListener,OnDoubleTapListener,还有一个内部类SimpleOnGestureListener。
GestureDetector.OnDoubleTapListener接口:用来通知DoubleTap事件,类似于鼠标的双击事件。
1,onDoubleTap(MotionEvent e):在双击的第二下,Touch down时触发 。
2,onDoubleTapEvent(MotionEvent e):通知DoubleTap手势中的事件,包含down、up和move事件(这里指的是在双击之间发生的事件,例如在同一个地方双击会产生DoubleTap手势,而在DoubleTap手势里面还会发生down和up事件,这两个事件由该函数通知);双击的第二下Touch down和up都会触发,可用e.getAction()区分。
3,onSingleTapConfirmed(MotionEvent e):用来判定该次点击是SingleTap而不是DoubleTap,如果连续点击两次就是DoubleTap手势,如果只点击一次,系统等待一段时间后没有收到第二次点击则判定该次点击为SingleTap而不是DoubleTap,然后触发SingleTapConfirmed事件。这个方法不同于onSingleTapUp,他是在GestureDetector确信用户在第一次触摸屏幕后,没有紧跟着第二次触摸屏幕,也就是不是“双击”的时候触发
GestureDetector.OnGestureListener接口:用来通知普通的手势事件,该接口有如下六个回调函数:
1. onDown(MotionEvent e):down事件;
2. onSingleTapUp(MotionEvent e):一次点击up事件;在touch down后又没有滑动
(onScroll),又没有长按(onLongPress),然后Touchup时触发。
点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
3. onShowPress(MotionEvent e):down事件发生而move或则up还没发生前触发该
事件;Touch了还没有滑动时触发(与onDown,onLongPress)比较onDown只要Touch down一定立刻触发。而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。所以Touchdown后一直不滑动
按照onDown->onShowPress->onLongPress这个顺序触发。
4. onLongPress(MotionEvent e):长按事件;Touch了不移动一直Touch down时触发;
5. onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY):滑动手
势事件;Touch了滑动一点距离后,在ACTION_UP时才会触发 ;
参数:e1 第1个ACTION_DOWN MotionEvent 并且只有一个;e2 最后一个ACTION_MOVE MotionEvent ;velocityX X轴上的移动速度,像素/秒 ;velocityY Y轴上的移动速度,像素/秒.触发条件:X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒
6. onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY):在屏幕上
拖动事件。无论是用手拖动view,或者是以抛的动作滚动,都会多次触发,这个方法在ACTION_MOVE动作发生时就会触发。
点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
Demo
自定义Button控件,实现手势的监听和处理:
public class MyButton extends Button { private GestureDetector mGesture; private OnDoubleClickListener onDoubleClickListener; //自定义监听器接口 interface OnDoubleClickListener{ void onDoubleClick(View view); } //设置双击事件监听器的方法 public void setOnDoubleClickListener(OnDoubleClickListener onDoubleClickListener){ this.onDoubleClickListener = onDoubleClickListener; }; public MyButton(Context context) { super(context); } public MyButton(final Context context, AttributeSet attrs) { super(context, attrs); // mGesture = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){ @Override public boolean onDoubleTap(MotionEvent e) { if(onDoubleClickListener!=null) { onDoubleClickListener.onDoubleClick(MyButton.this); } Toast.makeText(context,"双击事件",Toast.LENGTH_SHORT).show(); return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { /** * 滑动和拖拽最好不要一起实现,会产生矛盾 */ if(Math.abs(e1.getX()-e2.getX())>50){ setTranslationX(e2.getX() - e1.getX()); //根据手势滑动的距离而在水平方向上滑动控件 ObjectAnimator.ofFloat(MyButton.this,"translationX",getTranslationX(),e2.getX()-e1.getX()) .setDuration(500).start(); return true; } return super.onFling(e1, e2, velocityX, velocityY); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { //根据手势拖拽控件的相位而移动控件 setTranslationX(getTranslationX()+e2.getX() - e1.getX()); setTranslationY(getTranslationX()+e2.getY() - e1.getY()); return super.onScroll(e1, e2, distanceX, distanceY); } }); } public MyButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN){ } return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { //touch事件传给onTouchEvent() mGesture.onTouchEvent(event); return super.onTouchEvent(event); } }
布局:
<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=".MainActivity"> <com.example.administrator.selfishgroupview.MyButton android:id="@+id/button_doubleTap" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击"/> </RelativeLayout>
主活动:
public class MainActivity extends Activity { private MyButton myButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myButton = (MyButton) findViewById(R.id.button_doubleTap); myButton.setOnDoubleClickListener(new MyButton.OnDoubleClickListener() { @Override public void onDoubleClick(View view) { Log.d("","点击两次"); } }); } }
结果演示:
双击:
滑动:
拖拽:
作者:逆转星河