Show menu
Hide menu
Public class mainactivity extends activity implements ontouchlistener {
/***
* Left margin of the menu bar note: the position on the left of the menu bar and the X coordinate of the screen
**/
Private int leftedge = 0;
/***
* Right margin of the menu bar note: the position on the right of the menu bar and the X coordinate of the screen
**/
Private int rightedge = 0;
/***** Menu bar ***/
Private linearlayout menu;
/***** Main interface ***/
Private linearlayout content;
/*** Screen width ***/
Private int diswidth;
/***** Minimum display size of the main interface ***/
Public static final int padding_distance = 80;
/*** Layout parameters of the menu bar **/
Private linearlayout. layoutparams leftlayoutparams;
Private float xdown = 0;
Private float xmove = 0;
Private float xup = 0;
Private Boolean flag = false;
/*** The lowest stroke speed ***/
Public static final int velocity = 200;
/*** Speed record tracker, professional record ontouch () Click Event *****/
Private velocitytracker;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_main );
// Obtain the screen width
Displaymetrics metrics = new displaymetrics ();
This. getwindowmanager (). getdefadisplay display (). getmetrics (metrics );
Diswidth = metrics. widthpixels;
Menu = (linearlayout) findviewbyid (R. Id. Menu );
Leftlayoutparams = (layoutparams) menu. getlayoutparams ();
// Menu Bar Width
Leftlayoutparams. width = diswidth-padding_distance;
// Left margin of the menu bar
Leftedge =-leftlayoutparams. width;
// The Position of the menu bar from the X coordinate on the left of the screen
// Note: the default menu bar is invisible.
Leftlayoutparams. leftmargin = leftedge;
Log. I ("tag", "leftedge =" + leftedge + ", diswidth =" + diswidth );
// The main content interface is displayed by default.
Content = (linearlayout) findviewbyid (R. Id. content );
Content. getlayoutparams (). width = diswidth;
Content. setontouchlistener (this );
}
/***** The onfling () of gesture implements *****/
@ Override
Public Boolean ontouch (view, motionevent event ){
// Create a speed record Tracker
If (velocitytracker = NULL ){
Velocitytracker = velocitytracker. Obtain ();
}
Velocitytracker. addmovement (event );
Switch (event. getaction ()){
Case motionevent. action_down:
Xdown = event. getrawx ();
Log. I ("tag", "xdown =" + xdown );
Break;
Case motionevent. action_move:
Xmove = event. getrawx ();
Log. I ("tag", "xmove =" + xmove );
Int distance = (INT) (xmove-xdown );
If (FLAG) {// hide the display menu bar
Leftlayoutparams. leftmargin = distance;
} Else {// display the menu bar
Leftlayoutparams. leftmargin = leftedge + distance;
}
// When the menu bar is fully displayed, it is no longer moved to the right
If (leftlayoutparams. leftmargin> rightedge ){
Leftlayoutparams. leftmargin = rightedge;
// When the menu is completely hidden and the X coordinate position on the left of the screen is smaller than the width of the current menu, it is not moved
} Else if (leftlayoutparams. leftmargin <leftedge ){
Leftlayoutparams. leftmargin = leftedge;
}
// If this clause is not added, no effect is displayed.
Menu. setlayoutparams (leftlayoutparams );
Break;
Case motionevent. action_up:
Xup = event. getrawx ();
Log. I ("tag", "xup =" + xup );
// Drag to the right to display the menu (when the menu is not displayed)
If (xup-xdown> 0 &&! Flag ){
If (xup-xdown> diswidth/2
| Getvelocity ()> velocity ){
Log. I ("tag", "result =" + (xup-xdown ));
// The stroke speed is greater than 200.
// If (math. Abs (velocity)> velocity ){
// Update the UI main thread and display the menu bar
New myasynctask(cmd.exe cute (30 );
//}
// Move to the left to hide the menu bar (when the menu is displayed)
}
} Else if (xdown-xup> 0 & flag ){
If (xdown-xup + padding_distance> diswidth/2
| Getvelocity ()> velocity ){
Log. I ("tag", "result =" + (xup-xdown ));
// Int velocity = (INT) velocitytracker. getxvelocity ();
// Log. I ("tag", "Left direction: velocity =" + velocity );
// If (math. Abs (velocity)> velocity ){
// Update the UI main thread to hide the menu bar
New myasynctask(cmd.exe cute (-30 );
//}
}
}
// Record the record object
If (velocitytracker! = NULL ){
Velocitytracker. Recycle ();
Velocitytracker = NULL;
}
Break;
}
Return true;
}
Private int getvelocity (){
Velocitytracker. computecurrentvelocity (1000 );
Int velocity = (INT) velocitytracker. getxvelocity ();
Log. I ("tag", "velocity =" + velocity );
Return math. Abs (velocity );
}
/*** UI update effect ****/
Private class myasynctask extends asynctask <integer, integer, integer> {
@ Override
Protected integer doinbackground (integer... steps ){
// Change the X coordinate position on the left of the screen
Int leftmargin = leftlayoutparams. leftmargin;
While (true ){
Leftmargin = leftmargin + steps [0];
// Determine whether the menu bar is fully displayed or hidden. Exit the loop if it is fully displayed or hidden.
// Full display on the menu bar
If (leftmargin> rightedge ){
Leftmargin = rightedge;
Break;
// The menu bar is completely hidden.
} Else if (leftmargin <leftedge ){
Leftmargin = leftedge;
Break;
}
// The margin of the notification menu bar is updated.
Publishprogress (leftmargin );
// The thread sleeps for 20 milliseconds.
Try {
Thread. Sleep (20 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
If (steps [0]> 0 ){
Flag = true;
} Else {
Flag = false;
}
Return leftmargin;
}
@ Override
Protected void onprogressupdate (integer... values ){
Leftlayoutparams. leftmargin = values [0];
Menu. setlayoutparams (leftlayoutparams );
}
@ Override
Protected void onpostexecute (integer result ){
Leftlayoutparams. leftmargin = result;
Menu. setlayoutparams (leftlayoutparams );
}
}
}
: Http://download.csdn.net/detail/util_c/5735095