38

I have a scroll View. I performed smooth-scroll.using smoothScrollBy().It all works fine, but I want to change the duration of the smooth-scroll. Smooth-scroll happens very fast and user is not understanding what happened.Please help me reduce smooth-scroll speed?

ShineDown
  • 1,881
  • 3
  • 21
  • 29

4 Answers4

188

The simple answer is just to replace

scrollView.smoothScrollTo(0, scrollTo);

with

ObjectAnimator.ofInt(scrollView, "scrollY",  scrollTo).setDuration(duration).start();

where duration is the time in milliseconds you want it to take.

user1506104
  • 6,554
  • 4
  • 71
  • 89
365SplendidSuns
  • 3,175
  • 1
  • 21
  • 28
14

Try the following code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
    ValueAnimator realSmoothScrollAnimation = 
        ValueAnimator.ofInt(parentScrollView.getScrollY(), targetScrollY);
    realSmoothScrollAnimation.setDuration(500);
    realSmoothScrollAnimation.addUpdateListener(new AnimatorUpdateListener()
    {
        @Override
        public void onAnimationUpdate(ValueAnimator animation)
        {
            int scrollTo = (Integer) animation.getAnimatedValue();
            parentScrollView.scrollTo(0, scrollTo);
        }
    });

    realSmoothScrollAnimation.start();
}
else
{
    parentScrollView.smoothScrollTo(0, targetScrollY);
}
Muzikant
  • 8,070
  • 5
  • 54
  • 88
  • This should be selected as answer to the question. +1 – Federico Ponzi Mar 19 '15 at 09:38
  • 1
    Instead of using ValueAnimator, why not use ObjectAnimator (a subclass of ValueAnimator)? It makes it even easier - a one liner, in fact: ObjectAnimator.ofInt(scrollView, "scrollY", targetScrollY).setDuration(500).start(); – Tony Wickham Jun 13 '15 at 03:01
4

This is how I achieved a smooth vertical scroll (like movie credits). This also allows the user to move the scroll up and down and allow it to continue scrolling when they let go. In my XML, I encapsulated my TextView inside of a ScrollView called "scrollView1". Enjoy!

    final TextView tv=(TextView)findViewById(R.id.lyrics);
    final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);
    Button start = (Button) findViewById(R.id.button_start);
    Button stop = (Button) findViewById(R.id.button_stop);
    final Handler timerHandler = new Handler();
    final Runnable timerRunnable = new Runnable() {
        @Override
        public void run() {
            scrollView.smoothScrollBy(0,5);         // 5 is how many pixels you want it to scroll vertically by
            timerHandler.postDelayed(this, 10);     // 10 is how many milliseconds you want this thread to run
        }
    };

    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           timerHandler.postDelayed(timerRunnable, 0);
        }
    });

    stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            timerHandler.removeCallbacks(timerRunnable);
        }
    });
  • 1
    How will I know that scroll reached the bottom and the runnable has to be turned off? How to handle this? – AlexKost Oct 21 '15 at 12:56
-2
whichScreen = Math.max(0, Math.min(whichScreen, getBase().getDocumentModel().getPageCount()-1));
mNextScreen = whichScreen;
final int newX = whichScreen * getWidth();
final int delta = newX - getScrollX();
//scroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2);
scroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta));
invalidate();
stema
  • 90,351
  • 20
  • 107
  • 135
Rajesh Patil
  • 73
  • 1
  • 11