我整晚都在盯着这个看。我试图为安卓制作一个简单的互动球类游戏,但我似乎无法让onFling方法发挥作用。我在onFling中的日志没有出现在LogCat中,也没有对抛出手势的响应。我想我对妊娠探测器或妊娠器做错了什么。请帮帮我,这样我终于可以睡觉了:)这是我的密码:
这在onCreate之外,但仍在活动中:
@Override
public boolean onTouchEvent(MotionEvent me) {
return gestureDetector.onTouchEvent(me);
}
SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
ballSpeed.x = velocityX;
ballSpeed.y = velocityY;
Log.d("TAG", "----> velocityX: " + velocityX);
Log.d("TAG", "----> velocityY: " + velocityY);
return true;
}
};
GestureDetector gestureDetector = new GestureDetector(null,
simpleOnGestureListener);编辑:
显然,上面的代码是有效的。这意味着错误在别的地方。@bobnoble说我应该发布我的onCreate()方法:
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE); // hide title bar
getWindow().setFlags(0xFFFFFFFF,LayoutParams.FLAG_FULLSCREEN| LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create pointer to main screen
final FrameLayout mainView = (android.widget.FrameLayout) findViewById(R.id.main_view);
// get screen dimensions
Display display = getWindowManager().getDefaultDisplay();
try {
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
} catch (NoSuchMethodError e) {
screenWidth = display.getWidth();
screenHeight = display.getHeight();
}
ballPosition = new android.graphics.PointF();
ballSpeed = new android.graphics.PointF();
// create variables for ball position and speed
ballPosition.x = 4 * screenWidth / 5;
ballPosition.y = 2 * screenHeight / 3;
ballSpeed.x = 0;
ballSpeed.y = 0;
// create initial ball
ballView = new BallView(this, ballPosition.x, ballPosition.y, 20);
mainView.addView(ballView); // add ball to main screen
ballView.invalidate(); // call onDraw in BallView
//listener for touch event
mainView.setOnTouchListener(new android.view.View.OnTouchListener() {
public boolean onTouch(android.view.View v, android.view.MotionEvent e) {
//set ball position based on screen touch
ballPosition.x = e.getX();
ballPosition.y = e.getY();
ballSpeed.y = 0;
ballSpeed.x = 0;
//timer event will redraw ball
return true;
}});
}发布于 2012-11-18 21:57:14
我将您提供的代码放入Activity中,注释掉ballSpeed行并添加了Log输出。按预期工作。显示输出的logcat在末尾。
如果这对您不起作用,那么问题就在您的Activity代码中的其他地方。您可能希望发布您的onCreate方法。
@Override
public boolean onTouchEvent(MotionEvent me) {
Log.i(TAG, "onTouchEvent occurred...");
return gestureDetector.onTouchEvent(me);
}
SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
Log.i(TAG, "onDown event occurred...");
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// ballSpeed.x = velocityX;
// ballSpeed.y = velocityY;
Log.d("TAG", "----> velocityX: " + velocityX);
Log.d("TAG", "----> velocityY: " + velocityY);
return true;
}
};
GestureDetector gestureDetector = new GestureDetector(null,
simpleOnGestureListener);Logcat显示多个onTouchEvents、onDown和onFling
11-18 15:51:25.364: I/SO_MainActivity(13415): onTouchEvent occurred...
11-18 15:51:25.364: I/SO_MainActivity(13415): onDown event occurred...
...
11-18 15:51:25.584: I/SO_MainActivity(13415): onTouchEvent occurred...
11-18 15:51:25.604: I/SO_MainActivity(13415): onTouchEvent occurred...
11-18 15:51:25.664: D/TAG(13415): ----> velocityX: 3198.8127
11-18 15:51:25.664: D/TAG(13415): ----> velocityY: -1447.966基于查看onCreate方法的编辑:
mainView.setOnTouchListener onTouch方法通过返回true来消耗触点。通过返回true,onTouch方法指示它已经完全处理了触摸事件,而不是进一步传播它。
将其更改为return false;将导致它传播触摸事件,您应该会看到其他的触摸事件方法正在被调用。
发布于 2012-11-18 21:21:34
您的活动声明中是否有“实现OnGestureListener”?
看这个片段
http://www.androidsnippets.com/gesturedetector-and-gesturedetectorongesturelistener
https://stackoverflow.com/questions/13444551
复制相似问题