How to animate scroll position? How to scroll smoo

2019-02-05 20:43发布

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).

4条回答
Fickle 薄情
2楼-- · 2019-02-05 21:17

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 onComputeScroll method of a View (which implicitly is your animation loop).

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

查看更多
时光不老,我们不散
3楼-- · 2019-02-05 21:20

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

public void scroolToTop() {
            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();
}
查看更多
劫难
4楼-- · 2019-02-05 21:28

See the view_cache_demo sample 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.

查看更多
We Are One
5楼-- · 2019-02-05 21:32

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

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

查看更多
登录 后发表回答