gpt4 book ai didi

Android onDoubleTap、onScroll、onFling 不工作,但 onDown 和 onLongPress 工作?

转载 作者:行者123 更新时间:2023-11-29 14:27:55 25 4
gpt4 key购买 nike

我正在尝试在我的自定义 View 中使用 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 中为 android 开发类似的东西(使用 c# 而不是 java)。

我解决了它在像 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;
}

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

关于Android onDoubleTap、onScroll、onFling 不工作,但 onDown 和 onLongPress 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16861320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com