Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect GridView scrolling speed - Android

Tags:

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 GestureDetector.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.

like image 731
sttwister Avatar asked Jul 22 '10 09:07

sttwister


1 Answers

Well, I hope you got your answer by now, but I will still post one, for future use...

You will need to override OnScrollListenerand 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;
     }
 }
like image 181
Rotemmiz Avatar answered Oct 23 '22 09:10

Rotemmiz