Getting All x and y coordinates from a motion even

2019-07-10 10:18发布

问题:

I'm currently working on my own basic drawing app. So far, my app is working well but I noticed that my motion event doesn't seem to get all the X and Y axis points that are touched.

When I move my finger across the screen, there is noticeable spaces between the circles. Only when I move my finger slowly does it capture all the points. Is there a way I can grab all the points or is there a way i can optimize it to be able to handle all the points?

Here's how I'm doing it:

setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

                int x; 
            int y;    

            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                {
                    x = (int) event.getX();
                    y = (int) event.getY();   
                    //method draws circle at x and y coordinate
                    MyPainter mp = new MyPainter(x,y);

                    break;
                }
                case MotionEvent.ACTION_MOVE:
                {
                    x = (int) event.getX();
                    y = (int) event.getY();
                MyPainter mp = new MyPainter(x,y);

                    break;
                }
            }
            return true;

        }
    });

Any Suggestions or comments are appreciated. Thanks

回答1:

This is just the way the hardware is designed. The hardware samples the touchscreen N times per second, and spits out the coordinates every time it senses a finger.

If you move faster than the screen samples, you'll have gaps in the reported data. Only thing you can do is to generate the missing points yourself by interpolating along the line between the two touches.



回答2:

The accepted answer isn't strictly correct, it is possible to access the touch data from the graps using the following methods:

event.getHistorySize();   // returns the number of data points stored as an int
event.getHistoricalX(int);
event.getHistoricalX(int);

Other related methods are available when there are multiple pointers (see documentation).

A good example of how this can be used in practice can be found here.