android - 多点触控的处理

标签 android multi-touch

所以我想做的就是围绕正在触摸的两个点绘制圆圈,当手指在屏幕上拖动时,让这些圆圈跟随每个手指。

但是,我遇到了一些我无法弄清楚的奇怪行为。因此,当我将两根手指放在屏幕上时,我的圆圈就没有问题了。当我用两根手指拖动时,一切都会按照我想要的方式进行。但是,如果我抬起第一根手指,所有内容都会停止重绘,并且仍在屏幕上的第二根手指将停止被跟踪。如果我把第一个手指放回去,一切都会恢复正常。我想我的两根手指一定处理不好。

有人能看出我做错了什么吗?

此外,如果我有两根手指放在屏幕上,并且抬起并触摸第二根手指,一切都会正常。只有在我有两个手指并且抬起第一个手指的情况下,我才会发现问题。

@Override
    public boolean onTouchEvent(MotionEvent event) {    

       /*
       * Log this touch event 
       */
      String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
                  "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
       StringBuilder sb = new StringBuilder();
       int action = event.getAction();
       int actionCode = action & MotionEvent.ACTION_MASK;

       sb.append("OnTouchEvent ACTION_" ).append(names[actionCode]);

       if (actionCode == MotionEvent.ACTION_POINTER_DOWN
             || actionCode == MotionEvent.ACTION_POINTER_UP) {
          sb.append("(pid " ).append(
          action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
          sb.append(")" );
       }
       sb.append("[" );

       for (int i = 0; i < event.getPointerCount(); i++) {
          sb.append("#" ).append(i);
          sb.append("(pid " ).append(event.getPointerId(i));
          sb.append(")=" ).append((int) event.getX(i));
          sb.append("," ).append((int) event.getY(i));
          if (i + 1 < event.getPointerCount())
          sb.append(";" );
       }
       sb.append("]" );

       Log.d(TAG, sb.toString());


        /*
        * Track the touches that are on the screen
        * Grab X,Y, and Size data
        */
        int action = event.getAction();
        int actionCode = action & MotionEvent.ACTION_MASK;

        if (actionCode == MotionEvent.ACTION_POINTER_DOWN) {

            if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p0Down = true;
            }

            if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p1Down = true;
            }
        }
        else if (actionCode == MotionEvent.ACTION_POINTER_UP){

            if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p0Down = false;
            }

            if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p1Down = false;
            }
        }

        if(actionCode == MotionEvent.ACTION_DOWN){

            if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p0Down = true;
            }

            if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p1Down = true;
            }
        }


        else if (actionCode == MotionEvent.ACTION_UP){

            if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p0Down = false;
            }

            if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
                p1Down = false;
            }
        }


        int pointCt = event.getPointerCount();
        for(int i = 0; i < pointCt; i++){

            int pid = event.getPointerId(i);
            if((pid == 0) && p0Down){
                curX1 = event.getX(pid);
                curY1 = event.getY(pid);
                size1 = event.getSize(pid);
            }
            if((pid == 0) && (p0Down == false)){
                curX1 = -1;
                curY1 = -1;
                size1 = -1;
            }
            if((pid == 1) && p1Down){
                curX2 = event.getX(pid);
                curY2 = event.getY(pid);
                size2 = event.getSize(pid);
            }
            if((pid == 1) && (p1Down == false)){
                curX2 = -1;
                curY2 = -1;
                size2 = -1;
            }

        }
        return true;
    }

最佳答案

您正在使用指针 ID 作为参数而不是索引来调用 MotionEvent#getX()/getY()/getSize()。

关于android - 多点触控的处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3772736/

相关文章:

android - Flutter构建APK在Android Studio上不起作用

android - Android 中的多点触控 Canvas 旋转

android - 多点触控 Android - 一个手指使用一半屏幕,另一个手指使用一半屏幕

ios - 如何让这些方法适用于多点触控?

android - 获取 TapTargetView 的 MenuItem View 引用

java - 如何在 Java 桌面应用程序中模拟 Android 类型的文件系统?

android - 图像到整数

android - 总是恢复到旧版本

iPhone 多点触控 - 一些触摸调度 touchesBegan : but not touchesMoved:

c# - 触摸事件不触发 C# WPF