Android多点触控:onTouch()MotionEvent

时间:2016-05-24 01:32:44

标签: android multi-touch

我试图用多点触摸发出屏幕标准化坐标的OSC消息。我使用MotionEvent.ACTION_DOWN进行第一次触摸,使用MotionEvent.ACTION_POINTER_DOWN进行其他手指。为坐标。我把

int pointerIndex = event.getActionIndex();

然后x:

float x = (event.getX(pointerIndex) / touchViewWidth - 0.5f) * 2.f;

然而,结果是我只能进行一次触摸,但会触发两种情况。这是代码,谢谢你的帮助。:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final TextView textView = (TextView)findViewById(R.id.textView);
        final View touchView = findViewById(R.id.touchView); // listen for touch event;

        touchView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    touchView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    touchView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
                touchViewWidth = touchView.getWidth();
                touchViewHeight = touchView.getHeight();
            }
        });

        touchView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int pointerIndex = event.getActionIndex();
                switch( event.getAction()){
                    case MotionEvent.ACTION_DOWN: {
                        float x = (event.getX(pointerIndex) / touchViewWidth - 0.5f) * 2.f;
                        float y = -2.f * (event.getY(pointerIndex)/(touchViewHeight * 0.9f) - 0.5f);
                        Log.d("CLICK", " click!");
                        textView.setText("Touch coordinates : " +
                                String.valueOf(x) + " x " + String.valueOf(y));
                        Thread t = new Thread(new oscThread(x, y));
                        t.start();

                    }

                    case MotionEvent.ACTION_POINTER_DOWN: {
                        float x = (event.getX(pointerIndex) / touchViewWidth - 0.5f) * 2.f;
                        float y = -2.f * (event.getY(pointerIndex)/(touchViewHeight * 0.9f) - 0.5f);
                        Log.d("CLICK", " click!");
                        textView.setText("Touch coordinates : " +
                                String.valueOf(x) + " x " + String.valueOf(y));
                        Thread t = new Thread(new oscThread(x, y));
                        t.start();
                    }
                }
                return true;
            }
        });

0 个答案:

没有答案