没有来自onFling的回复

时间:2012-11-18 21:03:34

标签: android gesture onfling

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

这不在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;
        }}); 

}

2 个答案:

答案 0 :(得分: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显示多个onTouchEventonDownonFling

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;会使其传播触摸事件,您应该会看到其他触摸事件方法被调用。

答案 1 :(得分:0)

你有`实现OnGestureListener'你的活动声明?

请参阅此代码段

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