SoFunction
Updated on 2025-03-11

Android custom scrollview achieves rebound effect

I often see the page sliding up and down rebound effect on ios mobile phones. There is no native control support in Android. Here I will customize a scrollview to achieve rebound effect.

1. Create a new MyScrollView and inherit ScrollView. You can intercept and handle sliding events through the event distribution mechanism.

2. Rewrite the event distribution intercept event onInterceptTouchEvent method to calculate whether the event is required

//Intercept: implement parent view intercept child view//Whether the interception is successful depends on the return value of the method.  Return value true: Intercept successfully.  On the contrary, the interception failedprivate int lastY;//The coordinate position of the last y-axis operation    private Rect normal = new Rect();// Used to record the left, upper, right, and lower critical state    private boolean isFinishAnimation = true;//Whether the animation ends    private int lastX, downX, downY;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean isIntercept = false;
        int eventX = (int) ();
        int eventY = (int) ();
        switch (()) {
            case MotionEvent.ACTION_DOWN:
                lastX = downX = eventX;
                lastY = downY = eventY;
                break;
            case MotionEvent.ACTION_MOVE:
                //Get the horizontal and vertical movement distance                int absX = (eventX - downX);
                int absY = (eventY - downY);
                if(absY > absX && absY >= dp2px(10)){
                    isIntercept = true;//Execute interception                }
                lastX = eventX;
                lastY = eventY;
                break;
        }
        return isIntercept;
    }

3. Get the subview of scrollview for easy operation

//Get subview    @Override
    protected void onFinishInflate() {
        ();
        if (getChildCount() > 0) {
            childView = getChildAt(0);
        }
    }

4. Calculate whether flat-moving drawing is required

private boolean isNeedMove() {
        int childMeasuredHeight = ();//Get the height of the subview        int scrollViewMeasuredHeight = ();//Get the height of the layout        ("TAG", "childMeasuredHeight = " + childMeasuredHeight);
        ("TAG", "scrollViewMeasuredHeight = " + scrollViewMeasuredHeight);
        int dy = childMeasuredHeight - scrollViewMeasuredHeight;//dy >= 0
        int scrollY = ();//Get the user's offset in the y-axis direction (top + bottom -)        if (scrollY <= 0 || scrollY >= dy) {
            return true;// Handle it in the way we customize MyScrollView        }
        //Others that are within the critical range will return false.  That means that it is still processed in the ScrollView method        return false;
    }

5. Determine whether the flat drawing is needed

//Discern whether flat-moving painting needs to be performed    private boolean isNeedAnimation() {
        return !();
    }

6. Since we have done event interception, we must rewrite ontouchevent to execute response events.

@Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (childView == null || !isFinishAnimation) {
            return (ev);
        }
        int eventY = (int) ();//Get the current y-axis coordinate        switch (()) {
            case MotionEvent.ACTION_DOWN:
                lastY = eventY;
                break;
            case MotionEvent.ACTION_MOVE:
                int dy = eventY - lastY;//Small amount of movement                if (isNeedMove()) {
                    if (()) {
                        //The left, upper, right, and lower critical state of childView is recorded.                        ((), (), (), ());
                    }
                    //Re-layout                    ((), () + dy / 2, (), () + dy / 2);
                }
                lastY = eventY;//Reassign value                break;
            case MotionEvent.ACTION_UP:
                if (isNeedAnimation()) {
                    //Use flat moving painting                    int translateY = () - ;
                    TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, -translateY);
                    (200);
//          (true);// Stay at the final position                    (new () {
                        @Override
                        public void onAnimationStart(Animation animation) {
                            isFinishAnimation = false;
                        }
                        @Override
                        public void onAnimationEnd(Animation animation) {
                            isFinishAnimation = true;
                            ();//Clear the animation                            //Re-layout                            (, , , );
                            //Clear normal data                            ();
                        }
                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });
                    //Start the animation                    (translateAnimation);
                }
                break;
        }
        return (ev);
    }

In this way, the core part of the entire view has been completed. Nesting the view into the defined scrollview can achieve the sliding rebound effect of the page.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.