android Gesture(detector)
转自http://wayfarer.iteye.com/blog/460284
看到LZ把注释写得那么详细,于是把它转过来了,如下所示:
Android Touch Screen 与传统Click Touch Screen不同,会有一些手势(Gesture),例如Fling,Scroll等等。这些Gesture会使用户体验大大提升。Android中的Gesture识别(detector)是通过GestureDetector.OnGestureListener接口实现的。
首先,Android事件处理机制是基于Listener实现的,比如触摸屏相关的事件,就是通过onTouchListener实现;
其次,所有View的子类都可以通过setOnTouchListener()、setOnKeyListener()等方法来添加对某一类事件的Listener;
第三,Listener一般会以Interface的方式来提供,其中包含一个或多个abstract方法,我们需要实现这些方法来完成onTouch()、onKey()等操作。这样,程序便可以在特定的事件被dispatch到该view的时候,通过callback函数给予适当的响应。
1. Touch Screen Click举例
- public class MyGesture extends Activity implements OnTouchListener {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextView tv = (TextView) findViewById(R.id.tv);
- tv.setOnTouchListener(this);
- }
- public boolean onTouch(View v, MotionEvent event) {
- Toast.makeText(this, "Touch Touch", Toast.LENGTH_SHORT).show();
- return false;
- }
- }
我们可以通过MotionEvent的getAction()方法来获取Touch事件的类型,包括 ACTION_DOWN(按下触摸屏),ACTION_MOVE(按下触摸屏后移动受力点), ACTION_UP(松开触摸屏)和ACTION_CANCEL(不会由用户直接触发)。借助对于用户不同操作的判断,结合getRawX()、getRawY()、getX()和getY()等方法来获取坐标后,我们可以实现诸如拖动某一个按钮,拖动滚动条等功能。
2. 回到今天的重点,当我们捕捉到Touch操作的时候,如何识别出用户的Gesture?这里我们需要GestureDetector.OnGestureListener接口的帮助,代码如下:
- public class MyGesture extends Activity implements OnTouchListener, OnGestureListener {
- private GestureDetector mGestureDetector;
- public MyGesture() {
- mGestureDetector = new GestureDetector(this);
- }
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextView tv = (TextView) findViewById(R.id.tv);
- tv.setOnTouchListener(this);
- tv.setFocusable(true);
- tv.setClickable(true);
- tv.setLongClickable(true);
- mGestureDetector.setIsLongpressEnabled(true);
- }
-
-
-
-
-
- public boolean onTouch(View v, MotionEvent event) {
- return mGestureDetector.onTouchEvent(event);
- }
-
-
- public boolean onDown(MotionEvent arg0) {
- Log.i("MyGesture", "onDown");
- Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
- return true;
- }
-
-
-
-
-
- public void onShowPress(MotionEvent e) {
- Log.i("MyGesture", "onShowPress");
- Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
- }
-
-
- public boolean onSingleTapUp(MotionEvent e) {
- Log.i("MyGesture", "onSingleTapUp");
- Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
- return true;
- }
-
-
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
- Log.i("MyGesture", "onFling");
- Toast.makeText(this, "onFling", Toast.LENGTH_LONG).show();
- return true;
- }
-
-
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
- Log.i("MyGesture", "onScroll");
- Toast.makeText(this, "onScroll", Toast.LENGTH_LONG).show();
- return true;
- }
-
-
- public void onLongPress(MotionEvent e) {
- Log.i("MyGesture", "onLongPress");
- Toast.makeText(this, "onLongPress", Toast.LENGTH_LONG).show();
- }
- }
3. Fling事件的处理代码:除了第一个触发Fling的ACTION_DOWN和最后一个ACTION_MOVE中包含的坐标等信息外,我们还可以根据用户在X轴或者Y轴上的移动速度作为条件。比如下面的代码中我们就在用户移动超过100个像素,且X轴上每秒的移动速度大于200像素时才进行处理。
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
-
-
-
-
-
-
-
-
-
- final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;
- if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
-
- Log.i("MyGesture", "Fling left");
- Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();
- } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
-
- Log.i("MyGesture", "Fling right");
- Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();
- }
- return false;
- }
这个例子中,tv.setLongClickable(
true
)是必须的,因为
只有这样,view才能够处理不同于Tap(轻触)的hold(即ACTION_MOVE,或者多个ACTION_DOWN),我们同样可以通过layout定义中的android:longClickable来做到这一点。
4. SimpleOnGestureListener
- public class MyGesture extends Activity implements OnTouchListener {
- private GestureDetector mGestureDetector;
- public MyGesture() {
- mGestureDetector = new GestureDetector(new MySimpleGesture());
- }
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextView tv = (TextView) findViewById(R.id.tv);
- tv.setOnTouchListener(this);
- tv.setFocusable(true);
- tv.setClickable(true);
- tv.setLongClickable(true);
- }
- public boolean onTouch(View v, MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_UP) {
- Log.i("MyGesture", "MotionEvent.ACTION_UP");
- }
- return mGestureDetector.onTouchEvent(event);
- }
-
-
- private class MySimpleGesture extends SimpleOnGestureListener {
-
- public boolean onDoubleTap(MotionEvent e) {
- Log.i("MyGesture", "onDoubleTap");
- return super.onDoubleTap(e);
- }
-
-
- public boolean onDoubleTapEvent(MotionEvent e) {
- Log.i("MyGesture", "onDoubleTapEvent");
- return super.onDoubleTapEvent(e);
- }
-
-
- public boolean onDown(MotionEvent e) {
- Log.i("MyGesture", "onDown");
- return super.onDown(e);
- }
-
-
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
- Log.i("MyGesture", "onFling");
- return super.onFling(e1, e2, velocityX, velocityY);
- }
-
-
- public void onLongPress(MotionEvent e) {
- Log.i("MyGesture", "onLongPress");
- super.onLongPress(e);
- }
-
-
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
- Log.i("MyGesture", "onScroll");
- return super.onScroll(e1, e2, distanceX, distanceY);
- }
-
-
-
-
-
-
-
- public void onShowPress(MotionEvent e) {
- Log.i("MyGesture", "onShowPress");
- super.onShowPress(e);
- }
-
-
-
-
-
-
- public boolean onSingleTapConfirmed(MotionEvent e) {
- Log.i("MyGesture", "onSingleTapConfirmed");
- return super.onSingleTapConfirmed(e);
- }
- public boolean onSingleTapUp(MotionEvent e) {
- Log.i("MyGesture", "onSingleTapUp");
- return super.onSingleTapUp(e);
- }
- }
- }