慢慢向上滚动ScrollView

poi*_*oae 11 android scroll scrollview

问题是"如何将ScrollView向上滚动到非常平滑和缓慢".

在我的特殊情况下,我需要在大约1-2秒内滚动到顶部.我已经尝试使用Handler手动插值(调用scrollTo(0,y)),但这根本不起作用.

我已经在一些书籍阅读器应用程序上看到了这种效果,所以必须有一种方法,我确定:D.(文本非常慢地向上滚动以继续阅读而不触摸屏幕,进行输入).

Dan*_* L. 30

我使用对象动画师(在API> = 3中可用)并且它看起来非常好:

定义ObjectAnimator: final ObjectAnimator animScrollToTop = ObjectAnimator.ofInt(this, "scrollY", 0);

(this指扩展Android的类ScrollView)

您可以根据需要设置持续时间:

animScrollToTop.setDuration(2000); (2秒)

Ps别忘了开始动画.


Lum*_*mis 12

在2秒内将滚动视图移动到2000的位置

new CountDownTimer(2000, 20) {          

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

 public void onFinish() {  
 }      

}.start(); 
Run Code Online (Sandbox Code Playgroud)


Muz*_*ant 11

请尝试以下代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 它很流畅,与我目前正在尝试做的事情有不同的问题,但这绝对可以实现平滑滚动 (2认同)

Vin*_*eFR 5

你试过smoothScrollTo(int x, int y)吗?您无法设置速度参数,但也许此功能可以使用