I. Introduction of Slidingmenu
Slidingmenu is one of the most commonly used open source projects.
The open source project Slidingmenu on GitHub offers the best possible implementation: flexible customization, a variety of shades and gradients, and animations that have a good sliding effect. But this is an open source library, to introduce it as a library into your own project, simple configuration can achieve slidingmenu effect.
Ii. Introduction of the project
Third, the concrete realization
You can directly inherit the slidingactivity,slidingfragmentactivity,slidinglistactivity of the app in this package to achieve this effect, or initialize the Slidingmenu directly in the project, and used after configuration.
1. Initialize Slidingmenu in Project
Slidingmenu menu = new Slidingmenu (this); Menu.setmode (slidingmenu.left); Menu.settouchmodeabove (slidingmenu.touchmode_fullscreen); Menu.setshadowwidthres (r.dimen.shadow_width); Menu.setshadowdrawable (R.drawable.shadow); Menu.setbehindoffsetres (r.dimen.slidingmenu_offset); Menu.setfadedegree (0.35f); Menu.attachtoactivity (this, slidingmenu.sliding_content); <span style= "White-space:pre" ></span> Menu.setmenu (R.layout.menu);
This can be done before SetMenu ():
View view = Layoutinflater.from (this). Inflate (R.layout.menu, NULL);
Menu.setmenu (view);
This allows you to manipulate the controls in the menu layout directly.
2, the realization of slidingfragmentactivitythe main class inherits Slidingfragmentactivity. because the slidingfragmentactivity has integrated slidingmenu, it is possible to get a Slidingmenu object through Getslidingmenu (), You can then set the common properties like the following code to be able to l
<span style= "White-space:pre" ></span>//set the layout file of the container that holds the skid bar Setbehindcontentview (r.layout.menu);// Fill the fragment class of the skid bar into the layout file of the container for the skid bar fragmenttransaction transaction = Getsupportfragmentmanager (). BeginTransaction (); Fragment Fragment = new Menufragment () Transaction.replace (r.id.menu_fragment, Fragment); Transaction.commit (); Slidingmenu sm = Getslidingmenu () sm.setshadowwidth (0) sm.setshadowdrawable (r.drawable.shadow);//Set the slide bar to fully expand, Distance from the other side, unit px, the larger the setting, the smaller the width of the slide bar sm.setbehindoffset (100);//Set the degree of the gradient, the range is 0-1.0f, the larger the setting, then the slide bar just when the color is darker. 1.0f, the color is All Black Sm.setfadedegree (0.3f),//Set the touch mode, you can select Full screen, or the edge of the underline, or is not sm.settouchmodeabove ( Slidingmenu.touchmode_fullscreen)///Set Actionbar can follow the slide bar movement, if not, you can remove the setslidingactionbarenabled (false);
3. Double Slide BarWe can use the following code to achieve the effect of double slide bar:
<span style= "White-space:pre" ></span>//set the left side of the slide bar Getslidingmenu (). SetMode (Slidingmenu.left_right); Getslidingmenu (). Settouchmodeabove (Slidingmenu.touchmode_fullscreen); Setcontentview (R.layout.content_frame); Getsupportfragmentmanager (). BeginTransaction (). Replace (R.id.content_frame, New Samplelistfragment ()). commit ();// Set the slide bar on the right Getslidingmenu (). Setsecondarymenu (R.layout.menu_frame_two); Getslidingmenu (). setsecondaryshadowdrawable (r.drawable.shadowright); Getsupportfragmentmanager (). BeginTransaction (). Replace (R.id.menu_frame_two, new Samplelistfragment ()). commit ();
4. Set Touch mode
by Getslidingmenu (). Settouchmodeabove () You can set the touch mode of the slide bar with the following 3 constant values:
Slidingmenu.touchmode_fullscreen Full-screen mode, full-screen swipe to open
Slidingmenu.touchmode_margin side mode, slide only on the side of the screen to open, center slide not open
Slidingmenu.touchmode_none No Touch mode, can not be opened by touch, only through Slidingmenu (). Toggle () Open or close
5. Set the slide-bar display animationthe Slidingmenu.setbehindcanvastransformer (Canvastransformer) method allows you to set the display animation for the slide bar, which is an Canvastransformer object. Here are the settings for several common animations:
Collapse Animation:
New Canvastransformer () {@Overridepublic void Transformcanvas (canvas canvas, float percentopen) {Canvas.scale ( Percentopen, 1, 0, 0);}}
To scale an animation:
New Canvastransformer () {@Overridepublic void Transformcanvas (canvas canvas, float percentopen) {Float scale = (float) (PE rcentopen*0.25 + 0.75); Canvas.scale (scale, scale, canvas.getwidth ()/2, Canvas.getheight ()/2);}
Ascent Animation:
private static Interpolator Interp = new Interpolator () {@Overridepublic float getinterpolation (float t) {t-= 1.0f;return T * t * + 1.0f;}}; New Canvastransformer () {@Overridepublic void Transformcanvas (canvas canvas, float percentopen) {canvas.translate (0, Canvas.getheight () * (1-interp.getinterpolation (Percentopen)));}}
Iv. problems that may arise
Use of Android Open source Slidingmenu