0

我现在整夜都在盯着这个。我正在尝试为 Android 制作一个简单的交互式球类游戏,但我似乎无法让 onFling 方法正常工作。我在 onFling 中的日志没有出现在 LogCat 中,并且对投掷手势没有响应。我认为我在使用手势检测器或手势监听器时做错了。请帮帮我,这样我终于可以睡觉了:)。这是我的代码:

这在 onCreate 之外,但仍在 Activity 中:

@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;
        }}); 

}
4

2 回答 2

1

我将您提供的代码放入 中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, theonDown和 theonFling

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-18T21:57:14.077 回答
0

您的 Activity 声明中是否有“implements OnGestureListener”?

看到这个片段

http://www.androidsnippets.com/gesturedetector-and-gesturedetectorongesturelistener

于 2012-11-18T21:21:34.287 回答