Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android(x,y)连续触摸(拖动)坐标_Android_Touch_Coordinates - Fatal编程技术网

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

Android(x,y)连续触摸(拖动)坐标,android,touch,coordinates,Android,Touch,Coordinates,我是android新手,我一直在努力寻找如何在屏幕上检索连续触摸的坐标。例如,有两个变量(x,y)随着手指的移动而实时更新。我知道如何在触摸时找到它,但我真的不知道;当手指移动时,不知道如何让它返回结果 我一直在尝试switch语句,而/for循环与ACTION\u MOVE/UP/DOWN的不同组合。还是没什么 我在网站上发现了同样的问题,但答案只适用于第一步(仅显示触摸的协调性) 我真的很感激能找到解决办法!谢谢 没有看到您的代码,我只是在猜测,但本质上,如果您没有将true返回到对onTo

我是android新手,我一直在努力寻找如何在屏幕上检索连续触摸的坐标。例如,有两个变量(x,y)随着手指的移动而实时更新。我知道如何在触摸时找到它,但我真的不知道;当手指移动时,不知道如何让它返回结果

我一直在尝试switch语句,而/for循环与ACTION\u MOVE/UP/DOWN的不同组合。还是没什么

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

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


也许这就是你的问题?否则,请放置代码示例。

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

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

您可以使用
MotionEvent.getAction()
方法来确定何时发生拖动。我相信常数是
MotionEvent.ACTION\u MOVE
。以下是一些psuedo代码:

添加OnTouchListener接口

public class XYZ extends Activity implements OnTouchListener
在onCreate方法中注册侦听器

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

    View onTouchView = findViewById(R.id.whatever_id);
    onTouchView.setOnTouchListener(this);
}
实现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
    }
}

非常感谢蒂姆。这真的有道理(愚蠢的错误)。它正在工作。下面是一些代码,用于那些再次遇到相同错误的人:)非常感谢:)虽然这是正确的,但我面临的问题是返回语句是错误的,因此Action_Move没有更改以返回某些内容。!
@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;

        }

    });
}