Question

The app is following. I have a line at the screen. I touch the screen and move finger. At the moment when my finger touches this line - I want to show a message at the moment of intersection.

The code:

    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction() ;
            case MotionEvent.ACTION_DOWN: 
                break;
            case MotionEvent.ACTION_MOVE:
                boolean isline = check_line(event.getX(),event.getY());
                if(isline){
                    //SHOW MESSAGE
                }
                break;
            case MotionEvent.ACTION_UP:   
                break;
            }
        return true;
    }

But the app sometimes can't catch the moment when when the finger is on the line. If I move slowly - then the message always appears. But if I move a bit faster - no messages.

The equation of the line is A*x + B*y + C = 0

check_line is a mathematical function, which just places my x and y in this equation. So when the equation is satisfied it returnes true, otherwise - false.

But I want all the cases to be catched.

What to do?

Était-ce utile?

La solution

If you move fast then you can't get all points where you moving that's why your equation going wrong..

Solution:

Use Line Intersect equation.. For line intersect you need four x-y value. You have already two x-y value from line and third x-y value is current touch event and forth x-y value should be last touch event.

int lastX;
int lastY;
int currentX;
int currentY;

case MotionEvent.ACTION_MOVE:
   currentX = event.getX();
   currentY = event.getY();

   //your line intersect code


   lastX = event.getX();
   lastY = event.getY();
break;

Some Link for Line Intersect:

Solution1

Solution2

Autres conseils

To hit the line you currently have hit exactly the line, so you should increase the "field of touch'.

For example you can make a circle (whose center is in getX and getY) with a some radius and check if a circle intersects with your line.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top