如何确定Android图库中的运动方向?

时间:2011-11-15 14:58:12

标签: android gallery onfling onscroll

我一直在使用有限数量的Gallery创建View衍生产品,因此,Adapter需要能够填充这些View个在滚动或投掷期间提前。为此,我需要从onFling(...)onScroll(...)事件中获取动作方向。

如何使用distanceX中的onScroll(...)参数和velocityX中的onFling(...)参数来确定Gallery的旅行方式,以及View准备下一个?

1 个答案:

答案 0 :(得分:3)

onFling(...)中的速度参数和onScroll(...)上的距离参数的符号相反。为了正确确定Gallery正在移动的方向,代码应如下所示:

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    //if distanceX is POSITIVE, the Views are travelling left
    //therefore the selection position is INCREASING                    
    return super.onScroll(e1, e2, distanceX*mVelocityFactor, distanceY*mVelocityFactor);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    //if velocityX is NEGATIVE, the Views are travelling left
    //therefore the selection position is DECREASING                    
    return super.onFling(e1, e1, velocityX*mVelocityFactor, velocityY*mVelocityFactor);
}

顺便说一下,mVelocityFactor只是我引入的一个常量,使滚动/投掷不那么精力充沛。我发现0.6是一个相当不错的价值 - 它仍然感觉直观滚动但是飘动不那么暴力。