【问题标题】:Android (x,y) continuous touch (drag) coordinatesAndroid (x,y) 连续触摸(拖动)坐标
【发布时间】:2012-05-28 23:16:38
【问题描述】:

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

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

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

【问题讨论】:

    标签: android touch coordinates


    【解决方案1】:

    没有看到你的代码我只是在猜测,但基本上如果你不返回true 到第一次调用onTouchEvent,你将看不到手势中的任何后续事件(MOVE,UP , ETC)。

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

    【讨论】:

    • 非常感谢蒂姆。这真的很有意义(愚蠢的错误)。它现在正在工作。这里也有一些代码会再次遇到同样的错误:)
    【解决方案2】:
    @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;
    
            }
    
        });
    }
    

    【讨论】:

      【解决方案3】:

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

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

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

      添加 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 没有任何更改来返回。!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多