有关touchEvent的事件里都有一个 MotionEvent 參数,以下来简介一下它的属性的一些含义和使用的方法
通常单指操作时,一般例如以下:
switch (event.getAction()) {//第一个触摸点 case MotionEvent.ACTION_DOWN: //按下 = 0 float x = event.getX(); break; case MotionEvent.ACTION_MOVE: //移动 = 2 break; case MotionEvent.ACTION_UP: // 抬起 = 1 break; }
多点触摸时:
下面属性在api level 5出现,api level 8过时。
MotionEvent.ACTION_POINTER_1_DOWN
MotionEvent.ACTION_POINTER_2_DOWN
MotionEvent.ACTION_POINTER_3_DOWN
MotionEvent.ACTION_POINTER_1_UP
MotionEvent.ACTION_POINTER_2_UP
MotionEvent.ACTION_POINTER_3_UP
api level 8 即2.2后起用:MotionEvent.ACTION_POINTER_DOWNMotionEvent.ACTION_POINTER_UP
通过下面測试发现。多点触摸时,
switch (event.getAction()) { // case MotionEvent.ACTION_DOWN: //按下 = 0 // System.out.println("onTouchEvent"); // break; // case MotionEvent.ACTION_MOVE://移动 = 2 // // break; // case MotionEvent.ACTION_UP:// 抬起 = 1 // // break; case MotionEvent.ACTION_POINTER_DOWN://非第一个触摸点按下 = 5 System.out.println("ACTION_POINTER_DOWN"); break; case MotionEvent.ACTION_POINTER_UP: //非第一个触摸点抬起 = 6 System.out.println("ACTION_POINTER_UP"); break; default: break; }MotionEvent.ACTION_POINTER_DOWN、MotionEvent.ACTION_POINTER_UP 未运行
当屏幕上已经有一个点被按住,此时再按下其它点时。才会触发MotionEvent.ACTION_POINTER_DOWN
第二个点以后取x、y坐标值
float x2 = event.getX(event.getActionIndex());//action index 从0開始到pointer count -1 结束 float y2 = event.getY(event.getActionIndex());//某个index的触摸点的 相对于当前view的y坐标 event.getPointerCount(); //触摸点的数量
多点时获取坐标 索引 id 演示样例
int pointerCount = event.getPointerCount(); // switch (event.getAction() & MotionEvent.ACTION_MASK) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: case MotionEvent.ACTION_MOVE: for (int i = 0; i < pointerCount; i++) { int id = event.getPointerId(i); //同一点的id值保持不变 int index = i;//同一点的索引值,是可变的。当由多指逐渐降低时。index-- dx = (int) event.getX(id); dy = (int) event.getY(id); } invalidate(); break; case MotionEvent.ACTION_CANCEL: default: break; }
演示样例
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: System.out.println("ACTION_DOWN"); mDownX = (int) event.getX(); mDownY = (int) event.getY(); if (flag) { return true; //对于onTouchEvent,要return true。才干运行兴许的 move。up } else { break; } case MotionEvent.ACTION_MOVE: System.out.println("move"); int moveX = (int) event.getX(); int moveY = (int) event.getY(); break; case MotionEvent.ACTION_UP: System.out.println("ACTION_UP"); break; case MotionEvent.ACTION_POINTER_DOWN: //当屏幕上已经有一个点被按住,此时再按下其它点时触发 System.out.println("ACTION_POINTER_DOWN"); break; case MotionEvent.ACTION_POINTER_UP: System.out.println("ACTION_POINTER_UP"); break; } return super.onTouchEvent(event); }
输出:
06-06 23:25:38.974 11930-11930/com.immoc.canvaspath I/System.out: ACTION_DOWN 06-06 23:25:38.974 11930-11930/com.immoc.canvaspath I/System.out: ACTION_POINTER_DOWN 06-06 23:25:39.202 11930-11930/com.immoc.canvaspath I/System.out: move 06-06 23:25:39.218 11930-11930/com.immoc.canvaspath I/System.out: move 06-06 23:25:39.234 11930-11930/com.immoc.canvaspath I/System.out: move 06-06 23:25:39.250 11930-11930/com.immoc.canvaspath I/System.out: move 06-06 23:25:39.334 11930-11930/com.immoc.canvaspath I/System.out: ACTION_POINTER_UP 06-06 23:25:39.334 11930-11930/com.immoc.canvaspath I/System.out: ACTION_UP