如何检测 ImageView 上的手势?
我想检测手指被拖动到 ImageView 上。我获取 ImageView,创建 GestureDetector 和 View.OnTouchListener 的实例,然后将 View.OnTouchListener 设置为 ImageView。
OnTouchListener 检测所有触摸(“发生触摸”),并将它们传递给 GestureDetector,但它从不调用其 onScroll 或 onFling 方法。我做错了什么?下面你可以看到相关来源:
myImageView = (ImageView) findViewById(R.id.imageView1);
myGestureDetector = new GestureDetector(this,
new GestureDetector.OnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
Log.d("gestures", "scroll");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
// TODO Auto-generated method stub
Log.d("gestures", "fling");
return true;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
});
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (myGestureDetector.onTouchEvent(event)) {
Log.d("gestures", "here");
return true;
}
Log.d("gestures", "touch occured");
return false;
}
};
myImageView.setOnTouchListener(gestureListener);
I'd like to detect finger being dragged over ImageView. I get my ImageView, create instances of GestureDetector and View.OnTouchListener, then set View.OnTouchListener to ImageView.
OnTouchListener detects all touches ("touch occured") and I pass them to GestureDetector, but it never calls its methods onScroll or onFling. What I am doing wrong? Bellow you can see related source:
myImageView = (ImageView) findViewById(R.id.imageView1);
myGestureDetector = new GestureDetector(this,
new GestureDetector.OnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
Log.d("gestures", "scroll");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
// TODO Auto-generated method stub
Log.d("gestures", "fling");
return true;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
});
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (myGestureDetector.onTouchEvent(event)) {
Log.d("gestures", "here");
return true;
}
Log.d("gestures", "touch occured");
return false;
}
};
myImageView.setOnTouchListener(gestureListener);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
OnGestureListener 的 onDown 方法必须始终返回 true,否则检测器不会对以下事件做出反应,从而不会调用 onScroll。
onDown method of OnGestureListener must always return true otherwise the detector won't react to the following events as a result onScroll won't be invoked.
我认为您还需要重写 onTouchEvent 方法。我在 imageview 上检测到了 onFling() 手势,如下所示。
在我这边工作的代码是:
I think you will also need to override the onTouchEvent method. I have detected onFling() gesture on imageview like this.
The code which is working at my end is:
尝试通过扩展 SimpleOnGestureListener 来使用自定义手势侦听器类。另外,通过这样做,您也将能够覆盖上述所有方法。
Try using a customised gesture listener class by extending SimpleOnGestureListener. Also by doing so, you will be able to override all the above methods as well.
首先解决您在使用@Override时遇到的错误。
这主要是因为Java编译器配置较低[应用程序属性->Java编译器->编译器合规级别]确保它是1.6。
解决此问题后,请使用 @Override 符号重试。
First resolve the error which you are facing when you have used @Override.
This is mostly because of the lower Java compiler Configurations[application properties->Java Compiler->Compiler compliance level] Make sure it is 1.6.
Once this is solved, try again with @Override notations.