/*** Implementation of scrollview rebound effect */public class bouncescrollview extends scrollview {private view inner; // child viewprivate float y; // when the Y coordinate is clicked, private rect normal = new rect (); // rectangle (this is only a form, only used to determine whether an animation is required .) private Boolean iscount = false; // whether to start calculating public bouncescrollview (context, attributeset attrs) {super (context, attrs );} /***** generate a view based on XML. this function is called at the end of the view generation. After all the child views are added. even if the subclass overwrites the onfinishinflate * method, you should also call the parent Class, so that the method can be executed. */@ overrideprotected void onfinishinflate () {If (getchildcount ()> 0) {inner = getchildat (0 );}} /***** listen to touch */@ overridepublic Boolean ontouchevent (motionevent eV) {If (inner! = NULL) {commontouchevent (EV);} return Super. ontouchevent (EV);}/*** touch event *** @ Param EV */Public void commontouchevent (motionevent eV) {int action = eV. getaction (); Switch (Action) {Case motionevent. action_down: break; Case motionevent. action_up: // loosen your finger. if (isneedanimation () {animation (); iscount = false;} break;/*** exclude the first moving computation, because the Y coordinate cannot be known for the first time, in the motionevent. not obtained in action_down, * because it is the touch event of myscrollview Handed to the Child item of listview. so it starts from the second calculation. * However, we also need to initialize the sliding distance to 0 when moving for the first time. after the record is accurate, it will be executed normally. */case motionevent. action_move: Final float prey = y; // The Y coordinate float Nowy = eV at press time. gety (); // time y coordinate int deltay = (INT) (Prey-Nowy); // sliding distance if (! Iscount) {deltay = 0; // here 0 is returned .} y = Nowy; // it will not be rolled when it is rolled to the top or bottom. In this case, move the layout if (isneedmove () {// initialize the header rectangle if (normal. isempty () {// Save the normal layout position normal. set (inner. getleft (), inner. gettop (), inner. getright (), inner. getbottom ();} log. E ("JJ", "rectangle:" + inner. getleft () + "," + inner. gettop () + "," + inner. getright () + "," + inner. getbottom (); // move the layout inner. layout (inner. getleft (), inner. gettop ()-deltay/2, inner. getrig HT (), inner. getbottom ()-deltay/2);} iscount = true; break; default: break;}/***** contraction animation */Public void animation () {// enable the mobile animation translateanimation TA = new translateanimation (0, 0, inner. gettop (), normal. top); TA. setduration (1, 200); inner. startanimation (TA); // set to return to the normal layout position inner. layout (normal. left, normal. top, normal. right, normal. bottom); log. E ("JJ", "regression:" + normal. left + "," + normal. top + "," + normal. righ T + "," + normal. Bottom); normal. setempty () ;}// whether to enable the public Boolean isneedanimation () {return! Normal. isempty ();}/*** whether to move the layout inner. getmeasuredheight (): Get the total height of the control ** getheight (): Get the screen height ** @ return */Public Boolean isneedmove () {int offset = inner. getmeasuredheight ()-getheight (); int scrolly = getscrolly (); log. E ("JJ", "scrolly =" + scrolly); // 0 is the top, and the back is the bottom if (scrolly = 0 | scrolly = offset) {return true;} return false ;}}
Usage
<COM. techrare. view. bouncescrollview Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Background = "@ color/tab_chart_bg" Android: scrollbars = "NONE"> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "match_parent" Android: gravity = "center_horizontal" Android: Orientation = "vertical" Android: paddingleft = "20dp" Android: paddingright = "20dp"> <span style = "wh ITE-space: pre "> </span> <! -- Layout here --> </linearlayout> </COM. techrare. View. bouncescrollview>
Original address http://blog.csdn.net/h7870181/article/details/8960430