2

在我的 Android 应用程序中,我有一个自定义View接收触摸事件。但是,我每次触摸它时它都不会做出反应 - 只是有时。据我所知,如果我触摸屏幕,移动手指,然后松开——即使我只移动一点——事件就会被拾取,但如果我点击屏幕太快以至于手指无法滑过它, 没发生什么事。我怎样才能解决这个问题?

这是视图的代码:

public class SpeedShooterGameView extends GameActivity.GameView {
    public SpeedShooterGameView(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
    }

    @Override
    protected GameThread getNewThread(SurfaceHolder holder, Context context) {
        return new SpeedShooterGameThread(holder, context);
    }

    // Program is driven by screen touches
    public boolean onTouchEvent(MotionEvent event) {
        SpeedShooterGameThread thread = (SpeedShooterGameThread) getThread();
        if (thread.isRunning()) {
            return thread.recieveTouch(event);
        } else {
             return false;
        }
    }
}

我非常确信该行SpeedShooterGameThread thread = (SpeedShooterGameThread) getThread();中返回的对象正在按我的预期工作,但如果上面的代码看起来不错,我也会发布该类中的相关代码。当thread.recieveTouch(event);被调用时,MotionEvent它被发送到另一个线程。

编辑:我将继续发布以下代码SpeedShooterGameThread

public class SpeedShooterGameThread extends GameActivity.GameView.GameThread {
    //... snip ...
    private Queue<MotionEvent> touchEventQueue;

    //... snip ...

    public synchronized final void newGame() { //called from the constructor, used to go to a known stable state
        //... snip ...
        touchEventQueue = new LinkedList<MotionEvent>();
        //... snip ...
    }

    //...snip...

    public synchronized boolean recieveTouch(MotionEvent event) {
        return touchEventQueue.offer(event);
    }

    private synchronized void processTouchEvents() {
        synchronized (touchEventQueue) {
            while (!touchEventQueue.isEmpty()) {
                MotionEvent event = touchEventQueue.poll();
                if (event == null) {
                    continue;
                }
                //... snip ....
            }
        }
    }

    //... snip ...
}
4

1 回答 1

0

我通过完全取出来修复了这个错误Queue<MotionEvent>。我的代码现在看起来像这样:

线程不再使用 a Queue,并在被调用MotionEvents时立即处理:recieveTouch()

public class SpeedShooterGameThread extends GameActivity.GameView.GameThread {
//The touchEvent member has been removed.

//... snip ...

    public synchronized final void newGame() { //called from the constructor, used to go to a known stable state
        // touchEvents is no longer initialized.

        //...snip...
    }

    //...snip...

    public synchronized boolean recieveTouch(MotionEvent event) {
        //Immediately handle the MotionEvent here,
        //or return false if the event isn't processed
    }

    // The processTouchEvents() method is removed.

    //... snip ...
}

视图不变:

public class SpeedShooterGameView extends GameActivity.GameView {
    public SpeedShooterGameView(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
    }

    @Override
    protected GameThread getNewThread(SurfaceHolder holder, Context context) {
        return new SpeedShooterGameThread(holder, context);
    }

    // Program is driven by screen touches
    public boolean onTouchEvent(MotionEvent event) {
        SpeedShooterGameThread thread = (SpeedShooterGameThread) getThread();
        if (thread.isRunning()) {
            return thread.recieveTouch(event);
        } else {
             return false;
        }
    }
}
于 2013-04-03T04:33:46.067 回答