离开OnTouchListener

时间:2016-12-02 08:43:37

标签: android ontouchlistener

我正在尝试将坐标重置回原始位置,但我认为应该这样做,似乎并没有像我认为的那样工作。 这是重要的代码:

num1.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        startX = num1.getX(); // num1 is a TextView
        startY = num1.getY(); // here I am trying to get the original coordinates

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = event.getRawX();
                y = event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                x = event.getRawX();
                y = event.getRawY();
                num1.setX(x - 100);
                num1.setY(y - 250); // "y - 250" keeps the textview under finger
                apple1.setX(x - 100);
                apple1.setY(y - 250);
                break;

            case MotionEvent.ACTION_UP:
                if (((num1.getX() >= basket1.getX()) && (num1.getX() < basket2.getX())) && (event.getRawY() > linearLayout.getY())) {
                    basket1Num = String.valueOf(num1.getText());
                    basket1Text.setText(basket1Num);
                } else if (((num1.getX() >= basket2.getX()) && (num1.getX() > basket1.getX())) && (event.getRawY() > linearLayout2.getY())) {
                    basket2Num = String.valueOf(num1.getText());
                    basket2Text.setText(basket2Num);
                } else {
                    num1.setX(startX); // if the user has moved it somewhere other than the basket the TextView should go back.
                    num1.setY(startY);
                }
        }
        return true;
    }
});

所以基本上num1是一个TextView,在我试图获得起始位置的开关上方。如果用户没有将TextView放入篮子图像中,我希望它回到原来的位置。我虽然收集起始xy位置并告诉应用程序将其设置回那些位置会做到但它没有效果。任何帮助都会很棒。

谢谢!

编辑:Apple1是遵循Num1 TextView

的ImageView

1 个答案:

答案 0 :(得分:0)

每次触发触摸事件时,您都要设置起始坐标。这意味着在您拖动时,您将坐标重置为其当前位置。它失去了起始坐标的状态。

您可能想要做的是,当您开始拖动时,您可以设置起始坐标。在下面的示例中,我刚刚将起始坐标设置为ACTION_DOWN。按下时会触发一次。

num1.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = num1.getX(); // num1 is a TextView
                startY = num1.getY(); // here I am trying to get the original coordinates

                x = event.getRawX();
                y = event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                x = event.getRawX();
                y = event.getRawY();
                num1.setX(x - 100);
                num1.setY(y - 250); // "y - 250" keeps the textview under finger
                apple1.setX(x - 100);
                apple1.setY(y - 250);
                break;

            case MotionEvent.ACTION_UP:
                if (((num1.getX() >= basket1.getX()) && (num1.getX() < basket2.getX())) && (event.getRawY() > linearLayout.getY())) {
                    basket1Num = String.valueOf(num1.getText());
                    basket1Text.setText(basket1Num);
                } else if (((num1.getX() >= basket2.getX()) && (num1.getX() > basket1.getX())) && (event.getRawY() > linearLayout2.getY())) {
                    basket2Num = String.valueOf(num1.getText());
                    basket2Text.setText(basket2Num);
                } else {
                    num1.setX(startX); // if the user has moved it somewhere other than the basket the TextView should go back.
                    num1.setY(startY);
                }
        }
        return true;
    }
});