Android realizes NetEase News client sideslip menu (2) _android

Source: Internet
Author: User

I have already talked about this effect through the three-party open Source Library Slidemenu, please refer to Android to realize NetEase news client sideslip menu (i)

This feature is implemented today by customizing view.

The code is as follows:

Slidemenu.java

Package Com.jackie.slidemenu.view; 
Import Android.content.Context; 
Import Android.graphics.Canvas; 
Import Android.util.AttributeSet; 
Import android.view.MotionEvent; 
Import Android.view.View; 
Import android.view.ViewConfiguration; 
Import Android.view.ViewGroup; 
 
Import Android.widget.Scroller;  public class Slidemenu extends ViewGroup {private int mmostrecentx;  Offset of last X axis private final int menu_screen = 0;  Menu Interface Private final int main_screen = 1;  Main interface private int mcurrentscreen = Main_screen; 
 
 The current screen shows the main interface private scroller Mscroller; 
 
 private int touchslop; 
  Public Slidemenu (context, AttributeSet attrs) {Super (context, attrs); 
   
  Mscroller = new Scroller (context); 
 Touchslop = Viewconfiguration.get (context). Getscaledtouchslop (); 
  /** * Measures the width and height of all sub layouts/@Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) { 
   
  Super.onmeasure (Widthmeasurespec, Heightmeasurespec); Measureview(Widthmeasurespec, Heightmeasurespec); /** * Measure the width and height of all the child layouts * @param widthmeasurespec The parent layout is the ViewGroup breadth measurement specification * @param heightmeasurespec parent layout is VIEWGR  OUP Height Measurement specification */private void Measureview (int widthmeasurespec, int heightmeasurespec) {//Measurement menu width and height View menuview 
  = Getchildat (0); 
   
  Menuview.measure (Menuview.getlayoutparams (). width, heightmeasurespec); 
  Measurement of the main interface width and height View mainview = getchildat (1);  Mainview.measure (Widthmeasurespec, Heightmeasurespec); 
  The width and height of the main interface are the same as that of the parent control ViewGroup} @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) { 
  Location of the Layout menu View menuview = getchildat (0); 
   
  Menuview.layout (-menuview.getmeasuredwidth (), 0, 0, b); 
  Placement of the main interface View Mainview = Getchildat (1); 
 Mainview.layout (0, 0, R, b); @Override public boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.act 
   Ion_down:mmostrecentx = (int) event.getx (); 
  Break Case MotionEvent.action_move://Latest x-axis offset int moveX = (int) event.getx (); 
    
   increment value int deltax = Mmostrecentx-movex; 
    
   Assigns the newest X axis offset to the member variable mmostrecentx = MoveX; 
    
   Gets the offset int newscrollx = GETSCROLLX () + DeltaX after the x axis is moved; if (Newscrollx <-getchildat (0). GetWidth ()) {//the current screen x axis offset exceeds the left edge of the menu//back to the left edge of the menu Scrollto (-getchildat (0). GE 
   Twidth (), 0); 
   else if (Newscrollx > 0) {/////The right edge of the main interface//return to the right boundary of the main interface Scrollto (0, 0); 
   else {Scrollby (deltax, 0); 
  } break;  Case MotionEvent.ACTION_UP:int scrollx = Getscrollx ();  X Axis latest offset int menuxcenter =-getchildat (0). GetWidth ()/2; 
   The center point of the menu X axis if (Scrollx > Menuxcenter) {//switch to the main interface mcurrentscreen = Main_screen; 
   else {//Switch to menu interface mcurrentscreen = Menu_screen; 
   } switchscreen (); 
  Break 
  Default:break; 
 return true; /** * According to Mcurrentscreen switch screen/private void Switchscreen () {int scrollX = Getscrollx (); 
   
  Current x-axis offset int dx = 0; 
   if (Mcurrentscreen = = Main_screen) {//switch to main interface//scrollto (0, 0); 
  DX = 0-SCROLLX; 
   else if (Mcurrentscreen = = Menu_screen) {//Switch to menu interface//Scrollto (-getchildat (0). GetWidth (), 0); 
  DX =-getchildat (0). GetWidth ()-scrollx; 
   
  Mscroller.startscroll (scrollx, 0, DX, 0, Math.Abs (DX) * 5);  Invalidate (); Invalidate-> drawchild-> child.draw-> computescroll}/** * Invalidate start this method to update the x-axis offset of the screen * * 
   Override public void Computescroll () {if (Mscroller.computescrolloffset ()) {////To determine if the data is being simulated, true is in progress false data simulation is complete 
    
   Scrollto (Mscroller.getcurrx (), 0);  Invalidate (); The call that caused Computescroll}/** * shows the menu * @return * * public boolean Isshowmenu () {return MCURRENTSC 
 Reen = = Menu_screen; 
  }/** * Hidden menu */public void Hidemenu () {mcurrentscreen = Main_screen; 
 Switchscreen (); 
/** * Show Menu */public void ShowMenu () {  Mcurrentscreen = Menu_screen; 
 Switchscreen (); /** * The method to intercept the event * * @Override public boolean onintercepttouchevent (motionevent ev) {switch ev.getaction ( 
   ) {Case MotionEvent.ACTION_DOWN:mMostRecentX = (int) ev.getx (); 
  Break 
   Case MotionEvent.ACTION_MOVE:int DIFFX = (int) (EV.GETX ()-mmostrecentx); 
   if (Math.Abs (DIFFX) > Touchslop) {return true; 
  } break; 
  Default:break; 
 return super.onintercepttouchevent (EV);  } 
  
}

Mainactivity.java

Package com.jackie.slidemenu; 
 
Import Com.jackie.slidemenu.view.SlideMenu; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import Android.view.Menu; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.view.Window; 
Import Android.widget.TextView; 
 
Import Android.widget.Toast; 
 
 public class Mainactivity extends activity implements Onclicklistener {private Slidemenu mslidemenu; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  To remove the title, you need to call Requestwindowfeature (Window.feature_no_title) before Setcontentview; 
   
   
  Setcontentview (R.layout.activity_main); 
  Mslidemenu = (slidemenu) Findviewbyid (R.id.slidemenu); 
   
   
 Findviewbyid (R.id.iv_slidemenu_main_back). Setonclicklistener (this); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds the items to the action Ba 
  R if it is present. Getmenuinflater (). Inflate (R.menu.main, menu); 
 return true; 
  @Override public void OnClick (View v) {if (Mslidemenu.isshowmenu ()) {Mslidemenu.hidemenu (); 
  else {mslidemenu.showmenu (); 
  } public void Click (View v) {TextView TV = (TextView) v; 
 Toast.maketext (this, Tv.gettext (), 0). Show ();
 } 
}

Series of articles:

Android realizes NetEase News client effect

Android realizes NetEase News client sideslip menu (1)

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.