Android (x,y) 连续触摸(拖动)坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10791389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:58:48  来源:igfitidea点击:

Android (x,y) continuous touch (drag) coordinates

androidtouchcoordinates

提问by user1422535

I'm new to android, and I've been trying for a while to find out how can I retrieve the coordinates of a continuous touch on the screen. for example have 2 vars (x,y) that update in real time as finger moves around. I got it how to find it when the touch is made, but I really don;t get it how to make it return the result after that , when the finger is moving.

我是 android 新手,我一直在尝试找出如何检索屏幕上连续触摸的坐标。例如,有 2 个变量 (x,y) 在手指移动时实时更新。我知道如何在进行触摸时找到它,但我真的不知道如何让它在手指移动时返回结果。

I've been trying switch statements, while/for loops in different combination with ACTION_MOVE./ UP/ DOWN .. .still nothing.

我一直在尝试 switch 语句,while/for 循环与 ACTION_MOVE./ UP/ DOWN 的不同组合......仍然没有。

I've found like the same question on the website, but the answers only fit for the first step(showing the coordination from the touch only) I'd really appreciate a solution to this! Thanks!

我在网站上发现了同样的问题,但答案仅适用于第一步(仅显示触摸协调)我真的很感激这个问题的解决方案!谢谢!

回答by Tim

Without seeing your code I'm just guessing, but essentially if you don't return trueto the first call to onTouchEvent, you won't see any of the subsequent events in the gesture (MOVE, UP, etc).

没有看到您的代码,我只是猜测,但基本上,如果您不返回true对 的第一次调用onTouchEvent,您将看不到手势中的任何后续事件(移动、向上等)。

Maybe that is your problem? Otherwise please put code sample up.

也许那是你的问题?否则请放上代码示例。

回答by user1422535

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final TextView xCoord = (TextView) findViewById(R.id.textView1);
    final TextView yCoord = (TextView) findViewById(R.id.textView2);

    final View touchView = findViewById(R.id.textView3);
    touchView.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            final int action = event.getAction();
            switch (action & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN: {
                    xCoord.setText(String.valueOf((int) event.getX()));
                    yCoord.setText(String.valueOf((int) event.getY()));
                    break;
                }

                case MotionEvent.ACTION_MOVE:{
                    xCoord.setText(String.valueOf((int) event.getX()));
                    yCoord.setText(String.valueOf((int) event.getY()));
                    break;
                }
            }
            return true;

        }

    });
}

回答by K_Anas

You need to implement an OnTouchListenerfor whatever view you want to recognize the drag.

您需要为OnTouchListener要识别拖动的任何视图实现一个。

Then in the OnTouchListeneryou need to display the X and Y coordinates. I believe you can get those via MotionEvent.getRawX()and MotionEvent.getRawY()

然后在OnTouchListener您需要显示 X 和 Y 坐标。我相信你可以通过MotionEvent.getRawX()MotionEvent.getRawY()

You can use the MotionEvent.getAction()method to find out when a drag is occurring. I believe the constant is MotionEvent.ACTION_MOVE. Here is some psuedo-code:

您可以使用该MotionEvent.getAction()方法找出拖动发生的时间。我相信常数是MotionEvent.ACTION_MOVE。这是一些伪代码:

Add OnTouchListener interface

添加 OnTouchListener 接口

public class XYZ extends Activity implements OnTouchListener

Register the listener in the onCreate method

在 onCreate 方法中注册监听器

public void onCreate(Bundle savedInstanceState)
{
    //other code

    View onTouchView = findViewById(R.id.whatever_id);
    onTouchView.setOnTouchListener(this);
}

Implement the onTouch method

实现 onTouch 方法

public boolean onTouch(View view, MotionEvent event) 
{
    if(event.getAction() == MotionEvent.ACTION_MOVE)
    {
        float x = event.getRawX();
        float y = event.getRawY();
        //  Code to display x and y go here
        // you can print the x and y coordinate in a textView for exemple
    }
}