Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
In order to move the finger to the left or right on the mobile phone, this article can be moved about two views accordingly ( Source Download )
First look at the effect:
first, the realization of ideas1. Ideas
The menu is left, content is on the right, and the menu is displayed with a small amount of space on the right border of the phone. When the content is all displayed, the menu is not visible. As in the following two figures
Show content
Show Menu
2. Judgment Logic
This is whether you want to show or hide the menu when your finger is lifted by the screen and finger.
Second, the code list
First look at the layout:
< LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/ Tools "Android:id=" @+id/layout "android:layout_width=" match_parent "android:layout_height=" Match_parent "Andro id:orientation= "Horizontal" tools:context= ". Mainactivity "> <linearlayout android:id=" @+id/menu "android:layout_width=" Match_parent "and roid:layout_height= "match_parent" android:orientation= "horizontal" android:background= "@drawable/pn" > </LinearLayout> <linearlayout android:id= "@+id/content" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "horizontal" android:background= "@drawable/sn" & Gt </LinearLayout> </linearlayout>
Next look at the code
Package Com.example.learningjava;import Com.example.learningjava.r.string;import Android. R.integer;import Android. R.menu;import Android.os.asynctask;import Android.os.build;import Android.os.bundle;import Android.annotation.suppresslint;import Android.annotation.targetapi;import Android.widget.linearlayout.layoutparams;import Android.app.activity;import Android.util.displaymetrics;import Android.util.log;import Android.view.menu;import Android.view.motionevent;import Android.view.VelocityTracker; Import Android.view.view;import Android.view.view.ontouchlistener;import Android.view.window;import Android.widget.linearlayout;public class Mainactivity extends Activity implements Ontouchlistener{private LinearLayout menulayout;//menu item Private linearlayout contentlayout;//content Item Private Layoutparams menuparams;// menu item Parameters Private Layoutparams contentparams;//Content Item parameter contentlayout width value private int displaywidth;//phone screen resolution private float xdown;//finger point down the horizontal axis private float xmove;//finger movement of the horizontal axis private float The xup;//records the Velocitytracker mvelocitytracker of the horizontal axis after the finger is lifted; Used to calculate the speed of finger slippage. Float velocityx;//finger move around speed public static final int snap_velocity = 400; When scrolling to show and hide the menu, the speed at which your finger slides needs to be reached. Private Boolean menuisshow = false;//Initialization menu item is not phoenixes private static final int menupadding=80;//menu complete display, left to content width protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Initlayoutparams (); }/** * Initialize layout and set its corresponding parameters */private void Initlayoutparams () {//Get screen size displaymetrics dm = new Displaymetric S (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); Displaywidth =dm.widthpixels; Get Control Menulayout = (linearlayout) Findviewbyid (R.id.menu); Contentlayout = (linearlayout) Findviewbyid (r.id.content); Findviewbyid (r.id.layout). Setontouchlistener (this); Get Control Parameters MenuparaMs= (Linearlayout.layoutparams) menulayout.getlayoutparams (); Contentparams = (linearlayout.layoutparams) contentlayout.getlayoutparams (); Initialize menu and content width and margin menuparams.width = displaywidth-menupadding; Menuparams.leftmargin = 0-menuparams.width; Contentparams.width = Displaywidth; contentparams.leftmargin=0; Set parameter Menulayout.setlayoutparams (MENUPARAMS); Contentlayout.setlayoutparams (Contentparams); } @Override public boolean OnTouch (View V, motionevent event) {Acquirevelocitytracker (event); Switch (event.getaction ()) {case motionevent.action_down:xdown=event.getrawx (); Break Case MOTIONEVENT.ACTION_MOVE:XMOVE=EVENT.GETRAWX (); Isscrolltoshowmenu (); Break Case MOTIONEVENT.ACTION_UP:XUP=EVENT.GETRAWX (); Isshowmenu (); Releasevelocitytracker (); Break Case Motionevent.action_cancel: Releasevelocitytracker (); Break } return true; /** * Depending on the distance the finger presses, determines whether to scroll the menu */private void Isscrolltoshowmenu () {int Distancex = (int) (xmove-xdown); if (!menuisshow) {scrolltoshowmenu (DISTANCEX); }else{Scrolltohidemenu (DISTANCEX); }}/** * After lifting the finger to determine whether to display the menu */private void Isshowmenu () {Velocityx =getscrollvelocity (); if (Wanttoshowmenu ()) {if (Shouldshowmenu ()) {ShowMenu (); }else{Hidemenu (); }} else if (Wanttohidemenu ()) {if (Shouldhidemenu ()) {Hidemenu (); }else{ShowMenu (); }}}/** * Want to show menu when move right distance greater than 0 and menu not visible */private Boolean wanttoshowmenu () {return!menuisshow&&xup -xdown>0; /** * Want to hide the menu when the left move distance is greater than 0 and the menu is visible */private Boolean wanttohidemenu () {return menuisshow&&xdown-xup>0; }/** * Determines that the menu should be displayed when the distance to the right is more than half of the menu or the speed exceeds the given value */private Boolean shouldshowmenu () {return xup-xdown>menuparams.width/2| | velocityx>snap_velocity; }/** * Determines that the menu should be hidden when the left-moving distance exceeds half of the menu or the speed exceeds the given value */private Boolean shouldhidemenu () {return xdown-xup>menuparams.width/ 2| | velocityx>snap_velocity; }/** * Displays the menu bar */private void ShowMenu () {new Showmenuasynctask (). Execute (50); Menuisshow=true; }/** * Hides the menu bar */private void Hidemenu () {new Showmenuasynctask (). Execute (-50); Menuisshow=false; /** * When the pointer is pressed, scrolling will slowly show the menu * @param scrollx the distance each scroll moves */private void Scrolltoshowmenu (int scrollx) {if (scrollx> ; 0&&scrollx<= menuparams.width) Menuparams.leftmargin =-menuparams.width+scrollx; Menulayout.setlayoutparams (Menuparams); /** * When the pointer is pressed, the scroll will slowly hide the menu * @param scrollx the distance each scroll moves * * * private void Scrolltohidemenu (int scrollx) {if (scrollx> ; =-menuparams.width&&scrollx<0) Menuparams.leftmargin=scrollx; Menulayout.setlayoutparams (Menuparams); }/** * Create a Velocitytracker object and add sliding events that touch the content interface to the VELOCIAmong the Tytracker. * @param event adds motionevent */private void Acquirevelocitytracker (final motionevent event) to Velocitytracker { if (null = = Mvelocitytracker) {Mvelocitytracker = Velocitytracker.obtain (); } mvelocitytracker.addmovement (event); /** * Gets the speed at which the finger slides in the content interface. * @return sliding speed, in units of how many pixels are moved per second. */private int getscrollvelocity () {mvelocitytracker.computecurrentvelocity (1000); int velocity = (int) mvelocitytracker.getxvelocity (); return math.abs (velocity); }/** * Releases Velocitytracker */private void Releasevelocitytracker () {if (null! = Mvelocitytracker) { Mvelocitytracker.clear (); Mvelocitytracker.recycle (); Mvelocitytracker = null; }}/** *: Simulates the animation process so that the naked eye can see the effect of scrolling * */class Showmenuasynctask extends Asynctask<integer, Integer, integer> {@Override protected integer doinbackground (integer ... params) { int leftMargin = Menuparams.leftmargin; while (true) {//Scrolls the interface according to the incoming speed, and jumps out of the loop when scrolling reaches the left or right edge. LeftMargin + = Params[0]; if (Params[0] > 0 && leftMargin > 0) {leftmargin= 0; Break } else if (Params[0] < 0 && leftMargin <-menuparams.width) {Leftmargin=-menuparam S.width; Break } publishprogress (LeftMargin); try {thread.sleep (40);//sleep for the naked eye to see the scrolling effect} catch (Interruptedexception e) {E.printstacktrace (); }} return leftMargin; } @Override protected void Onprogressupdate (Integer ... value) {menuparams.leftmargin = value[0]; Menulayout.setlayoutparams (Menuparams); } @Override protected void OnPostExecute (Integer result) { Menuparams.leftmargin = result; Menulayout.setlayoutparams (Menuparams); } }}
third, effect and explanation
SOURCE download
Android Sidebar custom implementation (with source)