关于Android触摸事件中onShowPress和onLongPress事件无法识别的问题 15
publicclassGameViewextendsSurfaceViewimplementsSurfaceHolder.Callback,OnGestureListen...
public class GameView extends SurfaceView implements SurfaceHolder.Callback,
OnGestureListener, OnTouchListener {
private GestureDetector myGD;// 手势识别
private static final int FLING_MIN_VELOCITY = 0;// 最慢移动速度
private float FLING_MIN_DISTANCE = 50;// 最短触摸路径
……
public GameView(GameActivity father) {
……
this.setClickable(true);
this.setLongClickable(true);
this.setOnTouchListener(this);
myGD = new GestureDetector(this);
myGD.setIsLongpressEnabled(true);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return myGD.onTouchEvent(event);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.i("gesture", "onFling");
return false;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.i("gesture", "onSingleTapUp");
return false;
}
@Override
public boolean onDown(MotionEvent event) {
Log.i("gesture", "onDown");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.i("gesture", "onScroll");
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Log.i("gesture", "onShowPress");
}
@Override
public void onLongPress(MotionEvent event) {
Log.i("gesture", "onLongPress");
}
}
代码如上,不管怎么按,都是按下执行onDown(),抬起手后执行onSingleTapUp(),或者onDown()->onScroll(),或者onDown()->onScroll()->onFling()。不会执行onShowPress()或者onLongPress()。
如何才能让它区别并执行onSingleTapUp(),onFling(),onLongPress()这三种手势呢? 展开
OnGestureListener, OnTouchListener {
private GestureDetector myGD;// 手势识别
private static final int FLING_MIN_VELOCITY = 0;// 最慢移动速度
private float FLING_MIN_DISTANCE = 50;// 最短触摸路径
……
public GameView(GameActivity father) {
……
this.setClickable(true);
this.setLongClickable(true);
this.setOnTouchListener(this);
myGD = new GestureDetector(this);
myGD.setIsLongpressEnabled(true);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return myGD.onTouchEvent(event);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.i("gesture", "onFling");
return false;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.i("gesture", "onSingleTapUp");
return false;
}
@Override
public boolean onDown(MotionEvent event) {
Log.i("gesture", "onDown");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.i("gesture", "onScroll");
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Log.i("gesture", "onShowPress");
}
@Override
public void onLongPress(MotionEvent event) {
Log.i("gesture", "onLongPress");
}
}
代码如上,不管怎么按,都是按下执行onDown(),抬起手后执行onSingleTapUp(),或者onDown()->onScroll(),或者onDown()->onScroll()->onFling()。不会执行onShowPress()或者onLongPress()。
如何才能让它区别并执行onSingleTapUp(),onFling(),onLongPress()这三种手势呢? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询