我正在尝试在自定义视图中使用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” ...
但是我将return设置为false吗?
最佳答案
我有一些类似的问题,在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;
}
希望这可以帮助您找到解决方案。