Home » Android » android – Reduce speed of smooth scroll in scroll view

android – Reduce speed of smooth scroll in scroll view

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

Here is a solution with a timer which uses scrollTo but you can also use scrollBy
Android: HorizontalScrollView smoothScroll animation time

Answer:

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.

Answer:

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);
}

Answer:

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);
        }
    });

Answer:

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();