Android的GestureDetector与内SurfaceView SimpleOnGestureListener [英] Android GestureDetector with SimpleOnGestureListener within SurfaceView
问题描述
public class GameActivity extends Activity {
private static final String TAG = "GameActivity";
. . .
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
Log.d(TAG, "- onTouchEvent()");
return super.onTouchEvent(event);
}
. . .
}
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "GameView";
. . .
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
gestureDetector.onTouchEvent(event);
Log.d(TAG, "- onTouchEvent()");
return super.onTouchEvent(event);
}
private SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {
private static final String TAG = "GestureListener";
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(TAG, "+ onSingleTapConfirmed(event:" + event + ")");
singleTapDetected = true;
Log.d(TAG, "- onSingleTapConfirmed()");
return true;
}
@Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(TAG, "+ onDoubleTap(event:" + event + ")");
doubleTapDetected = true;
Log.d(TAG, "- onDoubleTap()");
return true;
}
};
private GestureDetector gestureDetector = new GestureDetector(getContext(), gestureListener);
. . .
}
我跑了它的AVD作出的SurfaceView(GameView)一个单一的点击。结果
据LogCat中日志:
I ran it on AVD an made a single click on the SurfaceView (GameView).
According to LogCat logs:
03-11 14:19:51.171: D/GameView(4839): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223172, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.171: D/GameView(4839): - onTouchEvent()
03-11 14:19:51.171: D/GameActivity(4839): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223172, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.171: D/GameActivity(4839): - onTouchEvent()
03-11 14:19:51.299: D/GameActivity(4839): + onTouchEvent(event:MotionEvent { action=ACTION_UP, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223303, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.299: D/GameActivity(4839): - onTouchEvent()
有几个问题:
-
为什么
SimpleOnGestureListener
不叫? (即使onTouchEvent()
被称为和presummably行gestureDetector.onTouchEvent(事件);
被执行死刑)
Why the
SimpleOnGestureListener
was not called? (Even thoughonTouchEvent()
was called and presummably the linegestureDetector.onTouchEvent(event);
was executed)
为什么GameActivity的 onTouchEvent()
获得 ACTION_DOWN 和 ACTION_UP ,但在GameView的 onTouchEvent()
只得到 ACTION_DOWN
Why the GameActivity's onTouchEvent()
gets ACTION_DOWN and ACTION_UP, but the GameView's onTouchEvent()
only gets ACTION_DOWN?
有些事情我已经试过
-
我实现了
GestureDetector
使用同一code中的GameActivity
类,如上图所示和它的工作如预期,称onSingleTapConfirmed()
I implemented the
GestureDetector
on theGameActivity
class with the same code as shown above and it worked as expected, callingonSingleTapConfirmed()
在GameView,而不是一个私有成员 SimpleOnGestureListener
,创建了一个私人类GestureListener扩展SimpleOnGestureListener
和用它来构建 GestureDetector
。我看到的描述相同的行为,给听者没有呼叫。
In GameView, instead of a private member SimpleOnGestureListener
, created a private class GestureListener extends SimpleOnGestureListener
and used it to construct the GestureDetector
. I see the same behavior as described, no call to the listener.
推荐答案
我调试的code到Android源$ C $ C,GestureDetector类。这解释了为什么 SimpleOnGestureListener
不叫:
I debugged the code into the android source code, GestureDetector class. And this explains why SimpleOnGestureListener
was not called:
- 的ACTION_DOWN被接收并产生内部TAP消息GestureDetector消息处理程序。
- 的处理程序将调用监听器
onSingleTapConfirmed()
仅在事件的不可以仍下降(!mStillDown
) - 事件仍然下跌,因为从来没有收到ACTION_UP事件。
- The ACTION_DOWN is received and generates an internal TAP message to the GestureDetector message Handler.
- The Handler calls the listener
onSingleTapConfirmed()
only if the event is not still down (!mStillDown
) - The event is still down, because the ACTION_UP event was never received.
所以你看,这两个问题是相关的,当我发现为什么ACTION_UP不发我会解决这个问题!
So you see, the two questions are related, when I find out why ACTION_UP is not issued I will solve the problem!
修改
这样就完成了答案,现在它正在工作。
This completes the answer, now it is working.
- 到
super.onTouchEvent()的调用
在GameView回报假
,因为超级
类SurfaceView是查看,并返回假
。这就是为什么ACTION_UP不会被调用 - 在GameActivity返回
super.onTouchEvent()
呼叫真正
,这就是为什么ACTION_UP上GameActivity发出。
- The call to
super.onTouchEvent()
in GameView returnsfalse
, because thesuper
class of SurfaceView is View, and it returnsfalse
. That's why ACTION_UP is never called - The call to
super.onTouchEvent()
in GameActivity returnstrue
, that's why ACTION_UP is issued on GameActivity.
的注:如果我设置的 GameView.setclickable(真)
的然后的超。 onTouchEvent()
的返回的真正
。的这也是一个可接受的解决方案的
现在我GameView code是:
Now my GameView code is:
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
gestureDetector.onTouchEvent(event);
Log.d(TAG, "- onTouchEvent()");
return true;
}
目前LogCat中日志是:
The current LogCat logs are:
03-11 16:32:06.629: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=977.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.629: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.641: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=978.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.641: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.772: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_UP, id[0]=0, x[0]=978.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158772, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.772: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.931: D/GestureListener(5316): + onSingleTapConfirmed(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=977.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.931: D/GestureListener(5316): - onSingleTapConfirmed()
这篇关于Android的GestureDetector与内SurfaceView SimpleOnGestureListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!