Scroll up a ScrollView slowly

前端 未结 5 573
小蘑菇
小蘑菇 2020-12-09 18:31

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 inter

相关标签:
5条回答
  • 2020-12-09 18:35

    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(); 
    
    0 讨论(0)
  • 2020-12-09 18:36

    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.

    0 讨论(0)
  • 2020-12-09 18:52

    You could use the Timer and TimerTask class. You could do something like

    scrollTimer = new Timer();
    scrollerSchedule = new TimerTask(){
        @Override
        public void run(){
            runOnUiThread(SCROLL TO CODE GOES HERE);
        }
    };
    scrollTimer.schedule(scrollerSchedule, 30, 30);
    
    0 讨论(0)
  • 2020-12-09 18:54

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

    0 讨论(0)
  • 2020-12-09 18:56

    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);
    }
    
    0 讨论(0)
提交回复
热议问题