我想检测手指被拖过 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);