Mainactivity as follows:
Package Cc.cd;import Android.os.asynctask;import Android.os.bundle;import android.util.log;import Android.view.motionevent;import Android.view.velocitytracker;import Android.view.view;import Android.view.view.ontouchlistener;import Android.view.windowmanager;import Android.widget.linearlayout;import Android.app.activity;import android.content.context;/** * Demo Description: * A simple example of a side-by-side menu Slidingmenu * that is, adding slidingmenu to an Activity * * Example Description: * There are two interface menu and content in the example. The content interface is displayed when you enter an app. * If your finger slides to the right of the screen, the menu and hidden content are displayed. Conversely, the same. * Menu display and hide is mainly to modify the menu Layoutparams in the LeftMargin so as to achieve the effect. * * Steps to Organize: * 1 when initializing, by setting the LeftMargin in the menu's layoutparams to make it completely hidden. * 2 set Touch monitoring for content. * 3 in the process of move to constantly modify the LeftMargin in the menu layoutparams to show or hide according to the swipe of the * gesture * 4 Modify the LeftMargin * in the layoutparams of the menu with the asynchronous task Asynctask on up to achieve full display of the menu or hide it completely. * * The above routines are quite common. * * Reference: * 1 http://blog.csdn.net/guolin_blog/article/details/8714621 * 2 http://blog.csdn.net/hudashi/article/ details/7352157 * Thank you very much * * NOTE: * 1 examples of images used in the example are also from reference 1 * 2 to simplify the logic, the two interfaces in the example are instead of the actual view interface */public class Mainactivity extends Activity {private int screenwidth; Private View Contentview; Private View Menuview; private float Xdown; private float Xmove; private float xUp; When the menu is fully displayed, the width of the content minimum value private int contentviewminwidth = 80; Menu whether the flag bit is visible, and the value is not valid during the swipe process. This value is changed only when the menu is completely displayed or hidden after the slide has ended private Boolean ismenuvisible=false; private int menuparamsmaxleftmargin=0; private int menuparamsminleftmargin=0; Speed Tracker private Velocitytracker mvelocitytracker; Threshold public static final int velocity_threshold=200; TAG private final static String tag= "Mainactivity"; The layout of the menu Layoutparams private linearlayout.layoutparams menulayoutparams; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); init ();} private void Init () {//Get screen width WindowManager windowmanager= (windowmanager) Getsystemservice (Context.window_service); Screenwidth=windOwmanager.getdefaultdisplay (). getwidth ();//Initialize Contentviewcontentview=findviewbyid (r.id.contentlinearlayout);// Set the width of the contentview to the width of the screen contentview.getlayoutparams (). width=screenwidth;// Set Touch Monitor Contentview.setontouchlistener (New Touchlistenerimpl ());//Initialize Menuviewmenuview=findviewbyid ( R.id.menulinearlayout); menulayoutparams= (Linearlayout.layoutparams) menuview.getlayoutparams ();//Set the width of MenuView. Sets its width to the screen width minus the minimum width of Contentview menulayoutparams.width=screenwidth-contentviewminwidth;// Menuview.menuparamsminleftmargin=-menulayoutparams.width;menulayoutparams.leftmargin= was completely hidden when initialized Menuparamsminleftmargin;} /** * Detailed description of Distancex in Action_move. * During a sliding process (from finger to finger) The value of DISTANCEX is continuously increasing or getting smaller. * because int distancex= (int) (xmove-xdown); * This xdown is the coordinate value at the time of the moving, when the DISTANCEX is calculated in the process of the xdown minus the xmove of every moment. That is, the Xdown is constant during the sliding process and the xmove is constantly changing. * Code Description: * IF (ismenuvisible) {* MENULAYOUTPARAMS.LEFTMARGIN=DISTANCEX; *} else {* Menulayoutparams.leftmargin=me Nuparamsminleftmargin+distancex; *} * At the very beginning, the menu is hidden. Finger Press, slide to the right of the screen. Call: * MENULAYOUTPARAMS.LEFTMARGIN=MENUPARAMSMINLEFTMARGIN+DISTANCEX; * So this menulayoutparams.leftmargin is constantly getting bigger until 0 (notice the cross-border judgment), at which time * Menuview is fully displayed. Then the finger is pressed and slid to the left of the screen. Call: * Menulayoutparams.leftmargin=distancex; * Distancex This negative number has been decreasing, it is the value of menulayoutparams.leftmargin until * Menuview completely hidden, at this time its value is menuparamsminleftmargin (pay attention to cross judgment). * * This problem is not difficult, but in this note to prevent later response. */private class Touchlistenerimpl implements ontouchlistener{@Overridepublic boolean OnTouch (View V, motionevent event) {//Start speed Tracking Startvelocitytracker (event); switch (Event.getaction ()) {case MOTIONEVENT.ACTION_DOWN:XDOWN=EVENT.GETRAWX (); Break;case motionevent.action_move:xmove=event.getrawx (); int distancex= (int) (xmove-xdown); LOG.I (TAG, "xdown=" +xdown+ ", xmove=" +xmove+ ", distancex=" +distancex "); if (ismenuvisible) { Menulayoutparams.leftmargin=distancex;} else {Menulayoutparams.leftmargin=menuparamsminleftmargin+distancex;} Handle out-of-bounds conditions if (menulayoutparams.leftmargin<menuparamsminleftmargin) {menulayoutparams.leftmargin= Menuparamsminleftmargin;} IfMenulayoutparams.leftmargin>menuparamsmaxleftmargin) {Menulayoutparams.leftmargin=menuparamsmaxleftmargin;} Set Menuview layoutparamsmenuview.setlayoutparams (menulayoutparams); Break;case motionevent.action_up:xup= EVENT.GETRAWX ();//The gesture intent is to show Menuif (Wanttoshowmenu ()) {//To determine if Menuif (Shouldscrolltomenu ()) {Scrolltomenu () is displayed);} else { Scrolltocontent ();}} Determine the gesture intent to show Contentif (Wanttoshowcontent ()) {//To determine if the display is Contentif (Shouldscrolltocontent ()) {scrolltocontent ();} else { Scrolltomenu ();}} Stop speed tracking Stopvelocitytracker (); break;default:break;} return true;}} /** * Determine if the current gesture wants to display menu * criteria: * 1 lift coordinates greater than press coordinates * 2 menu itself not visible */private Boolean wanttoshowmenu () {return (xup-xdown>0) &A mp;& (!ismenuvisible));} /** * Determine if the menu should be fully displayed * judging condition: * Sliding distance is greater than one-second of the screen * or sliding speed is greater than the speed threshold Velocity_threshold */private boolean shouldscrolltomenu () {R Eturn ((XUP-XDOWN>SCREENWIDTH/2) | | (Getscrollvelocity () >velocity_threshold));} /** * Scroll the screen to menu. The menu will be displayed in full. * Modify the LeftMargin */private void scrolltomen in the layoutparams of the menu by changing the 30 paceU () {new Scrollasynctask (). Execute (30);} /** * Determines whether the current gesture wants to display the menu content * Judging Condition: * 1 lift coordinates less than press coordinates * 2 menu itself visible */private boolean wanttoshowcontent () {return (xup-xdown< ; 0) && (ismenuvisible));} /** * Determine if the content should be fully displayed * judging condition: * Xdown-xup+contentviewminwidth is greater than one-second * or sliding speed is greater than the speed threshold Velocity_threshold */private Boolean shouldscrolltocontent () {return (XDOWN-XUP+CONTENTVIEWMINWIDTH>SCREENWIDTH/2) | | (Getscrollvelocity () >velocity_threshold));} /** * Scrolls the screen to content. The content will be fully displayed * Follow the steps of-30 modify the Layoutparams in the menu leftmargin */private void Scrolltocontent () {New Scrollasynctask (). Execute (-30);} /** * Start speed tracking */private void Startvelocitytracker (Motionevent event) {if (mvelocitytracker==null) {mvelocitytracker= Velocitytracker.obtain (); }mvelocitytracker.addmovement (event);} /** * Gets the finger sliding speed in the x direction of the content */private int getscrollvelocity () {//sets the Velocitytracker unit. 1000 represents the pixel that is moving within 1 seconds of time Mvelocitytracker.computecurrentvelocity (1000);//Gets the pixel value that is sliding in the x direction within 1 seconds int xvelocity= (int) mvelocitytracker.getxvelocity (); return MatH.abs (xvelocity);} /** * STOP speed tracking */private void Stopvelocitytracker () {if (mvelocitytracker!=null) {mvelocitytracker.recycle (); Mvelocitytracker=null;}} /** * Use asynchronous tasks to constantly modify the LeftMargin in the menu's layoutparams to achieve a * View movement */private class Scrollasynctask extends asynctask< Integer, Integer, integer>{@Overrideprotected integer doinbackground (integer ... speed) {int leftmargin= Menulayoutparams.leftmargin;while (True) {//each change speedleftmargin=leftmargin+speed[0];//if it crosses the border, the processing is out of bounds and the loop jumps out if ( Leftmargin>menuparamsmaxleftmargin) {leftmargin=menuparamsmaxleftmargin;break;} If out of bounds, the processing is out of bounds and jumps out of the loop if (leftmargin<menuparamsminleftmargin) {leftmargin=menuparamsminleftmargin;break;} Notification Progress update publishprogress (leftMargin);//thread Sleep 25 milliseconds, easy to reflect the scrolling effect try {thread.sleep (+);} catch (Exception e) {}}// Set the flag bit Ismenuvisibleif (speed[0]>0) {ismenuvisible=true;} according to the speed of the slide. Else{ismenuvisible=false;} return leftMargin;} @Overrideprotected void Onprogressupdate (Integer ... leftMargin) {super.onprogressupdate (leftMargin); Menulayoutparams.leftmargin=leFtmargin[0];menuview.setlayoutparams (menulayoutparams);} @Overrideprotected void OnPostExecute (Integer leftMargin) {super.onpostexecute (leftMargin); Menulayoutparams.leftmargin=leftmargin;menuview.setlayoutparams (Menulayoutparams);}}}
Main.xml as follows:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "Horizontal" > <linearlayout android:id= "@+id/menulinearlayout" Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:background= "@drawable /menu " android:orientation=" vertical "/> <linearlayout android:id=" @+id/contentlinearlayout " android:layout_width=" match_parent " android:layout_height=" match_parent " android:background=" @drawable/content " android:orientation=" vertical "/></linearlayout>
Android Slide-Out menu Complete detailed example (Basic version)