简体   繁体   English

每个事件两次调用Android 2.2 SurfaceView#onTouchEvent()

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

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. 因此,我跟随着一个2D图形教程(感兴趣的人,那是一个@ http://droidnova.com/ ),然后到了我在onTouchEvent()回调中不断崩溃的地步。 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. 我仍在尝试跟踪该错误,但是在尝试调试该问题时,我注意到我的onTouchEvent回调针对每个事件被调用两次。

For rerference 供参考

@Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        int action = event.getAction();
        synchronized (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) 动作= 0(MotionEvent.ACTION_DOWN)
  2. action = 0 (MotionEvent.ACTION_DOWN) 动作= 0(MotionEvent.ACTION_DOWN)
  3. action = 1 (MotionEvent.ACTION_MOVE) 动作= 1(MotionEvent.ACTION_MOVE)
  4. action = 1 (MotionEvent.ACTION_MOVE) 动作= 1(MotionEvent.ACTION_MOVE)
  5. action = 2 (MotionEvent.ACTION_UP) 动作= 2(MotionEvent.ACTION_UP)
  6. action = 2 (MotionEvent.ACTION_UP) 动作= 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. 首先,我自己是一个全新的android newb。

According to developer documentation, the onTouchEvent method requires you to override the method with your own implementation of it. 根据开发人员文档,onTouchEvent方法要求您使用自己的实现重写该方法。 This seems rather impractical, as you need to extend every view object where you would like an action with a touchevent. 这似乎是不切实际的,因为您需要将每个视图对象扩展到您希望通过touchevent进行操作的位置。

If i understand correctly from the android documentation for event handling ; 如果我从android文档中正确理解了事件处理 ; one must implement one of the various event listeners (according to your needs) rather then implementing the onTouchEvent method. 一个人必须实现各种事件侦听器之一(根据您的需要),而不是实现onTouchEvent方法。 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. 我的主意是,如果您确实要重写onTouchEvent,则该方法必须自己搜索哪个事件处理程序适合该工作。 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! 如果情况发生变化,我会通知您!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM