Android Custom Flex ScrollView
Summarizes the next most recently written elastic ScrollView, the following code is mainly through the touch event plus dynamic change layout implementation of the elastic ScrollView, specific analysis are in the annotations!
Package Ljh.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;/** * Elastic ScrollView realizes pull back and bounce back * * @author LJH * August 26, 2015 */public class Reboundscrollview extends ScrollView {//Save ScrollView Neutron control private View Contentview = null;/ /The layout information used to hold the unique child control private Rect contentviewrect = new rect ();//The y-coordinate of the start of the move the private float starty;//linear damping buffer excess movement speed private STA Tic float move_factor = 0.5f;//Excessive Displacement recovery animation duration private static long Duration_millis = 280;public Reboundscrollview (Context Co ntext, attributeset attrs, int defstyleattr) {Super (context, attrs, defstyleattr);} Public Reboundscrollview (context context, AttributeSet Attrs) {Super (context, attrs);} Public Reboundscrollview (Context context) {super (context);} /** * The unique sub-view of ScrollView is obtained after layout is complete and there is contentview in/@Overrideprotected void Onfinishinflate () {if (getchildCount () > 0) {contentview = Getchildat (0);}} /** * Handling Touch Events in event distribution * Based on the mechanism of event distribution in Android, the individual feels it is better to write the event-handling logic in the dispatcher than in the Ontouchevent, because the touch event is handled by itself before its child view has received 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)) {// The distance that the view moves beyond the screen scroll is move_factor times detaily = (int) (detaily * move_factor) of the sliding displacement, and//re-layout child view, And only modify the top and bottom positions contentview.layout (contentviewrect.left, Contentviewrect.top + detaily, Contentviewrect.right, Contentviewrect.bottom + detaily);} Break;default:break;} return super.dispatchtouchevent (EV);} /** * Contentview layout is also determined after layout is complete */@Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {Super.on Layout (changed, L, T, R, b), or//Contentview before the move has not changed, that is contentview in the globalThe layout does not change if (contentview! = null) {Contentviewrect.set (Contentview.getleft (), Contentview.gettop (), Contentview.getright ( ), Contentview.getbottom ());}} /** * Determine if you need to move beyond the screen * * by three to determine whether to move and how to move, these three quantities are scrolly, * contentviewheight and scrollviewheight plus auxiliary detaily finger Movement displacement. In three cases: * * Two of them are contentviewheight>scrollviewheight: * 1, when the top of Contentview is at the top of the ScrollView and swipe down on the finger, you need to go beyond the screen movement condition: * Scrolly = = 0 && detaily > 0, * |-----scrollviewheight-----| * |----------contentviewheight--------| *-----detaily----> * 2, when the bottom of the Contentview is at the bottom of the ScrollView and swipe up on your finger, you need to exceed the screen movement condition: * scrolly + scrollviewheight >= cont Entviewheight && detaily < 0, * |--scrolly--| * |-----scrollviewheight-----| * |-----------contentviewheight----------| * <-----detaily----* * Another case is that contentviewheight<=scrollviewheight swipe up or down Need to do beyond the screen movement * 3, when the Contentview itself in the ScrollView internal time regardless of the move up or down the finger when the need to exceed the screen movement conditions: * contentviewheight <= scrollviewheight, * |-----scroLlviewheight-----| * |---contentviewheight---| * <-----detaily----> * * @param detaily * shift of finger movement (swipe down or right for positive direction) * @return need to move */private boolean Isnee Dmove (int detaily) {int scrolly = getscrolly (); int contentviewheight = Contentview.getheight (); int scrollviewheight = Get Height (); return (scrolly = = 0 && detaily > 0) | | (scrolly + scrollviewheight >= contentviewheight && detaily < 0) | | (Contentviewheight <= scrollviewheight);} /** * Play Contentview reset animation and reset the Contentview * Animation can be customized * animation execution time decreases with the distance of the extrusion */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,conteNtviewrect.bottom);} /** * Determine if animation effects are required * @return */private boolean isneedanimation () {return contentview.gettop ()! = Contentviewrect.top;}}
The problem in this implementation is or is to be optimized, when the Reboundscrollview at the top or the lowest end of the drag to achieve excessive displacement without releasing the finger and then reversing the offset, the scroll bar will scroll! If there are other questions can be a message to see a certain reply! Small partners have better elasticity scrollview words can share to me! email [email protected] Thank you.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Custom Flex ScrollView