缓慢向上滚动 ScrollView

发布于 2024-12-01 19:16:10 字数 198 浏览 0 评论 0原文

问题是“如何非常平滑且缓慢地将 ScrollView 滚动到顶部”。

在我的特殊情况下,我需要在大约 1-2 秒内滚动到顶部。 我尝试使用处理程序手动插值(调用scrollTo(0, y)),但这根本不起作用。

我已经在一些图书阅读器应用程序上看到了这种效果,所以一定有办法,我确定:D。 (文本非常缓慢地向上滚动以继续阅读而无需触摸屏幕进行输入)。

The question is "How do i scroll up a ScrollView to top very smoothly and slowly".

In my special case i need to scroll to top in about 1-2 seconds.
Ive tried interpolating manually using a Handler (calling scrollTo(0, y)) but that didnt work at all.

I've seen this effect on some bookreader-apps yet, so there must be a way, im sure :D.
(Text is very slowly scrolling up to go on reading without touching the screen, doing input).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小傻瓜 2024-12-08 19:16:10

我使用对象动画器(API >= 3 中可用)完成了它,它看起来非常好:

定义一个对象动画器:
final ObjectAnimator animScrollToTop = ObjectAnimator.ofInt(this, "scrollY", 0);

this 指的是扩展 Android 的 ScrollView 的类)

你可以根据需要设置其持续时间:

animScrollToTop.setDuration(2000);(2 秒)

Ps 不要忘记启动动画。

I did it using object animator (Available in API >= 3) and it looks very good:

Define an ObjectAnimator:
final ObjectAnimator animScrollToTop = ObjectAnimator.ofInt(this, "scrollY", 0);

(this refers to the class extending Android's ScrollView)

you can set its duration as you wish:

animScrollToTop.setDuration(2000); (2 seconds)

P.s. Don't forget to start the animation.

温馨耳语 2024-12-08 19:16:10

2秒内将滚动视图移动到2000的位置

new CountDownTimer(2000, 20) {          

 public void onTick(long millisUntilFinished) {     
   scrollView.scrollTo(0, (int) (2000 - millisUntilFinished)); // from zero to 2000    
 }          

 public void onFinish() {  
 }      

}.start(); 

In 2 seconds move the scroll view to the possition of 2000

new CountDownTimer(2000, 20) {          

 public void onTick(long millisUntilFinished) {     
   scrollView.scrollTo(0, (int) (2000 - millisUntilFinished)); // from zero to 2000    
 }          

 public void onFinish() {  
 }      

}.start(); 
〆凄凉。 2024-12-08 19:16: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);
}

Try the following code:

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);
}
沧笙踏歌 2024-12-08 19:16:10

您尝试过smoothScrollTo(int x, int y)吗?
您无法设置速度参数,但也许这个功能适合您

Have you tried smoothScrollTo(int x, int y)?
You can't set the speed parameter but maybe this function will be ok for you

给我一枪 2024-12-08 19:16:10

您可以使用 Timer 和 TimerTask 类。你可以做类似的事情

scrollTimer = new Timer();
scrollerSchedule = new TimerTask(){
    @Override
    public void run(){
        runOnUiThread(SCROLL TO CODE GOES HERE);
    }
};
scrollTimer.schedule(scrollerSchedule, 30, 30);

You could use the Timer and TimerTask class. You could do something like

scrollTimer = new Timer();
scrollerSchedule = new TimerTask(){
    @Override
    public void run(){
        runOnUiThread(SCROLL TO CODE GOES HERE);
    }
};
scrollTimer.schedule(scrollerSchedule, 30, 30);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文