Getting All x and y coordinates from a motion event in android

一个人想着一个人 提交于 2019-12-13 16:46:15

问题


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.



来源:https://stackoverflow.com/questions/13547875/getting-all-x-and-y-coordinates-from-a-motion-event-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!