3

My app scrolling is super fast! How can I limit the scroll speed of a scroll view in my android app? The scroll can be very fast and it's meaningless to scroll in that speed.

Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141

4 Answers4

12

This thread is old, but I will reply with a partial solution: limiting the fling velocity. Feel free to comment so I can improve my solution.

As explained in the Developer Training guide:

Flinging is the type of scrolling that occurs when a user drags and lifts her finger quickly.

That's where I needed a velocity limit. So, in the Custom ScrollView (whether horizontal or vertical) override fling method like this.

@Override
public void fling(int velocityY) {
    int topVelocityY = (int) ((Math.min(Math.abs(velocityY), MAX_SCROLL_SPEED) ) * Math.signum(velocityY));
    super.fling(topVelocityY);
}

I found that velocityY (in horizontal scrollview, it would be velocityX) could be between -16000 and 16000. Negative just means scrolling back. I'm still testing this values, and I have tested it in only one device. Not sure if it's the same in older devices/API versions. I will come back later to edit this.

(int) ((Math.min(Math.abs(velocityY), MAX_SCROLL_SPEED) ) * Math.signum(velocityY));

What I'm doing there is obtaining the minimum value between my constant MAX_SCROLL_SPEED and original velocityY, then obtaining the sign of the original velocityY. We need the sign to scroll back.

Finally, sending back the modified velocityY.

It's a partial solution, because if the user keeps pressing the scrollview, the speed won't change.

Again, feel free to improve my answer, I'm still learning.

kinich49
  • 486
  • 4
  • 8
3
 ObjectAnimator anim = ObjectAnimator.ofInt(mScrollView, "scrollY", mScrollView.getBottom());                               
 anim.setDuration(9000);                     
 anim.start();
Dev Gurung
  • 1,290
  • 14
  • 16
0

I think using timer you can limit the speed of scroll. look at this link Android: HorizontalScrollView smoothScroll animation time

Community
  • 1
  • 1
Sunny
  • 14,522
  • 15
  • 84
  • 129
0

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);
        }
    });
  • Can you please tell me how to stop runnable when scrolling reached the bottom? Please see the [link](http://stackoverflow.com/questions/33281780/how-to-stop-auto-scroll-when-it-reached-the-bottom). – AlexKost Oct 24 '15 at 12:17