慢慢向上滚动ScrollView

时间:2011-08-26 08:49:05

标签: android scroll scrollview

问题是“如何将ScrollView向上滚动到非常平滑和缓慢的顶部”。

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

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

5 个答案:

答案 0 :(得分:28)

我是使用object animator(在API> = 3中可用)完成的,它看起来非常好:

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

this是指扩展Android的ScrollView

的类

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

animScrollToTop.setDuration(2000);(2秒)

P.S。别忘了开始动画。

答案 1 :(得分: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(); 

答案 2 :(得分: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);
}

答案 3 :(得分:5)

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

答案 4 :(得分:0)

您可以使用Timer和TimerTask类。你可以做点什么

scrollTimer = new Timer();
scrollerSchedule = new TimerTask(){
    @Override
    public void run(){
        runOnUiThread(SCROLL TO CODE GOES HERE);
    }
};
scrollTimer.schedule(scrollerSchedule, 30, 30);