37

我有一个滚动视图。我使用 smoothScrollBy() 执行了平滑滚动。一切正常,但我想更改平滑滚动的持续时间。平滑滚动发生得非常快,用户不明白发生了什么。请帮我降低平滑滚动速度?

4

4 回答 4

185

简单的答案就是替换

scrollView.smoothScrollTo(0, scrollTo);

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

duration您希望花费的时间(以毫秒为单位)在哪里。

于 2015-10-08T10:55:42.383 回答
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);
}
于 2014-11-19T22:15:04.093 回答
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);
        }
    });
于 2015-02-17T21:54:34.860 回答
-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();
于 2012-01-06T10:47:22.610 回答