1

我正在尝试在我的自定义视图中使用 onScroll 方法。onDown 和 onLongPress 工作,但为什么我的 onScroll 或 onFling 或 onDoubleTap 不能工作?这是我的代码,它几乎是谷歌在他们的 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”......但我已将返回设置为 false?

4

3 回答 3

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

希望这可以帮助您找到解决方案。

于 2013-06-23T22:13:44.153 回答
2

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

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

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

于 2013-05-31T16:10:34.587 回答
0

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

我首先通过以下测试解决了这个问题:

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

在里面onTouchEvent,但它并不总是工作。

我想我的理解还不够好。

于 2013-11-16T18:52:15.463 回答