减少滚动视图中平滑滚动的速度

38

我有一个ScrollView,我使用smoothScrollBy()实现了平滑滚动。虽然一切都正常工作,但我想改变平滑滚动的持续时间。 平滑滚动发生得非常快,用户不理解发生了什么。请帮助我减慢平滑滚动速度。

4个回答

188
简单的回答就是替换。
scrollView.smoothScrollTo(0, scrollTo);

随着

ObjectAnimator.ofInt(scrollView, "scrollY",  scrollTo).setDuration(duration).start();

其中duration是您希望它花费的毫秒时间。


6
这绝对是最好的答案,非常棒。 - aikmanr
1
什么是ObjectAnimator?这段代码应该在哪里调用? - Stefano Giacone
惊人的 xd <3 很好。 - silentsudo
简单而美丽 - 应该是正确的答案。 - slott
来自数小时的搜索的爱<3 - Beeeeeeer
显示剩余2条评论

14

请尝试以下代码:

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);
}

这应该被选为问题的答案。+1 - Federico Ponzi
1
为什么不使用ValueAnimator,而是使用ObjectAnimator(ValueAnimator的子类)呢?这样会更容易 - 实际上只需要一行代码:ObjectAnimator.ofInt(scrollView, "scrollY", targetScrollY).setDuration(500).start(); - Tony Wickham

4

以下是我如何实现平滑的垂直滚动(就像电影字幕一样)。这也允许用户上下移动滚动条,并在他们松开时继续滚动。在我的XML中,我将TextView封装在名为“scrollView1”的ScrollView中。享受吧!

    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);
        }
    });

1
如何知道滚动条是否达到底部以及如何关闭可运行程序?如何处理这个问题? - AlexKost

-2
whichScreen = Math.max(0, Math.min(whichScreen, getBase().getDocumentModel().getPageCount()-1));
mNextScreen = whichScreen;
final int newX = whichScreen * getWidth();
final int delta = newX - getScrollX();
//scroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2);
scroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta));
invalidate();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接