- 触摸事件监听
注意:
1)
对于多点触控,我们需要由MotionEvent.getAction()&MotionEvent.ACTION_MASK得到:ACTION_POINTER_DOWN、ACTION_POINTER_UP;
2)
第二个手指按下时,将触发MotionEvent.ACTION_POINTER_DOWN事件
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction()& MotionEvent.ACTION_MASK;
switch (action) {
case MotionEvent.ACTION_DOWN:
//单点和多点两者共用的
break;
case MotionEvent.ACTION_POINTER_DOWN:
//第二个手指按下时,触发
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
break;
}
}
- 1
event.getPointerCount() 获取当前触控点的数量
event.getPointerId(i) 获取指定触控点的Id,用于区分不同的触控点
event.getX(i) 获取指定触控点的X坐标
event.getY(i) 获取指定触控点的Y坐标
event.getPressure(i) 获取指定触控点的压力值,此值的获取需要LCD硬件和驱动的支持
2. 常用手势处理
//两点的距离
private float distance(MotionEvent event) {
float x = event.getX(0)-event.getY(1);
float y = event.getY(0)-event.getY(1);
return (float)Math.sqrt(x*x+y*y);
}
- 1
/**
* 取旋转角度
*
* @param event
* @return
*/
private float rotation(MotionEvent event) {
double delta_x = (event.getX(0) - event.getX(1));
double delta_y = (event.getY(0) - event.getY(1));
double radians = Math.atan2(delta_y, delta_x);
return (float) Math.toDegrees(radians);
}
- 1
/**
* 按下两点的中心点
*
* @param point
* @param event
*/
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
- 1