遇到MotionEvent.ACTION_UP问题

时间:2017-08-03 19:01:30

标签: java android motionevent joystick

我正在实施一个操纵杆应用程序,并且在处理动作事件时遇到问题。除了我试图在ACTION_MOVE之后捕获事件之外,大部分工作都很好。因此,在用户完成拖动操纵杆之后,我想在MotionEvent案例中执行一些代码。 ACTION_UP不满足此要求,尽管在ACTION_MOVE之后放置ACTION_UP确实有效,并且一切运行正常,但仅在调试模式下,而不是在实际运行程序时非常奇怪。以下是有问题的代码:

public boolean onTouch(View v,MotionEvent me){

        switch(v.getId()) {
            case R.id.x_button:
                switch(me.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        buttonPressed = "X";
                       new SendServer().execute();
                        break;

                    case MotionEvent.ACTION_UP:

                        break;

                }
                break;


            case R.id.a_button:
                 switch(me.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        buttonPressed = "Q";
                       new SendServer().execute();
                        break;

                    case MotionEvent.ACTION_UP:

                        break;
                }


                break;

            case R.id.y_button:
                switch(me.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        buttonPressed = "Y";
                        new SendServer().execute();
                        break;

                    case MotionEvent.ACTION_UP:

                        break;
                }
                break;

            case R.id.b_button:
                switch(me.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        buttonPressed = "B";
                        new SendServer().execute();
                        break;

                    case MotionEvent.ACTION_UP:

                        break;
                }
                break;

            case R.id.start_button:
                switch(me.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        buttonPressed = "START";
                        new SendServer().execute();
                        break;

                    case MotionEvent.ACTION_UP:

                        break;
                }
                break;

            case R.id.select_button:
                switch(me.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        buttonPressed = "SELECT";
                        new SendServer().execute();
                        break;

                    case MotionEvent.ACTION_UP:

                        break;

                    case MotionEvent.ACTION_CANCEL:

                        break;
                }
                break;

            case R.id.rb_button:
                switch(me.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        buttonPressed = "RB";
                        new SendServer().execute();
                        break;

                    case MotionEvent.ACTION_UP:

                        break;
                }
                break;

            case R.id.lb_button:
                switch(me.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        buttonPressed = "LB";
                        new SendServer().execute();
                        break;

                    case MotionEvent.ACTION_UP:
                        break;
                }
                break;

            case R.id.rt_button:
                switch(me.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        buttonPressed = "RT";
                        new SendServer().execute();
                        break;

                    case MotionEvent.ACTION_UP:
                        break;
                }
                break;

            case R.id.lt_button:
                switch(me.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        buttonPressed = "LT";
                        new SendServer().execute();
                        break;

                    case MotionEvent.ACTION_UP:

                        break;
                }





            case R.id.analogStick:

                switch(me.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        x = me.getRawX();
                        y = me.getRawY();
                        System.out.println(x);
                        System.out.println(y);
                        dx = x - v.getX();
                        dy = y - v.getY();
                        //direction_result = sendDirection(originX, originY, me.getRawX(), me.getRawY());
                        //new SendServer().execute();
                        break;



                    case MotionEvent.ACTION_MOVE:

                       float xx = (me.getRawX() - originX) - dx;
                       float yy = (me.getRawY() - originY) - dy;

                       float displacement = (float) Math.sqrt((xx * xx) + (yy * yy));
                            if (displacement < deadzoneRadius) {
                                v.setX(me.getRawX() - dx);
                                v.setY(me.getRawY() - dy);
                                direction_result = sendDirection(originX, originY, me.getRawX(), me.getRawY());
                                new SendServer().execute();
                                System.out.println(me.getRawX() - dx);
                                System.out.println(me.getRawY() - dy);

                            }

                            else {
                                float ratio = (float) deadzoneRadius / displacement;
                                float constrainedX = originX + ((me.getRawX() - originX) - dx) * ratio;
                                float constrainedY = originY + ((me.getRawY() - originY) - dy) * ratio;
                               direction_result = sendDirection(originX, originY, me.getRawX(), me.getRawY());
                                new SendServer().execute();
                                v.setX(constrainedX);
                                v.setY(constrainedY);
                                me.getActionMasked();
                            }
                            break;

                    case MotionEvent.ACTION_UP:
                        xDirection2 = me.getRawX();
                        yDirection2 = me.getRawY();
                        delta_xDirection = x - xDirection2;
                        delta_yDirection = y - yDirection2;
                        v.setX(originX);
                        v.setY(originY);
                        resetKeyboardState();
                        new SendServer().execute();
                        break;



                }
                break;
        }

        return true;
    }

此外,我的问题似乎与这篇文章非常相似,但如果这有用的话,它从未被回答:how to detect when MotionEvent.ACTION_MOVE is finished

0 个答案:

没有答案