Detect GridView scrolling speed - Android
I have a custom view subclassed from GridView that I use in order to display some custom 3D animation/effect. The way I do this is by overriding dispatchDraw()
.
Ideally, I'd want to know the current speed of the scrolling when doing the draw. Currently, I use Gesture开发者_运维问答Detector.OnGestureListener
and capture onScroll
events and this works very well, except that it doesn't also detect flings as scrolling events.
One idea that comes to mind would be to capture onFling
events and then do future processing on my own in order to detect the speed at a later time.
Is there any better way to achieve this? Any simple way to query the current scrolling speed of a GridView?
Thanks.
Well, I hope you got your answer by now, but I will still post one, for future use...
You will need to override OnScrollListener
and calculate the speed for yourself.
From Kinematics: Distance/Time = Speed
private class SpeedDetectorOnScrollListener implements OnScrollListener {
private long timeStamp;
private int prevFirstVisibleItem;
private int scrollingSpeed;
public SpeedDetectorOnScrollListener () {
timeStamp = System.currentTimeMillis();
lastFirstVisibleItem = 0;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
long lastTime = System.currentTimeMillis();
timeStamp = lastTime;
lastFirstVisibleItem = firstVisibleItem;
scrollingSpeed = (firstVisibleItem - lastFirstVisibleItem)/(lastTime-timeStamp)
}
public int getSpeed()
{
return scrollingSpeed;
}
}
精彩评论