GestureDetector是对Touch事件的封装化处理,辅助我们队事件的行为加以区别并监听
onScroll:在屏幕上划出一段距离会触发
onFling:猛的一划会触发
要想让手势类监听到touch事件 下面例子是在touch中写的
对于activity,可以复写dispatchTouchEvent方法中调用 mGestureDetector.onTouchEvent(event);
总之需要调用 mGestureDetector.onTouchEvent(event);才能实现手势类的监听效果
代码例子:
package com.test.jd.myapplication; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.Button; public class MainActivity extends Activity { private Button mButton; private GestureDetector mGestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGestureDetector = new GestureDetector(this, new MyOnGestureListener()); mButton = (Button) findViewById(R.id.btn_textgesture); mButton.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.i(getClass().getName(), "onTouch-----" + getActionName(event.getAction())); mGestureDetector.onTouchEvent(event); // 一定要返回true,不然获取不到完整的事件 return true; } }); } private String getActionName(int action) { String name = ""; switch (action) { case MotionEvent.ACTION_DOWN: { name = "ACTION_DOWN"; break; } case MotionEvent.ACTION_MOVE: { name = "ACTION_MOVE"; break; } case MotionEvent.ACTION_UP: { name = "ACTION_UP"; break; } default: break; } return name; } class MyOnGestureListener extends SimpleOnGestureListener { @Override public boolean onSingleTapUp(MotionEvent e) { Log.i(getClass().getName(), "onSingleTapUp-----" + getActionName(e.getAction())); return false; } @Override public void onLongPress(MotionEvent e) { Log.i(getClass().getName(), "onLongPress-----" + getActionName(e.getAction())); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.i(getClass().getName(), "onScroll-----" + getActionName(e2.getAction()) + ",(" + e1.getX() + "," + e1.getY() + ") ,(" + e2.getX() + "," + e2.getY() + ")"); return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.i(getClass().getName(), "onFling-----" + getActionName(e2.getAction()) + ",(" + e1.getX() + "," + e1.getY() + ") ,(" + e2.getX() + "," + e2.getY() + ")"); return false; } @Override public void onShowPress(MotionEvent e) { Log.i(getClass().getName(), "onShowPress-----" + getActionName(e.getAction())); } @Override public boolean onDown(MotionEvent e) { Log.i(getClass().getName(), "onDown-----" + getActionName(e.getAction())); return false; } @Override public boolean onDoubleTap(MotionEvent e) { Log.i(getClass().getName(), "onDoubleTap-----" + getActionName(e.getAction())); return false; } @Override public boolean onDoubleTapEvent(MotionEvent e) { Log.i(getClass().getName(), "onDoubleTapEvent-----" + getActionName(e.getAction())); return false; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { Log.i(getClass().getName(), "onSingleTapConfirmed-----" + getActionName(e.getAction())); return false; } } }
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btn_textgesture" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/app_name" /> </RelativeLayout>
下载利用手势类关闭当前Activity的例子:http://pan.baidu.com/s/1mgMfdVy