【问题标题】:Get current x and y co-ordinates of touch (if the user is touching) every 20 milliseconds - Timers, Schedulers?每 20 毫秒获取当前的 x 和 y 触摸坐标(如果用户正在触摸) - 计时器、调度程序?
【发布时间】:2013-08-09 12:33:20
【问题描述】:

我想使用一个函数,我可以每 20 毫秒或类似的时间获取用户的 x 和 y 坐标(如果他/她当前正在触摸屏幕)。我知道 Android 中有计时器和调度程序。但问题是如何在不使用 OnTouch 事件侦听器的情况下获得 X 和 y 坐标。

或者我如何确保每 20 毫秒调用一次 OnTouch,或者在当前点被用户触摸时,或者如果用户在该实例中没有触摸屏幕,则为空。

时间是一个重要因素。我确实检查了线程,但不确定是否可以使用计时器或调度程序。

【问题讨论】:

    标签: android touch-event ontouchlistener timertask


    【解决方案1】:

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

    最终视图 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;
    
    }
    

    });

    【讨论】:

    • 我认为这不会帮助我每 20 毫秒获得一次 X 和 Y 坐标?基本上,当用户触摸屏幕时会调用 onTouch。然而,我想每 20 毫秒获得一次积分。如果用户在某些情况下没有触摸屏幕,它应该只是返回空白。但是感谢您的尝试。如果您有更多的想法,那将是非常有帮助的!
    猜你喜欢
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多