2011-12-27 484 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