在我的 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 ...
}