"Go" Android Custom Control (ii)--Resilient ScrollView

Source: Internet
Author: User

Original address: http://blog.csdn.net/a105865708/article/details/17784041

When the fingers slide to the top and bottom of the ScrollView,

You can continue stretching up and down. When you release your finger, bounce back up and down.

Effect:

Main code:

1      Public classElasticscrollviewextendsScrollView {2         PrivateView Inner; 3         Private floaty; 4         PrivateRect normal =NewRect (); 5         Private BooleanAnimationfinish =true; 6       7          PublicElasticscrollview (Context context) {8             Super(context); 9         }  Ten        One          PublicElasticscrollview (Context context, AttributeSet attrs) { A             Super(context, attrs);  -         }   -        the @Override -         protected voidonfinishinflate () { -             if(Getchildcount () > 0) {   -Inner = getchildat (0);  +             }   -         }   +            A @Override at          Public Booleanonintercepttouchevent (motionevent ev) { -             return Super. onintercepttouchevent (EV);  -         }   -        - @Override -          Public Booleanontouchevent (motionevent ev) { in             if(Inner = =NULL) {   -                 return Super. ontouchevent (EV);  to}Else {   + commontouchevent (EV);  -             }   the             return Super. ontouchevent (EV);  *         }   $       Panax Notoginseng          Public voidcommontouchevent (motionevent ev) { -             if(animationfinish) { the                 intAction =ev.getaction ();  +                 Switch(action) { A                  CaseMotionevent.action_down: the     //System.out.println ("Action_down");  +y =ev.gety ();  -                     Super. ontouchevent (EV);  $                      Break;  $                  Casemotionevent.action_up: -     //System.out.println ("action_up");  -y = 0;  the                     if(Isneedanimation ()) { - animation (); Wuyi                     }   the                     Super. ontouchevent (EV);  -                      Break;  Wu                  CaseMotionevent.action_move: -     //System.out.println ("Action_move");  About                     Final floatPreY = y = = 0?ev.gety (): y;  $                     floatNowy =ev.gety ();  -                     intDeltaY = (int) (PreY-Nowy);  -                     //scrolling -     //Scrollby (0, DeltaY);  A        +y =Nowy;  the                     //when scrolling to the top or bottom, no more scrolling, then move the layout -                     if(Isneedmove ()) { $                         if(Normal.isempty ()) { the                             //save a normal layout location the Normal.set (Inner.getleft (), Inner.gettop (), Inner.getright (), Inner.getbottom ());  the                         }   the                         //Mobile Layouts -Inner.layout (Inner.getleft (), Inner.gettop ()-DELTAY/2, Inner.getright (), Inner.getbottom ()-DELTAY/2);  in}Else {   the                         Super. ontouchevent (EV);  the                     }   About                      Break;  the                 default:   the                      Break;  the                 }   +             }   -         }   the       Bayi         //Turn on animation movement the        the          Public voidAnimation () { -             //turn on mobile animation -Translateanimation TA =NewTranslateanimation (0, 0, 0, normal.top-inner.gettop ());  theTa.setduration (200);  theTa.setanimationlistener (NewAnimationlistener () { the @Override the                  Public voidOnanimationstart (Animation Animation) { -Animationfinish =false;  the        the                 }   the       94 @Override the                  Public voidonanimationrepeat (Animation Animation) { the        the                 }  98        About @Override -                  Public voidonanimationend (Animation Animation) {101 inner.clearanimation (); 102                     //set back to normal layout position103 inner.layout (Normal.left, Normal.top, Normal.right, Normal.bottom); 104 Normal.setempty ();  theAnimationfinish =true; 106                 }  107             }); 108 inner.startanimation (TA); 109         }   the       111         //do you want to turn on animation the          Public Booleanisneedanimation () {113             return!Normal.isempty ();  the         }   the        the         //Whether you need to move the layout117          Public BooleanIsneedmove () {118             intoffset = inner.getmeasuredheight ()-getheight (); 119             intscrolly =getscrolly ();  -             if(scrolly = = 0 | | scrolly = =offset) {  121                 return true; 122             }  123             return false; 124         }   the       126}

"Go" Android Custom Control (ii)--Resilient ScrollView

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.