Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll up a ScrollView slowly

The question is "How do i scroll up a ScrollView to top very smoothly and slowly".

In my special case i need to scroll to top in about 1-2 seconds. Ive tried interpolating manually using a Handler (calling scrollTo(0, y)) but that didnt work at all.

I've seen this effect on some bookreader-apps yet, so there must be a way, im sure :D. (Text is very slowly scrolling up to go on reading without touching the screen, doing input).

like image 345
poitroae Avatar asked Aug 26 '11 08:08

poitroae


4 Answers

I did it using object animator (Available in API >= 3) and it looks very good:

Define an ObjectAnimator: final ObjectAnimator animScrollToTop = ObjectAnimator.ofInt(this, "scrollY", 0);

(this refers to the class extending Android's ScrollView)

you can set its duration as you wish:

animScrollToTop.setDuration(2000); (2 seconds)

P.s. Don't forget to start the animation.

like image 193
Daniel L. Avatar answered Nov 12 '22 04:11

Daniel L.


In 2 seconds move the scroll view to the possition of 2000

new CountDownTimer(2000, 20) {          

 public void onTick(long millisUntilFinished) {     
   scrollView.scrollTo(0, (int) (2000 - millisUntilFinished)); // from zero to 2000    
 }          

 public void onFinish() {  
 }      

}.start(); 
like image 43
Lumis Avatar answered Nov 12 '22 05:11

Lumis


Have you tried smoothScrollTo(int x, int y)? You can't set the speed parameter but maybe this function will be ok for you

like image 6
VinceFR Avatar answered Nov 12 '22 05:11

VinceFR


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);
}
like image 13
Muzikant Avatar answered Nov 12 '22 04:11

Muzikant