Stretchanimation stretch animation.

Source: Internet
Author: User

The principle is to inherit animation and then change his margintop and marginbottom to form 2 effects

Expandtopanimation

public class Expandtopanimation extends Animation {private View manimatedview;private layoutparams mviewlayoutparams; private int Mmarginstart, mmarginend;private boolean misvisibleafter = false;private Boolean mwasendedalready = False;pub Lic expandtopanimation (view view, int duration) {setduration (duration); Manimatedview = View;mviewlayoutparams = ( Layoutparams) View.getlayoutparams ()//If the bottom margin is 0,//and after the animation would end it ' ll be negative, and Invisible.misvisibleafter = (Mviewlayoutparams.topmargin = = 0); mmarginstart = Mviewlayoutparams.topmargin; Mmarginend = (Mmarginstart = = 0?) (0-view.getheight ()): 0); view.setvisibility (view.visible);} @Overrideprotected void Applytransformation (float interpolatedtime, transformation t) {super.applytransformation ( Interpolatedtime, T), if (Interpolatedtime < 1.0f) {//calculating the new bottom margin, and setting Itmviewlayoutparam S.topmargin = mmarginstart+ (int) ((mmarginend-mmarginstart) * interpolatedtime);//Invalidating the layout, making us seeing the changes we mademanimatedview.requestlayout ();//Making sure we didn ' t run th E ending before (it happens!)} else if (!mwasendedalready) {mviewlayoutparams.topmargin = Mmarginend;manimatedview.requestlayout (); if ( Misvisibleafter) {manimatedview.setvisibility (view.gone);} Mwasendedalready = True;}}}

 

Expandbottomanimation

 Public classExpandbottomanimation extends Animation {PrivateView Manimatedview; PrivateLayoutparams Mviewlayoutparams; Private intMmarginstart, Mmarginend; PrivateBoolean misvisibleafter =false; PrivateBoolean mwasendedalready =false;  PublicExpandbottomanimation (View view,intduration)        {setduration (duration); Manimatedview=view; Mviewlayoutparams=(Layoutparams) view.getlayoutparams (); //If the bottom margin is 0,//Then after the animation would end it ' ll be negative, and invisible.Misvisibleafter = (Mviewlayoutparams.bottommargin = =0); Mmarginstart=Mviewlayoutparams.bottommargin; Mmarginend= (Mmarginstart = =0? (0-View.getheight ()):0);    View.setvisibility (view.visible); } @Overrideprotected voidApplytransformation (floatInterpolatedtime, Transformation t)        {super.applytransformation (interpolatedtime, T); if(Interpolatedtime <1.0f) {            //calculating the new bottom margin, and setting itMviewlayoutparams.bottommargin =Mmarginstart+ (int) ((mmarginend-mmarginstart) *interpolatedtime); //invalidating the layout, making us seeing the changes we mademanimatedview.requestlayout (); //Making sure we didn ' t run the ending before (it happens!)}Else if(!Mwasendedalready) {Mviewlayoutparams.bottommargin=Mmarginend;            Manimatedview.requestlayout (); if(misvisibleafter) {manimatedview.setvisibility (view.gone); } Mwasendedalready=true; }    }}

Mainactivity

 Public classMainactivityextendsActivity {PrivateListView LV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); LV=(ListView) Findviewbyid (r.id.lv); Arrayadapter<String> adapter =NewArrayadapter<string> ( This, R.layout.lv_item);  for(inti = 0; I < 30; i++) {Adapter.add ("Wiki" +i);            } lv.setadapter (adapter); }     Public voidTop (view view) {//Trans (LV);expandtopanimation ea=NewExpandtopanimation (LV, 200);    View.startanimation (EA); }         Public voidBottom (view view) {expandbottomanimation ea=NewExpandbottomanimation (LV, 200);    View.startanimation (EA); }        }

Top effect

Bottom effect

Stretchanimation stretch animation.

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.