Android Open Source project Slidingmenu in-depth analysis

Source: Internet
Author: User

Slidingmenu Introduction:

Slidingmenu is a relatively new setup interface or configuration interface effect, in the main interface left or right slide display settings interface, can easily do a variety of operations. There are a lot of applications that are using this effect now. such as Evernote, Google +, Foursquare, and so on, the domestic pea folder, everyone, 360 mobile phone assistants, etc. are using the Slidingmenu interface scheme.   Project: Https://github.com/jfeinstein10/SlidingMenu Note: Slidingmenu relies on another open source project Actionbarsherlock, Therefore, it is necessary to add Actionbarsherlock as a Slidingmenu library project, otherwise it will report that the resource could not find the error. Then add the Slidingmenu to your project.  slidingmenu Integration Common error: Jar mismatch! Fix your dependencies: a conflict caused by a inconsistency in the version of the jar package referenced by the project and itself. Ensure that the jar package versions used by Slidingmenu and Actionbarsherlock are consistent   If you use the relevant API to Actionbarsherlock, May not find the way to Getsupportactionbar and other actionbarsherlock. The reason is that the activity that uses actionbarsherlock needs to inherit from Sherlockactivity, modify the slidingfragmentactivity in the Slidingmenu library, Let it inherit from the sherlockfragmentactivity and recompile the library import.  slidingmenu Common Properties: Menu.setmode (Slidingmenu.left);//Set Left slide menu Menu.settouchmodeabove (slidingmenu.touchmode_ Fullscreen)///Set the sliding screen range, which can slide menu.setshadowdrawable (R.drawable.shadow) in full-screen area,//Set shadow Picture Menu.setshadowwidthres ( R.dimen.shadow_width)///Set the width of the shadow Picture Menu.setbehindoffsetres (R.dimen.slidingmenu_offset),//slidingmenu when the main page is markedThe remaining width of the menu.setbehindwidth (400);//Set the width of the Slidingmenu menu Menu.setfadedegree (0.35f);// Slidingmenu Gradient Degree menu.attachtoactivity (this, slidingmenu.sliding_content) when sliding;// Make slidingmenu attached to activity Menu.setmenu (r.layout.menu_layout);//Set menu layout file Menu.toggle ();// Dynamic judgment automatically turns off or on Slidingmenumenu.showmenu ();//displays slidingmenumenu.showcontent ();//display content Menu.setonopenlistener ( Onopenlistener);//monitor Slidingmenu Open about close menu There are two listeners, simply speaking, for the menu Close event, one is when and one is After menu. Onclosedlistener (Onclosedlistener);//Listener Slidingmenu When the event menu is closed. Onclosedlistener (Onclosedlistener)///monitor Slidingmenu after Shutdown event   left and right can be drawn slidingmenu menu only need to set Menu.setmode ( Slidingmenu.left_right), and then set the layout file for the menu on the right menu.setsecondaryshadowdrawable (r.drawable.shadowright);// The shadow picture on the right menu uses fragment to implement Slidingmenu:1. First the activity inherits from the SlidingFragmentActivity2 under the Slidingmenu package. Setcontentview (r.layout.content_frame);//The layout is a full-screen FrameLayout3. Setbehindcontentview (r.layout.menu_frame);//Set the layout used by the Slidingmenu, which is also a full-screen FrameLayout4. Set Slidingmenu on the left menu of fragment      [JAVA] SETBEhindcontentview (R.layout.menu_frame);  fragmenttransaction t = This.getsupportfragmentmanager (). BeginTransaction ();  leftmenufragment = new Menufragment ();  t.replace (R.id.menu_frame, leftmenufragment);  t.commit ();   menufragment is actually a fragment, show a ListView and then click on each item of the ListView, notify the activity switch different fragment in order to see the effect, We created 5 new frament, namely Fragment1, Fragment2, Fragment3, Fragment4, Fragment5 in the Slidingmenu with a listview display.   Set the main page display Fragment:[java]  if (savedinstancestate = = null) {//== NULL when creating a new Fragment1  contentfragment = New Fragment1 ();  } else {//not equal to NULL, direct get out  //not equal to NULL to find the fragment  contentfragment of the current activity shown previously saved = Getsupportfragmentmanager (). Getfragment (Savedinstancestate, "contentfragment");  }  //set content fragment  getsupportfragmentmanager ()  .begintransaction ()  .replace ( R.id.content_frame, Contentfragment)  .commit ();     saves the currently displayed Fragmentgetsupportfragmentmanager () in the onsaveinstancestate of the activity. PutFragment (outstate, "contentfragment", contentfragment); set Slidingmenu Property SM = Getslidingmenu ();//If only the left menu is displayed, On the right side, all support is Left_rightsm.setmode (slidingmenu.left_right);//Set menu slide mode, whether the menu appears on the left or right side, Or the left and right sides have sm.setshadowdrawable (R.drawable.shadow);//Set the shadow of the picture Resource Sm.setshadowwidthres (r.dimen.shadow_width);// Sets the width of the shadow Picture//sm.setbehindwidth (200);//The Width sm.setbehindoffsetres (r.dimen.slidingmenu_offset) of the Settings menu;// Slidingmenu the remaining width sm.settouchmodeabove (slidingmenu.touchmode_fullscreen) displayed on the main page,//Set the sliding area   support the right side of the menu:// Slidingmenu can simultaneously support the left and right side of the menu, non-conflict, and animation beautiful, good experience. Sm.setsecondarymenu (r.layout.menu_frame2);//Set Right menu sm.setsecondaryshadowdrawable (r.drawable.shadowright);// Sets the picture resource for the right menu shadow//Right Slidingmenu Fragment getsupportfragmentmanager (). BeginTransaction (). Replace (r.id.menu_ Frame2, New Samplelistfragment ()). Commit ();  settings Actionbar can be clicked: Getsupportactionbar (). sethomebuttonenabled ( true);//actionbar main button can be clicked Getsupportactionbar (). Setdisplayhomeasupenabled (true);// Show left icon setslidingactionbarenabled (false);//right and left side slidingThe Fragment of the menu shows whether the title bar   Toggle Main Page display fragment:public void Switchcontent (Fragment f) {//assigns content Fragment, and save this fragmentcontentfragment = f when onsaveinstancestate; Fragmenttransaction  t = Getsupportfragmentmanager (). BeginTransaction (); T.replace (R.id.content_frame, F); T.commit (); Sm.showcontent ();  uses normal activity to implement Slidingmenu:[java]  slidingmenu menu = new Slidingmenu (this); /Direct new, not Getslidingmenu  menu.setmode (slidingmenu.left);  menu.settouchmodeabove (Slidingmenu.touchmode_fullscreen);  menu.setshadowdrawable (R.drawable.shadow);  menu.setshadowwidthres (R.dimen.shadow_width);  menu.setbehindoffsetres (R.dimen.slidingmenu_offset);  menu.setbehindwidth (400);//Set the width of the Slidingmenu menu  menu.setfadedegree (0.35f);  menu.attachtoactivity (this, slidingmenu.sliding_content);//Must call  menu.setmenu (R.layout.menu_layout_ left);//is the general layout  menu.setbehindcanvastransformer (Mtransformer);     Click events in the corresponding Slidingmenu, because Slidingmenu is already included in the activity, so the direct Findviewbyid (ID), get the view can be processed accordingly.   Support the left and right sides can draw a menu menu.setsecondarymenu (r.layout.menu_layout_right); Menu.setsecondaryshadowdrawable ( R.drawable.shadowright);  Right menu just like direct Findviewbyid (ID), get view after you can handle the   Changing Slidingmenu animations Slidingmenu support left or right sliding when defining different animations, including stretching, zooming, rotating, and so on. It is the animation that slidingmenu how to appear during the sliding process. Animation use is also very simple first define Canvastransformer mtransformer; variables: [java]  mtransformer = new Canvastransformer () {  @Override & nbsp;public void Transformcanvas (canvas canvas, float percentopen) { float scale = (float) (percentopen*0.25 + 0.75) ;  canvas.scale (scale, scale, canvas.getwidth ()/2, Canvas.getheight ()/2);  }  };    then set the Mtransformer object to Slidingmenu, which is the Zoom animation: [Java]  private void Initslidupcanvastransformer () {  mtransformer = new Canvastransformer () {    @Override  public void Transformcanvas (Canvas Canvas, float percentopen) { canvas.translate (0, Canvas.getheight () * (1-interp.getinterpolation (percentopen))) ;  }  };  }    private static Interpolator interp = new Interpolator () {  @Override  public float get Interpolation (float t) { t-= 1.0f;  return T * t * t + 1.0f;  }  };    Stretch Animation: [java]  mtransformer = new Canvastransformer () {  @Override  public void Transformcanvas (canvas canvas, float percentopen) { canvas.scale (Percentopen, 1, 0, 0);  }  });   

Android Open Source project Slidingmenu in-depth analysis

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.