alexward1230
alexward1230

Reputation: 589

Android development, how to get approximate coordinates?

Ok, so I am making a simple app on a surfaceView where I have a bitmap of a ball that goes from the top of the screen to the bottom. Once it gets to the bottom it appears at the top again where it starts to fall back down again.

Next I tried to make it so that when I clicked the ball when it was falling down it would go back to the top of the screen. However, I am having a problem with this because I can't click it (because its only one pixel, I think). I set an onTouchListener to the getX() and getY() coordinates of the click, and if the x and y coordinatesof the click are equal to the x and y of the current position of the ball then the ball goes back to the top of the screen.

This doesnt work though. Because in order for me to be able to click the ball I would have to click the exact center pixle that the ball was on at that time. So my question is how do I say: If the click is close to, or approximately equal to the current position of the ball then go back to the top. I am a noob with this so if I am asking dumb questions I apologize, I'm trying my best to learn. And thank you very much for the help. I appreciate it.

Upvotes: 1

Views: 420

Answers (1)

asish
asish

Reputation: 4807

It should work for you. Just override the onTouch method.

@Override
    public boolean onTouch(View v, MotionEvent me)
    {
        try
        {
            Thread.sleep(50);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        switch (me.getAction())
        {
        case MotionEvent.ACTION_DOWN:
            x = me.getX();
            y = me.getY();
            break;
        case MotionEvent.ACTION_UP:
            x = me.getX();
            y = me.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            x = me.getX();
            y = me.getY();
            break;
        case MotionEvent.ACTION_OUTSIDE:
            x = 100;
            y = 200;
            break;
        }
        return true;
    }

Upvotes: 3

Related Questions