Android 如何动画滚动位置?如何平滑滚动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10621439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:24:14  来源:igfitidea点击:

How to animate scroll position? How to scroll smoothly?

androidanimationscrolldrawing

提问by Suzan Cioc

I wish to move smoothly to next scroll position in my custom view (on button press or event). How to implement this? First of all I can't see scroll animation class (only alpha, rotate, scale and translate). Secondly, having animation class, I can't see iterative one (say to scroll 100 pixels rights whatever position we have) only absolute ones (i.e. to animate from one constant value to another).

我希望平滑地移动到自定义视图中的下一个滚动位置(按下按钮或事件时)。如何实施?首先,我看不到滚动动画类(只有 alpha、旋转、缩放和平移)。其次,有动画类,我看不到迭代的(比如在我们拥有的任何位置滚动 100 像素的权利)只有绝对的(即从一个常数值动画到另一个)。

回答by Ian Warwick

Assuming you are using a ScrollView, does smoothScrollTo(...) work for you?

假设您使用的是 ScrollView,那么 smoothScrollTo(...) 对您有用吗?

http://developer.android.com/reference/android/widget/ScrollView.html#smoothScrollTo%28int,%20int%29

http://developer.android.com/reference/android/widget/ScrollView.html#smoothScrollTo%28int,%20int%29

回答by anton46

Using ObjectAnimator, this is a sample for scrolling to top:

使用ObjectAnimator,这是滚动到顶部的示例:

public void scrollToTop() {
    int x = 0;
    int y = 0;

    ObjectAnimator xTranslate = ObjectAnimator.ofInt(mScrollView, "scrollX", x);
    ObjectAnimator yTranslate = ObjectAnimator.ofInt(mScrollView, "scrollY", y);

    AnimatorSet animators = new AnimatorSet();
    animators.setDuration(1000L);
    animators.playTogether(xTranslate, yTranslate);

    animators.addListener(new AnimatorListener() {

        @Override
        public void onAnimationStart(Animator arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onAnimationRepeat(Animator arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animator arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationCancel(Animator arg0) {
            // TODO Auto-generated method stub

        }
    });
    animators.start();
}

回答by numan salati

Animating scroll is done through a combination of using Scroller/OverScroller(to compute the time interpolated values of your scroll offsets), GestureDetectors(to start the scroller object) and the onComputeScrollmethod of a View (which implicitly is your animation loop).

滚动动画是通过结合使用Scroller/OverScroller(计算滚动偏移的时间插值)、GestureDetectors(启动滚动对象)和View的onComputeScroll方法(隐式是您的动画循环)来完成的。

The official android docs now have a detailed tutorial on precisely this topic. http://developer.android.com/training/gestures/scroll.html

官方 android 文档现在有一个关于这个主题的详细教程。http://developer.android.com/training/gestures/scroll.html

回答by Lawrence D'Oliveiro

See the view_cache_demosample code to see how to do animated scrolling. It works in 2D, caches complex drawing and also handles fling gestures, but you can simplify all that as necessary.

请参阅view_cache_demo示例代码以了解如何进行动画滚动。它可以在 2D 中工作,缓存复杂的绘图,还可以处理滑动手势,但您可以根据需要简化所有这些。