У меня есть просмотр прокрутки. Я выполнил smooth-scroll.using smoothScrollBy (). Все работает нормально, но я хочу изменить продолжительность сглаживания. Плавная прокрутка происходит очень быстро, и пользователь не понимает, что произошло. Помогите мне уменьшить скорость плавной прокрутки?
Простой ответ просто заменить
scrollView.smoothScrollTo(0, scrollTo);
с
ObjectAnimator.ofInt(scrollView, "scrollY", scrollTo).setDuration(duration).start();
где duration - время в миллисекундах, которое вы хотите, чтобы оно прошло.
duration
Попробуйте следующий код:
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); }
Так я добился плавной вертикальной прокрутки (как в фильмах).Это также позволяет пользователю перемещать прокрутку вверх и вниз и позволяет ему продолжать прокрутку, когда он отпускает.В моем XML я инкапсулировал свой TextView внутри ScrollView с именем «scrollView1».Наслаждайтесь!
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); } });
Вот решение с таймером, которое использует scrollTo, но вы также можете использовать scrollBy Android: HorizontalScrollView smoothSroll, время анимации, время
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();