Gallery手指滑动的时候,幅度过大的话会一次滑到最后一张图片 通过重写onFling方法捕捉X、Y坐标 使其不过头
代码如下:
/**
* 重写Gallery,捕捉xy坐标,使其不会跑过头
* **/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
int kEvent;
if(isScrollingLeft(e1, e2)){
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
}
else{
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
return onKeyDown(kEvent, null);
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2)
{
return e2.getX() > e1.getX();
}
或者降低滑动的速度 代码如下:
/**
* 重写onFling方法,将滑动的速率降低
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return super.onFling(e1, e2, velocityX/3, velocityY);}