开发者

Android 2.2 SurfaceView#onTouchEvent() being called twice for each event

开发者 https://www.devze.com 2023-02-14 07:15 出处:网络
So I was following along with a 2D graphics tutorial (those interested, its the one @ http://droidnova.com/), and I got to a point where I\'ve been experiencing constant crashes in my onTouchEvent() c

So I was following along with a 2D graphics tutorial (those interested, its the one @ http://droidnova.com/), and I got to a point where I've been experiencing constant crashes in my onTouchEvent() callback. I'm still trying to track the bug, but while trying to debug the issue I noticed that my onTouchEvent callback gets called twice for each event.

For rerference

@Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        int action = event.getAction();
        sy开发者_如何转开发nchronized (getHolder()) {
            if (action == MotionEvent.ACTION_DOWN) {
                if (gameObjects.size() < 2) {
                    currentlySelected = new GameObject(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
                    currentlySelected.getCoordinates().setX((int) event.getX());
                    currentlySelected.getCoordinates().setY((int) event.getY());
                } else {
                    for (GameObject gameObject: gameObjects) {
                        if (gameObject.inBoundingBox(event.getX(), event.getY())) {
                            currentlySelected = gameObject;
                            break;
                        }
                    }

                    if (currentlySelected != null) {
                        // Remember to remove it from the list so that we don't waste time updating it in updatePhysics twice.
                        gameObjects.remove(currentlySelected);
                    }
                }
            } else if (action == MotionEvent.ACTION_UP && (currentlySelected != null)) {
                gameObjects.add(currentlySelected);
                currentlySelected = null;
            } else if (action == MotionEvent.ACTION_MOVE) {
                currentlySelected.getCoordinates().setX((int) event.getX());
                currentlySelected.getCoordinates().setY((int) event.getY());
            }
        }

No when I put a breakpoint on the first line, and run it in debug mode, when I touch the screen the callback gets called with the following action's...

  1. action = 0 (MotionEvent.ACTION_DOWN)
  2. action = 0 (MotionEvent.ACTION_DOWN)
  3. action = 1 (MotionEvent.ACTION_MOVE)
  4. action = 1 (MotionEvent.ACTION_MOVE)
  5. action = 2 (MotionEvent.ACTION_UP)
  6. action = 2 (MotionEvent.ACTION_UP)

for each action, the second one gets out at the synchronized call. Anyone know the reason for this behavior?


As it turns out, the problem was with the line

synchronized (getHolder()) {

It should really be

SurfaceHolder h = getHolder()
syncronized(h) {

That seemed to solve it for me.


First of all, i'm myself a total android newb.

According to developer documentation, the onTouchEvent method requires you to override the method with your own implementation of it. This seems rather impractical, as you need to extend every view object where you would like an action with a touchevent.

If i understand correctly from the android documentation for event handling ; one must implement one of the various event listeners (according to your needs) rather then implementing the onTouchEvent method. MY GUESS is, that if you DO override the onTouchEvent, the method has to search for himself which of the event handlers is suitable for the job. therefore it has to pass on again the same information to this event listener, and therefore, we have "twice" the same event called...

This is just my noob guess, hope someone is out there to actually proof this.

I am in the same situation and i'll try out the guidelines written in de herementioned documentation page. If this changes stuff i'll let you know!

0

精彩评论

暂无评论...
验证码 换一张
取 消