2011-12-27 244 views
21

我有滚动视图。我执行smooth-scroll.using smoothScrollBy()。它一切正常,但我想更改平滑滚动的持续时间。平滑滚动发生得非常快,用户不知道发生了什么。请帮助我降低平滑滚动速度?降低滚动视图中的平滑滚动的速度

回答

-1
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(); 
10

试试下面的代码:

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

这应该被选为答案这个问题。 +1 –

+0

而不是使用ValueAnimator,为什么不使用ObjectAnimator(ValueAnimator的子类)?它使得它更容易 - 实际上是一个单线程,ObjectAnimator.ofInt(scrollView,“scrollY”,targetScrollY).setDuration(500).start(); –

2

这是我如何实现平稳垂直滚动(如电影信用)。这也允许用户上下移动滚动并允许它们在放开时继续滚动。在我的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); 
     } 
    }); 
+0

我将如何知道滚动到底并且runnable必须关闭?如何处理这个? – AlexKost

111

答案很简单,只是为了

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

其中所述持续时间是你希望它采取以毫秒为单位的时间更换

scrollView.smoothScrollTo(0, scrollTo); 

+3

这绝对是最好的答案,非常好。 – aikmanr

+0

优雅的解决方案,野兽! –

+0

美丽的解决方案! – Simas