Android custom elastic ScrollView

Source: Internet
Author: User

Android custom elastic ScrollView

Android custom elastic ScrollView

Summarizes the recently written elastic ScrollView. The following code mainly uses the touch event and Dynamic Layout Change to implement the elastic ScrollView. The specific analysis is in the annotation!

 

 

Package l.pdf. android. view; import android. content. context; import android. graphics. rect; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; import android. view. animation. translateAnimation; import android. widget. scrollView;/*** implement pull-down and pull-up of elastic ScrollView ** @ author lw.* August 26, 2015 */public class ReboundScrollView extends ScrollView {// Save the private View of the subcontrol of ScrollView ContentView = null; // used to save the layout information of the unique child control. private Rect contentViewRect = new Rect (); // The Y coordinate private float startY at the start of the Movement; // linear damping buffer the movement speed of excessive movement. private static float MOVE_FACTOR = 0.5f; // The animation duration for over-displacement recovery. private static long DURATION_MILLIS = 280; public ReboundScrollView (Context context, attributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr);} public ReboundScrollView (Context context, AttributeSe T attrs) {super (context, attrs) ;}public ReboundScrollView (Context context) {super (context) ;}/ *** obtain the unique subview of the ScrollView after the layout is complete, in contentView, */@ Overrideprotected void onFinishInflate () {if (getChildCount ()> 0) {contentView = getChildAt (0 );}} /*** processing touch events in event distribution * judging from the event distribution mechanism in android, I personally think it is better to write the event processing logic in the distributor than to write it in onTouchEvent, * because the child View handles the touch event before it receives the touch event. * // @ Overridepublic boolean dispatchTouchEvent (MotionEvent ev) {if (contentView! = Null) switch (ev. getAction () {case MotionEvent. ACTION_DOWN: startY = ev. getY (); break; case MotionEvent. ACTION_UP: if (isNeedAnimation () {playAnimation ();} break; case MotionEvent. ACTION_MOVE: float nowY = ev. getY (); int detailY = (int) (nowY-startY); if (isNeedMove (detailY )) {// After the screen is exceeded, the moving distance of the scroll View is MOVE_FACTOR times detailY = (int) (detailY * MOVE_FACTOR); // re-layout the sub-View, only the contentView at the top and bottom is modified. layout (conte NtViewRect. left, contentViewRect. top + detailY, contentViewRect. right, contentViewRect. bottom + detailY);} break; default: break;} return super. dispatchTouchEvent (ev);}/*** after the layout is complete, the contentView layout determines */@ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {super. onLayout (changed, l, t, r, B); // The layout of contentView remains unchanged if (contentView! = Null) {contentViewRect. set (contentView. getLeft (), contentView. getTop (), contentView. getRight (), contentView. getBottom () ;}}/*** determine whether to move beyond the screen ** determine whether to move and how to move through three quantities, the three quantities are scrollY, * contentViewHeight, and scrollViewHeight, respectively, plus the displacement of the secondary detailY finger movement. There are three situations: ** two of them are contentViewHeight> scrollViewHeight: * 1. When the top of contentView is on the top of ScrollView and sliding its finger down, the conditions for moving the screen are as follows: * scrollY = 0 & detailY> 0, * | ----- scrollViewHeight ----- | * | ---------- contentViewHeight -------- | * ----- detailY ----> ** 2. When the bottom of contentView is at the bottom of ScrollView and the screen needs to be moved up and down, the condition is: * scrollY + scrollViewHeight> = contentViewHeight & detailY <0, * | -- scrollY -- | * | ----- scrollViewHeight ----- | * | ----------- ContentViewHeight ---------- | * <----- detailY ---- ** another case is contentViewHeight <= scrollViewHeight sliding up and down requires moving beyond the screen. * 3. When contentView is inside ScrollView when swiping your finger up or down, you must move the screen beyond the following conditions: * contentViewHeight <= scrollViewHeight, * | ----- scrollViewHeight ----- | * | --- contentViewHeight --- | * <----- detailY ----> ** @ param detailY * shift of the finger movement (sliding down or to the right in the positive direction) * @ return whether to move */private boolean isNeedMove (int de TailY) {int scrollY = getScrollY (); int contentViewHeight = contentView. getHeight (); int scrollViewHeight = getHeight (); return (scrollY = 0 & detailY> 0) | (scrollY + scrollViewHeight> = contentViewHeight & detailY <0) | (contentViewHeight <= scrollViewHeight );} /*** play the reset animation of contentView and reset the contentView. * The animation can be customized. * The animation execution time decreases with the increase of the stretch distance */private void playAnimation () {int contentViewTop = contentView. GetTop (); int scrollViewHeight = this. getHeight (); float factor = 1-Math.abs (contentViewTop-contentViewRect. top)/(scrollViewHeight * 1.0f); TranslateAnimation ta = new TranslateAnimation (0, 0, contentViewTop, contentViewRect. top); ta. setDuration (long) (DURATION_MILLIS * factor); contentView. startAnimation (ta); contentView. layout (contentViewRect. left, contentViewRect. top, contentViewRect. right, contentViewRec T. bottom);}/*** determine whether the animation effect is required * @ return */private boolean isNeedAnimation () {return contentView. getTop ()! = ContentViewRect. top ;}}

 

This implementation method has problems or issues to be optimized. When the ReboundScrollView is at the top or at the bottom of the drag to achieve excessive displacement, the scroll bar will scroll when the finger is not released and the offset is reduced in reverse direction! If you have other questions, please leave a message and you will receive a reply! If you have a better elastic ScrollView, you can share it with me! Email 1075209054@qq.com thank you.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.