android 多点触控

业务需求,做手机游戏手柄,研究多点触控。直播如下:

从touchEvent开始,在onTouchEvent里面return true,打印getAction()值。

1点触摸(点击事件):

0 (action_down)

2 (action_move) 

……

2

1 (action_up)

总结:单点流程简单,事件分明。

 

2点触摸

(先down先up,第1个手指先起来):

0

2

261(action_pointer_2_down)

2

………..

2

6(action_pointer_up)

1

 

(先down后up,第2个手指先起来):

0

2

261(action_pointer_2_down)

2

………..

262(action_pointer_2_up)

2

1

总结:2点触摸流程出现分歧,第一个down只发action_ down(0),不发action_pointer_1_down(5),最后一个up不发action_pointer_X_up只发action_up(1)。那么可以预期其他多点触控了。

 

3点触控

(1down->2down->3down->3up->2up->13up)

(先down后up,第2个手指先起来):

0

2

261(action_pointer_2_down)

2

………..

517(action_pointer_3_down)

518(action_pointer_3_up)

2

262(action_pointer_2_up)

2

1

 

(ok,乱点几个)

0

2

261(action_pointer_2_down)

2

………..

517(action_pointer_3_down)

6

2

262(action_pointer_2_up)

2

1

猜点击顺序是(1down->2down->3down->1up->2up->3up)

 

 

0

2

261(action_pointer_2_down)

2

………..

517(action_pointer_3_down)

6

2

6

2

1

猜点击顺序是(1down->2down->3down->1up->2up->3up),结果略有出入,第一个action过时后,第二个变成第一个,这个是list操作。猜想(1down->2down->3down->2up->3up->1up)打印结果是:

0

2

261(action_pointer_2_down)

………..

517(action_pointer_3_down)

262

2

262

2

1

验证正确,那么到这里,action的表现基本确定,可以根据这些表现调节多点触控的响应。基本需要支持3点及以上触控的应用较少,逻辑也不是那么简单,响应逻辑也不是那么简单。

 

 

 

进阶:

  

//掩码action,无触控点的索引信息

public boolean onTouchEvent(MotionEvent event) {

 

         switch (event.getActionMasked()) {

         case MotionEvent.ACTION_DOWN:

             break;

         case MotionEvent.ACTION_POINTER_UP:

             break;

         /**

          * API原文是 A non-primary pointer has gone down.

          * 翻译过来就是:非第一个点按下

        */

         case MotionEvent.ACTION_POINTER_DOWN:

             oldDist = spacing(event);

             midPoint(midPoint, event);

             isZoom = true;

             break;

         case MotionEvent.ACTION_MOVE:           

              break;

         }

 

         return true;

     }

 

 

      //统一单点和多点

  @Override

  public boolean onTouch(View v, MotionEvent event) {

    int pointerCount = event.getPointerCount();

    int pointerId = 0;

    int action = event.getActionMasked() % 5;//统一单点和多点

    switch(action){

      case MotionEvent.ACTION_DOWN:

        if(pointerCount>1){

          pointerId = (event.getAction()&MotionEvent.ACTION_POINTER_ID_MASK)>>> MotionEvent.ACTION_POINTER_ID_SHIFT;

        }

        break;

      case MotionEvent.ACTION_MOVE:

        if(pointerCount == 2){

        }

        break;

      case MotionEvent.ACTION_UP:

        break;

    }

 

    return true;

  }

posted on 2013-05-30 18:42  asi24  阅读(465)  评论(0编辑  收藏  举报