JayRow
JayRow

Reputation: 139

Check when finger enters ImageView

(I know that there are others like this, but bear with me because I have a special problem)

So I am developing a game called Dodge in which the user dodges ImageViews that come from the top of the screen to the bottom. What I am aiming to do is that when the user's finger enters the ImageView, run endGame(). I have already set it up to lose when you lift your finger.

The problem here is that you are starting the game by using an ACTION_DOWN event on a TextView, and when you lift your finger, you lose. The problem with this is that I cannot figure out how to run another onTouch inside the one already running from a finger drag.

My TextView onClick()

title3.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            int eventAction = event.getAction();

            switch (eventAction) {
                case MotionEvent.ACTION_DOWN:
                    //On button down
                    startGame();
                    break;

                case MotionEvent.ACTION_UP:
                    // finger leaves the button
                    endGame();
                    break;
            }

            return false;
        }
    });

Upvotes: 0

Views: 117

Answers (3)

Umang Kothari
Umang Kothari

Reputation: 3694

Action_Down -- for the first pointer(finger) that touches the screen.This starts the gesture.

Action_Pointer_Down -- for the extra pointer(second finger) that enters the screen beyond the first.

Action_Move -- A change has happened during a press gesture

Action_Pointer_Up -- sent when a non primary pointer goes up.

Action_Up -- sent when the last pointer leaves the screen

now you can code according to above actions.

Upvotes: 0

Hossein
Hossein

Reputation: 324

onTouch called Continuous when user touch the screen, and usually its enough
you must use getX and getY function to indicate finger location and do something
and use MotionEvent.ACTION_CANCEL when finger out of view

Thnak you

Upvotes: 0

Chris Feist
Chris Feist

Reputation: 1676

Use MotionEvent.ACTION_MOVE in conjunction with event.getX() and event.getY() and see if it that x, y coordinate falls within one of the falling pic's boundaries.

Upvotes: 1

Related Questions