Android onDoubleTap,onScroll,onFling无法正常工作,但onDown和onLongPress有效吗?

时间:2013-05-31 15:55:24

标签: android debugging gestures onscroll

我正在尝试在自定义视图中使用onScroll方法。 onDown和onLongPress工作,但为什么我的onScroll或onFling或onDoubleTap不起作用? 这是我的代码,它几乎是Google在developer.android.com网站上提供的所有内容......

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.MotionEventCompat;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.*;

public class GameView extends View implements GestureDetector.OnGestureListener,      GestureDetector.OnDoubleTapListener{

public static int cols = 10;
public static int rows = 9;

private String DEBUG_TAG = "Debug";

private GestureDetectorCompat mDetector;
private GameThread mGameThread;
private Sprite mSprite;
private MineField mMineField;
private Context context;
private Paint mPaint;

public GameView(Context context) {
    super(context);
    this.context = context;
    if(mMineField == null)buildMineField(cols, rows);
    mPaint = new Paint();
    mSprite = new Sprite(context, this);
    mGameThread = new GameThread(this, mSprite, mMineField);
    mGameThread.start();

    mPaint.setColor(Color.WHITE);
    mPaint.setStyle(Style.FILL);
    mPaint.setTextSize(50);

    mDetector = new GestureDetectorCompat(context, this);
}

public Sprite getSprite() {
    return mSprite;
}

public MineField getMineField() {
    return mMineField;
}

public void buildMineField(int cols, int rows) {
    if(cols >= 1 && rows >= 1) {
        mMineField = new MineField(cols, rows, context, this);
    }
}

public void drawMineField(Canvas canvas) {
    for(int i = 0; i < mMineField.rows; i++) {
        for(int j = 0; j < mMineField.cols; j++) {
            canvas.drawBitmap(mMineField.mines[j][i].getBitmap(), mMineField.mines[j][i].getX(), mMineField.mines[j][i].getY(), null);
        }
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.BLACK);
    drawMineField(canvas);
    if(mSprite.isVisible()) {
        canvas.drawBitmap(mSprite.getBitmap(), mSprite.getX(), mSprite.getY(), null);
    }
    canvas.drawText(Long.toString(mGameThread.getFPS()), 50, 50, mPaint);
}

@Override
public void postInvalidate() {
    super.postInvalidate();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    this.mDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}

@Override
public boolean onDoubleTap(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    Log.d(DEBUG_TAG, "onDoubleTap: " + e.toString());
    return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onDown(MotionEvent arg0) {
    Log.d(DEBUG_TAG,"onDown: " + arg0.toString()); 
    return true;
}

@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
        float arg3) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onLongPress(MotionEvent arg0) {
    Log.d(DEBUG_TAG, "onLongPress: " + arg0.toString()); 
}

@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
        float arg3) {
    Log.d(DEBUG_TAG, "onScroll");
    return true;
}

@Override
public void onShowPress(MotionEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}



}

我刚刚更新了代码,并且onDown()方法被编辑为“return false”

@Override 
public boolean onTouchEvent(MotionEvent event){ 
    this.mDetector.onTouchEvent(event);
    // Be sure to call the superclass implementation
    return super.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent event) { 
    Log.d(DEBUG_TAG,"onDown: " + event.toString()); 
    return false;
}

@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, 
        float velocityX, float velocityY) {
    Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
    return true;
}

@Override
public void onLongPress(MotionEvent event) {
    Log.d(DEBUG_TAG, "onLongPress: " + event.toString()); 
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
    return true;
}

@Override
public void onShowPress(MotionEvent event) {
    Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
}

@Override
public boolean onSingleTapUp(MotionEvent event) {
    Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
    return true;
}

@Override
public boolean onDoubleTap(MotionEvent event) {
    Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
    return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent event) {
    Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());
    return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
    Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
    return true;
}

但是,当触摸屏幕时,我得到一个“OnDown”调试通知以及“OnShowPress”和“OnLongPress”...... 但我把回报设定为假?

3 个答案:

答案 0 :(得分:7)

我有一些类似的问题,在xamarin(使用c#而不是java)中为android开发类似的东西。

我解决了它在onDown(...)等事件中返回false,但是在onTouchEvent()中返回true,所以尝试这样的方法来检查是否有效:

@Override
public boolean onTouchEvent(MotionEvent event) {
    this.mDetector.onTouchEvent(event);
    return true;
}

顺便说一下,你正在实现OnGestureListener,所以我认为也许不应该覆盖像onDown()这样的方法。然而,onTouchEven()需要被覆盖。

// without the overriden directive
public boolean onDown(MotionEvent event) { 
    Log.d(DEBUG_TAG,"onDown: " + event.toString()); 
    return false;
}

希望这有助于您找到解决方案。

答案 1 :(得分:2)

当然......所以你的问题很“简单”

@Override
public boolean onDown(MotionEvent arg0) {
    Log.d(DEBUG_TAG,"onDown: " + arg0.toString()); 
    return false;
}

如果你返回假...你会告诉听众:“我没有消耗这个事件”并让它“冒泡”

答案 2 :(得分:0)

true内回复onTouchEvent也适用于我。 但它感觉不对,因为它会拦截所有“点击”,即使你不打算这样做。

我首先通过类似的测试解决了这个问题:

if (event.getAction()==MotionEvent.ACTION_SCROLL)

onTouchEvent内,但它不会一直有效。

我认为我的理解力不够好。