2011-12-27 50 views
21

Tôi có Chế độ xem cuộn. Tôi thực hiện trơn tru-scroll.using smoothScrollBy().Nó tất cả các công trình tốt, nhưng tôi muốn thay đổi thời gian của mịn-scroll. Smooth-scroll xảy ra rất nhanh và người dùng không hiểu chuyện gì đã xảy ra. Hãy giúp tôi giảm tốc độ cuộn mượt mà?Giảm tốc độ cuộn trơn tru trong chế độ xem cuộn

Trả lời

-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

Hãy thử đoạn mã sau:

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

này nên được chọn là câu trả lời cho câu hỏi. +1 –

+0

Thay vì sử dụng ValueAnimator, tại sao không sử dụng ObjectAnimator (một lớp con của ValueAnimator)? Nó làm cho nó dễ dàng hơn - một lớp lót, trên thực tế: ObjectAnimator.ofInt (scrollView, "scrollY", targetScrollY) .setDuration (500) .start(); –

2

Đây là cách tôi đã đạt được một cuộn dọc mịn (như tín dụng phim). Điều này cũng cho phép người dùng di chuyển cuộn lên và xuống và cho phép người dùng tiếp tục cuộn khi họ thả. Trong XML của tôi, tôi đóng gói TextView bên trong một ScrollView được gọi là "scrollView1". Thưởng thức!

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

Làm cách nào để biết rằng cuộn xuống dưới cùng và có thể tắt runnable không? Làm thế nào để xử lý này? – AlexKost

111

Câu trả lời đơn giản chỉ là để thay thế

scrollView.smoothScrollTo(0, scrollTo); 

với

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

nơi thời gian là thời gian trong mili giây bạn muốn nó mất

+3

Đây chắc chắn là câu trả lời hay nhất, rất hay. – aikmanr

+0

giải pháp duyên dáng, thú! –

+0

Giải pháp tuyệt vời! – Simas

Các vấn đề liên quan