qrobot开发总结之android手势识别

来源:互联网 发布:gm300写频软件 编辑:程序博客网 时间:2024/04/30 08:49

1、实现两个接口

implements OnTouchListener, OnGestureListener

onTouchListener接口只有一个方法

public abstract boolean onTouch (View v, MotionEvent event)

onGestureListener接口有以下方法

public abstract boolean onDown (MotionEvent e)
public abstract boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
public abstract void onLongPress (MotionEvent e)
public abstract boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
public abstract void onShowPress (MotionEvent e)
public abstract boolean onSingleTapUp (MotionEvent e)

处理手势识别只需要复写onFiing方法,其中由e1.getX()和e1.getY()可以得到按下屏幕时的坐标,

同样可以由e2得到离开屏幕时的坐标,比较两个坐标的相对位置,可以判断手指划过屏幕的方向。

qrobot开发过程中只用到从左向右或从右向左的手势识别。

2、GestureDetector类

初始化:

private GestureDetector gestureDetector = null;

gestureDetector = new GestureDetector(this);gestureDetector.setIsLongpressEnabled(true);

注意第2句


在onTouch方法中

public boolean onTouch(View v, MotionEvent event) {return gestureDetector.onTouchEvent(event);}

3、给控件添加监听器

在qrobot开发中监听器是加在一个空白的textview上的

 textView.setFocusable(true); textView.setClickable(true); textView.setLongClickable(true); textView.setOnTouchListener(this);

大功告成,快试试吧!