Android Side-Slide Menu implementation

Source: Internet
Author: User

Android side-by-side menu implementation, refer to the online code to implement the side-slip menu. The most important thing is that this animation class ugcanimations, how to use the animation class to slide the package flipperlayout.

The source of this project: click

1. Realize the effect


2, Animation class Ugcanimations

Package Com.mmsx.base;import Android.content.context;import Android.view.view;import Android.view.viewgroup.marginlayoutparams;import Android.view.animation.alphaanimation;import Android.view.animation.animation;import Android.view.animation.animationset;import Android.view.animation.anticipateinterpolator;import Android.view.animation.overshootinterpolator;import Android.view.animation.rotateanimation;import Android.view.animation.scaleanimation;import Android.view.animation.translateanimation;import Android.widget.imageview;import android.widget.RelativeLayout;/ * * Path Animation class * */public class Ugcanimations {private static int xoffset = 15;private static int yoffset = -13;public stat IC void Initoffset (context context) {xoffset = (int) (* context.getresources (). Getdisplaymetrics (). density); Yoffset = -(int) (* context.getresources (). Getdisplaymetrics (). density);} public static Animation getrotateanimation (float fromdegrees,float todegrees, long Durationmillis) {rotateanimation RotatE = new Rotateanimation (fromdegrees, Todegrees,animation.relative_to_self, 0.5f, animation.relative_to_self,0.5f); Rotate.setduration (Durationmillis); Rotate.setfillafter (true); return rotate;}  public static Animation getalphaanimation (float fromalpha, float toalpha,long durationmillis) {alphaanimation alpha = new Alphaanimation (Fromalpha, Toalpha); Alpha.setduration (Durationmillis); Alpha.setfillafter (true); return Alpha;} public static Animation getscaleanimation (long durationmillis) {scaleanimation scale = new Scaleanimation (1.0f, 1.5f, 1.0 F, 1.5f,animation.relative_to_self, 0.5f, animation.relative_to_self,0.5f); Scale.setduration (DurationMillis); return scale;} public static Animation gettranslateanimation (float fromxdelta,float toxdelta, float fromydelta, float toydelta,long Durationmillis) {Translateanimation translate = new Translateanimation (Fromxdelta,toxdelta, Fromydelta, ToYDelta); Translate.setduration (Durationmillis); Translate.setfillafter (true); return translate;} public static void STartopenanimation (relativelayout relativelayout,imageview background, ImageView menu, long Durationmillis) { Background.setvisibility (view.visible); relativelayout.setvisibility (view.visible); Background.startAnimation ( Getalphaanimation (0f, 1f, Durationmillis)); Menu.startanimation (getrotateanimation (0, N, durationmillis)); for (int i = 0; I < Relativelayout.getchildcount (); i++) {ImageView ImageView = (ImageView) relativelayout.getchildat (i); imageview.setvisibility (view.visible); Marginlayoutparams params = (marginlayoutparams) imageview.getlayoutparams (); Animationset set = new Animationset (true); Set.addanimation (getrotateanimation ( -270, 0, Durationmillis)); Set.addanimation (Getalphaanimation (0.5f, 1.0f, Durationmillis)) Set.addanimation (Gettranslateanimation (- Params.leftmargin + Xoffset, 0f, params.bottommargin+ yoffset, 0f, Durationmillis); Set.setfillafter (true); Set.setduration (Durationmillis); Set.setstartoffset ((i * +)/( -1 + relativelayout.getchildcount ())); Set.setinterpolator (New Overshootinterpolator (1f)); Imageview.startanimation (set);}} public static void Startcloseanimation (final relativelayout relativelayout,final ImageView background, ImageView menu, Long Durationmillis) {background.startanimation (Getalphaanimation (1f, 0f, Durationmillis)); Menu.startanimation ( Getrotateanimation (0, Durationmillis)); for (int i = 0; i < Relativelayout.getchildcount (); i++) {Final ImageView im Ageview = (ImageView) relativelayout.getchildat (i); Marginlayoutparams params = (marginlayoutparams) imageview.getlayoutparams (); Animationset set = new Animationset (true); Set.addanimation (getrotateanimation (0, -270, Durationmillis)); Set.addanimation (Getalphaanimation (1.0f, 0.5f, Durationmillis)), Set.addanimation (Gettranslateanimation (0f,- params.leftmargin+ Xoffset, 0f, Params.bottommargin + yoffset,durationmillis); Set.setfillafter (true); Set.setduration (Durationmillis); Set.setstartoffset (((Relativelayout.getchildcount ()-i) * 100)/(-1 + Relativelayout.getchildcount ())); Set.setintErpolator (New Anticipateinterpolator (1f)); Set.setanimationlistener (new Animation.animationlistener () {public void Onanimationstart (Animation arg0) {}public void onanimationrepeat (Animation arg0) {}public void Onanimationend ( Animation arg0) {relativelayout.setvisibility (view.gone); background.setvisibility (View.gone);}}); Imageview.startanimation (set);}} public static Animation clickanimation (long durationmillis) {Animationset set = new Animationset (true); set.addanimation (Getalphaanimation (1.0f, 0.3f, Durationmillis)); Set.addanimation (Getscaleanimation (Durationmillis)); Set.setduration (Durationmillis); return set;}}

3, packaging use animation class Flipperlayout

Package Com.mmsx.base;import Android.content.context;import Android.util.attributeset;import Android.util.typedvalue;import Android.view.motionevent;import Android.view.velocitytracker;import Android.view.view;import Android.view.viewconfiguration;import Android.view.viewgroup;import android.widget.scroller;/** * Own rewrite of the ViewGroup, with the use of sliding switch interface, the code is not known, the words should be able to read slowly ... */public class Flipperlayout extends ViewGroup {private Scroller mscroller;private velocitytracker mvelocitytracker;private int mwidth;public static final int screen_state_close = 0;public static final int screen_state_open = 1;public static final int touch_state_restart = 0;p  ublic static final int touch_state_scrolling = 1;public static final int scroll_state_no_allow = 0;public static final int Scroll_state_allow = 1;private int mscreenstate = 0;private int mtouchstate = 0;private int mscrollstate = 0;private int Mvelocityvalue = 0;private Boolean monclick = false;private onugcdismisslistener monugcdismisslistener;private OnUgcSHowlistener Monugcshowlistener;public flipperlayout (context context) {super (context); Mscroller = new Scroller (context ); mwidth = (int) typedvalue.applydimension (typedvalue.complex_unit_dip,54, Getresources (). Getdisplaymetrics ());} Public Flipperlayout (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} Public Flipperlayout (context context, AttributeSet Attrs) {Super (context, attrs);} protected void OnLayout (Boolean changed, int l, int t, int r, int b) {for (int i = 0; i < Getchildcount (); i++) {View C Hild = Getchildat (i); int height = child.getmeasuredheight (); int width = Child.getmeasuredwidth (); child.layout (0, 0, width, height);}} protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC); int width = measurespec.getsize (widthmeasurespec); int height = measurespec.getsize ( HEIGHTMEASURESPEC); setmeasureddimension (width, height); for (int i = 0; i < Getchildcount (); i++) {Getchildat (i). MeasurE (Widthmeasurespec, Heightmeasurespec);}} public boolean dispatchtouchevent (motionevent ev) {obtainvelocitytracker (EV), switch (Ev.getaction ()) {case MotionEvent.ACTION_DOWN:mTouchState = mscroller.isfinished ()? Touch_state_restart:touch_state_scrolling;if (mtouchstate = = touch_state_restart) {int x = (int) ev.getx (); int ScreenWidth = GetWidth (); if (x <= mwidth && mscreenstate = = screen_state_close&& Mtouchstate = TOUCH_S tate_restart| | X >= screenwidth-mwidth&& Mscreenstate = = screen_state_open&& Mtouchstate = TOUCH_STATE_RESTART) {i F (mscreenstate = = Screen_state_open) {Monclick = true;} Mscrollstate = Scroll_state_allow;} else {Monclick = False;mscrollstate = Scroll_state_no_allow;}} else {return false;} Break;case MotionEvent.ACTION_MOVE:mVelocityTracker.computeCurrentVelocity (1000, Viewconfiguration.getmaximumflingvelocity ()); if (mscrollstate = = scroll_state_allow&& GetWidth ()-(int) Ev.getx () < Mwidth) {return true;} Break;case MotIonEvent.ACTION_UP:releaseVelocityTracker (); if (Monclick) {Monclick = False;mscreenstate = Screen_state_close; Mscroller.startscroll (Getchildat (1). GETSCROLLX (), 0,-getchildat (1). GETSCROLLX (), 0, N); invalidate (); break;} return super.dispatchtouchevent (EV);} public boolean onintercepttouchevent (motionevent ev) {obtainvelocitytracker (EV), switch (Ev.getaction ()) {case MotionEvent.ACTION_DOWN:mTouchState = mscroller.isfinished ()? Touch_state_restart:touch_state_scrolling;if (mtouchstate = = touch_state_scrolling) {return false;} Break;case MotionEvent.ACTION_MOVE:mOnClick = false;mvelocitytracker.computecurrentvelocity (1000, Viewconfiguration.getmaximumflingvelocity ()); if (mscrollstate = = scroll_state_allow&& Math.abs ( Mvelocitytracker.getxvelocity ()) > () {return true;} Break;case MotionEvent.ACTION_UP:releaseVelocityTracker (); if (mscrollstate = = scroll_state_allow&& Mscreenstate = = Screen_state_open) {return true;} break;} return super.onintercepttouchevent (EV);} Public BooLean Ontouchevent (Motionevent event) {Obtainvelocitytracker (event); switch (Event.getaction ()) {case MotionEvent.ACTION_DOWN:mTouchState = mscroller.isfinished ()? Touch_state_restart:touch_state_scrolling;if (mtouchstate = = touch_state_scrolling) {return false;} Break;case MotionEvent.ACTION_MOVE:mVelocityTracker.computeCurrentVelocity (1000, Viewconfiguration.getmaximumflingvelocity ()); mvelocityvalue = (int) mvelocitytracker.getxvelocity (); getChildAt (1) . ScrollTo (-(int) event.getx (), 0); Break;case MotionEvent.ACTION_UP:if (mscrollstate = scroll_state_allow) {if ( Mvelocityvalue >) {mscreenstate = Screen_state_open;mscroller.startscroll (Getchildat (1). GetScrollX (), 0,-( GetWidth ()-Math.Abs (Getchildat (1). GETSCROLLX ())-mwidth), 0, (+); invalidate ();} else if (Mvelocityvalue < -2000) {mscreenstate = Screen_state_close;mscroller.startscroll (Getchildat (1). GetScrollX (), 0,-getchildat (1). GETSCROLLX (), 0, (+); invalidate ();} else if (Event.getx () < GetWidth ()/2) {mscreenstate = SCREen_state_close;mscroller.startscroll (Getchildat (1). GETSCROLLX (), 0,-getchildat (1). GETSCROLLX (), 0, 800); Invalidate ();} else {mscreenstate = Screen_state_open;mscroller.startscroll (Getchildat (1). GETSCROLLX (), 0,-(GetWidth ()-Math.abs ( Getchildat (1). GETSCROLLX ())-mwidth), 0, (+); invalidate ();}} break;} Return Super.ontouchevent (event);} public void Open () {mtouchstate = mscroller.isfinished ()? Touch_state_restart:touch_state_scrolling;if (mtouchstate = = Touch_state_restart) {mscreenstate = SCREEN_STATE_OPEN; Mscroller.startscroll (Getchildat (1). GETSCROLLX (), 0,-(getwidth ()-Math.Abs (Getchildat (1). GETSCROLLX ())-mWidth), 0 , invalidate ();}} Close the current slide-out menu, open the Click event's page with view public void close (view view) {mscreenstate = Screen_state_close;mscroller.startscroll ( Getchildat (1). GETSCROLLX (), 0,-getchildat (1). GETSCROLLX (), 0, +); invalidate (); Setcontentview (view);} public void Computescroll () {super.computescroll (); if (Mscroller.computescrolloffset ()) {Getchildat (1). ScrollTo ( Mscroller.getcurrx (), mScRoller.getcurry ());p ostinvalidate ();} else {if (mscreenstate = = Screen_state_open) {if (Monugcdismisslistener! = null) {Monugcdismisslistener.dismiss ();}} else if (mscreenstate = = Screen_state_close) {if (Monugcshowlistener! = null) {monugcshowlistener.show ();}}}} private void Obtainvelocitytracker (Motionevent event) {if (Mvelocitytracker = = null) {Mvelocitytracker = Velocitytracker . obtain ();} Mvelocitytracker.addmovement (event);} private void Releasevelocitytracker () {if (Mvelocitytracker! = null) {mvelocitytracker.recycle (); mvelocitytracker = null;}} public int getscreenstate () {return mscreenstate;} public void Setcontentview (view view) {Removeviewat (1); AddView (view, 1, getlayoutparams ());} Public interface Onopenlistener {public abstract void Open ();} Public interface Oncloselistener {public abstract void Close ();} Public interface Onugcdismisslistener {public abstract void Dismiss ();} Public interface Onugcshowlistener {public abstract void show (); public void Setonugcdismisslistener (ONUGCDismisslistener onugcdismisslistener) {monugcdismisslistener = Onugcdismisslistener;} public void Setonugcshowlistener (Onugcshowlistener onugcshowlistener) {monugcshowlistener = Onugcshowlistener;}}
4. Main interface mainactivity
Package Com.mmsx.activity;import Com.mmsx.activity.sideslipmenu.onchangeviewlistener;import Com.mmsx.activity.sideslipother.ondatalistener;import Com.mmsx.base.flipperlayout;import Com.mmsx.base.flipperlayout.onopenlistener;import Com.mmsx.base.viewutil;import Android.os.Bundle;import Android.app.activity;import Android.view.viewgroup.layoutparams;import Android.widget.toast;public Class Mainactivity extends Activity implements onopenlistener{//slide the main control class, set the default interface with the layout private flipperlayout mroot;//Slide The main interface private Sidesliphome mhome;//side-by-side menu, make a selection of the private Sideslipmenu msideslipmenu;//other menu list The effect of the selection of private sideslipother mother;//exit interval variable private long mexittime;//interval 2sprivate static final int INTERVAL = 2000;//side-slip menu selected Itemprivate int mviewposition;//the title Private String mstrtitle, @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Create the container and set the full-screen size mroot = new Flipperlayout (this);//Layout parameters Layoutparams params = new Layoutparams (layoutparams.fill_parent, Layoutparams.fill_parent) Mroot.setlayoutparams (params);//Create Menu interface and Content first page interface, and add to container for initial display mhome = new Sidesliphome (this); Msideslipmenu = new Sideslipmenu (this), Mroot.addview (Msideslipmenu.getview (), params), Mroot.addview (Mhome.getview ( ), params);//settings and Layout Setcontentview (mroot);//Set Listener Setlistener ();} Set the listener private void Setlistener () {Mhome.setonopenlistener (this);//Listen menu interface Toggle Display content ( The Onchangeviewlistener interface is defined in Sideslipmenu) Msideslipmenu.setonchangeviewlistener (new Onchangeviewlistener () {public void Onchangeview (int arg0) {mviewposition = Arg0;mother = new Sideslipother (mainactivity.this); switch (arg0) {case Viewu Til. HOME:mRoot.close (Mhome.getview ()); Break;case ViewUtil.MESSAGE:mstrTitle = "message";//Set Data Interface monitoring Mother.setdatatitle (new Datatitle ()); Mroot.close (Mother.getview ()); Break;case ViewUtil.FRIENDS:mstrTitle = "Friends"; Mother.setdatatitle (new Datatitle ()); Mroot.close (Mother.getview ()); Break;case ViewUtil.PHOTO:mstrTitle = "Photo"; Mother.setdatatitle (new Datatitle ()); Mroot.close (Mother.getview ()); Break;case ViewutiL.viewed:mstrtitle = "reprint"; Mother.setdatatitle (New Datatitle ()); Mroot.close (Mother.getview ()); break;case ViewUtil.GIFTS:mstrTitle = "Gift"; Mother.setdatatitle (New Datatitle ()); Mroot.close (Mother.getview ()); break;case ViewUtil.RECOMMEND:mstrTitle = "Game"; Mother.setdatatitle (New Datatitle ()); Mroot.close (Mother.getview ()); break;case ViewUtil.LBS:mstrTitle = "Nearby"; Mother.setdatatitle (New Datatitle ()); Mroot.close (Mother.getview ()); Break;default: Break;}});} Passing data to a slide-selected page Private class Datatitle implements ondatalistener{@Overridepublic String Getdatatitle () {return Mstrtitle;}} @Overridepublic void Open () {if (mroot.getscreenstate () = = Flipperlayout.screen_state_close) {Mroot.open ();}} /** * Return key listener */public void onbackpressed () {/** * If the path menu of the interface is not closed, the path menu is closed, otherwise the two return time interval is determined, less than two seconds will exit the program */if ( Mroot.getscreenstate () = = Flipperlayout.screen_state_open) {if (msideslipmenu.getugcisshowing ()) { MSIDESLIPMENU.CLOSEUGC ();} else {exit ();}} else {switch (mviewposition) {case ViewUtil.HOME:if (mhome.getugcisshOwing ()) {MHOME.CLOSEUGC ();} else {exit ();} Break;default:exit (); break;}}} /** * Determine two return intervals, less than two seconds exit program */private Void exit () {if (System.currenttimemillis ()-mexittime > INTERVAL) {Toast.maketex T (this, "Press the return key again to exit the program directly", Toast.length_short). Show (); mexittime = System.currenttimemillis ();} else {finish (); Android.os.Process.killProcess (Android.os.Process.myPid ()); System.exit (0);}}

5, Sidesliphome

Package Com.mmsx.activity;import Com.mmsx.base.flipperlayout.onopenlistener;import Android.app.activity;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.widget.textview;public class Sidesliphome {private Context mcontext;private Activity mactivity;private View Mhomeview;private Boolean mugcisshowing = False;private onopenlistener monopenlistener;public SideslipHome (Context Context, activity activity) {Mcontext = Context;mactivity = Activity;mhomeview = Layoutinflater.from (context). Inflate ( R.layout.sideslip_home, NULL); Initui ();} private void Initui () {TextView ivtitlename = (TextView) Mhomeview.findviewbyid (r.id.ivtitlename); ivtitlename.settext ("home page dynamic");} public void Setonopenlistener (Onopenlistener onopenlistener) {monopenlistener = Onopenlistener;} Public View GetView () {return mhomeview;} /** * Get path Menu Display Status */public boolean getugcisshowing () {return mugcisshowing;} /** * Close Path menu */public void Closeugc () {mugcisshowing = false;}}

There are some source code for post, interested can download.

The source of this project: click

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Side-Slide Menu implementation

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.