我正在使用 GestureDetector.SimpleOnGestureListener 的 onScroll 方法在 Canvas 上滚动大位图。当滚动结束时,我想重新绘制位图,以防用户想要进一步滚动......离开位图的边缘,但我看不到如何检测滚动何时结束(用户抬起手指从屏幕上)。
e2.getAction() 似乎总是返回值 2 所以这没有帮助。 e2.getPressure 似乎返回相当恒定的值(大约 0.25),直到最后一次 onScroll 调用,此时压力似乎下降到大约 0.13。我想我可以检测到这种压力降低,但这远非万无一失。
一定有更好的方法:有人可以帮忙吗?
最佳答案
这是我解决问题的方法。希望这会有所帮助。
// declare class member variables
private GestureDetector mGestureDetector;
private OnTouchListener mGestureListener;
private boolean mIsScrolling = false;
public void initGestureDetection() {
// Gesture detection
mGestureDetector = new GestureDetector(new SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
handleDoubleTap(e);
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
handleSingleTap(e);
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// i'm only scrolling along the X axis
mIsScrolling = true;
handleScroll(Math.round((e2.getX() - e1.getX())));
return true;
}
@Override
/**
* Don't know why but we need to intercept this guy and return true so that the other gestures are handled.
* https://code.google.com/p/android/issues/detail?id=8233
*/
public boolean onDown(MotionEvent e) {
Log.d("GestureDetector --> onDown");
return true;
}
});
mGestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
}
if(event.getAction() == MotionEvent.ACTION_UP) {
if(mIsScrolling ) {
Log.d("OnTouchListener --> onTouch ACTION_UP");
mIsScrolling = false;
handleScrollFinished();
};
}
return false;
}
};
// attach the OnTouchListener to the image view
mImageView.setOnTouchListener(mGestureListener);
}
关于Android:如何检测滚动何时结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2089552/