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.