Detect touch position in Android

This question already has an answer here:

  • Get the co-ordinates of a touch event on Android 4 answers
  • Fling gesture detection on grid layout 18 answers

  • 尝试这个,

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final TextView textView = (TextView)findViewById(R.id.textView);
        // this is the view on which you will listen for touch events
        final View touchView = findViewById(R.id.touchView);
        touchView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                textView.setText("Touch coordinates : " +
                    String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
                    return true;
            }
        });
    }
    

    If Using PhoneGap you can use the HTML/JS. Refer Below

    recommend this link- http://miloq.blogspot.in/2011/05/coordinates-mouse-click-canvas.html

    <style type="text/css">
    
    #canvas{background-color: #000;}
    
    </style>
    
    <script type="text/javascript">
    
      document.addEventListener("DOMContentLoaded", init, false);
    
      function init()
     {
        var canvas = document.getElementById("canvas");
        canvas.addEventListener("mousedown", getPosition, false);
      }
    
     function getPosition(event)
     {
    var x = new Number();
    var y = new Number();
    var canvas = document.getElementById("canvas");
    
    if (event.x != undefined && event.y != undefined)
    {
      x = event.x;
      y = event.y;
    }
    else // Firefox method to get the position
     {
         x = event.clientX + document.body.scrollLeft +
            document.documentElement.scrollLeft;
          y = event.clientY + document.body.scrollTop +
          document.documentElement.scrollTop;
         }
    
    x -= canvas.offsetLeft;
    y -= canvas.offsetTop;
    
        alert("x: " + x + "  y: " + y);
      }
    
    </script>
    
    链接地址: http://www.djcxy.com/p/91206.html

    上一篇: 如何在Android中通过手势轻松滑动?

    下一篇: 检测Android中的触摸位置