0

在下面的代码中,我正在我的设备上测试多点触控。我遇到的问题是移动动作。如果我用一根手指触摸屏幕,我会在 Logcat 中获得触摸输入的 ID,当我用第二根手指触摸屏幕并开始移动它时,我会获得两根手指的 ID,尽管第一根手指不是动不动!我很困惑,需要一些帮助来改进我的代码。

@Override
public boolean onTouch(View v, MotionEvent event) {
    int pointerId;
    int pointerIndex;

    synchronized (gameLoop) {
        for (int i = 0; i < event.getPointerCount() && i < 2; i++) {

            num = event.getPointerCount();
            //id = event.getPointerId(i);

            // Down
            if(event.getActionIndex() == i && (event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN || event.getActionMasked() == MotionEvent.ACTION_DOWN)) {

                int j = event.getPointerCount();
                int id = event.getPointerId(i);
                String numDown = Integer.toString(j);
                String sId = Integer.toString(id);
                Log.i("Test", "Number DOWN: " + numDown + " ID: " + sId);
            }

            // Move
            if(event.getActionMasked() == MotionEvent.ACTION_MOVE) {

                int j = event.getPointerCount();
                int id = event.getPointerId(i);
                String numDown = Integer.toString(j);
                String sId = Integer.toString(id);
                Log.i("Test", "Moving: " + numDown + " ID: " + sId);
            }

            // Up
            if(event.getActionIndex() == i && (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP || event.getActionMasked() == MotionEvent.ACTION_UP)) {

                int k = event.getPointerCount();
                int kid = event.getPointerId(i);
                String numUp = Integer.toString(k);
                String ssId = Integer.toString(kid);
                Log.i("Test", "Number UP: " + numUp + " ID: " + ssId);
            }
        }
    }
    return true;
}
4

1 回答 1

0
 case MotionEvent.ACTION_MOVE:

        int j= event.getPointerCount();

        int fingerOneDown=0;
        int fingerTwoDown=0;
        for(int i = 0; i < j; ++i)
        {
            int pointerIndex = i;
            id= event.getPointerId(pointerIndex);
            Log.d("pointer id - move",Integer.toString(id));
            if(id== 0)
            {
                fingerOneDown = 1;
                int fingerOneX = event.getX(pointerIndex);
                int fingerOneY = event.getY(pointerIndex);
                Log.e("finger touch","touch=="+fingerOneX+","+fingerOneY);

            }
            if(pointerId == 1)
            {
                fingerTwoDown = 1;
                int fingerTwoX = event.getX(pointerIndex);
                int fingerTwoY = event.getY(pointerIndex);

                Log.e("finger touch","touch=="+fingerTwoX+","+fingerTwoY);
            }
        }
        break;
于 2013-06-22T06:32:55.823 回答