我有一个ScrollView,我使用smoothScrollBy()实现了平滑滚动。虽然一切都正常工作,但我想改变平滑滚动的持续时间。 平滑滚动发生得非常快,用户不理解发生了什么。请帮助我减慢平滑滚动速度。
我有一个ScrollView,我使用smoothScrollBy()实现了平滑滚动。虽然一切都正常工作,但我想改变平滑滚动的持续时间。 平滑滚动发生得非常快,用户不理解发生了什么。请帮助我减慢平滑滚动速度。
scrollView.smoothScrollTo(0, scrollTo);
随着
ObjectAnimator.ofInt(scrollView, "scrollY", scrollTo).setDuration(duration).start();
其中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封装在名为“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);
}
});
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();