High imitation qqhd mini sliding menu bar Effect

Source: Internet
Author: User
Tags xdiff

First, let's take a look at several:

1. When you enter the program, the intermediate menu is displayed, that is, the list of recent sessions of qqhd mini.

2. slide to the left, or click the icon in the upper left corner to expand the left menu bar, that is, the Friends List of qqhd Mini, with animation effect.

3. slide to the right or click the icon in the upper-right corner to expand the menu on the right, that is, the settings of the qqhd Mini. Here I put a custom view as an example.

Let's take a look at the source code of the most important custom View:

/*** You can switch between left and right of the linearlayout ** @ author way **/public class centerlayout extends linearlayout {private final static string tag = "centerlayout "; public static final int left = 0x001; // currently, the left menu bar public static final int right = 0x002 is displayed; // currently, the right menu bar is public static final int middle = 0x000; // currently, the Center Main Interface private int mcurstate = middle is displayed; // The currently displayed viewpublic final int menu_border_width = 50; // sidebar width private scroller m Scroller; private linearlayout leftlayout, rightlayout, childlayout; private context; private Boolean fling; private Boolean misbeingdragged = false; private int mtouchslop;/*** position of the last motion event. */private float mlastmotionx, mlastmotiony;/*** ID of the active pointer. this is used to retain consistency during * drags/flings if multiple pointers are used. */private int mactiv Epointerid = invalid_pointer;/*** sentinel value for no current active pointer. used by * {@ link # mactivepointerid }. */Private Static final int invalid_pointer =-1; int menuwidth = 0; int movewidth = 0; // three constructors public centerlayout (context, attributeset attrs) {super (context, attrs); initview (context);} public centerlayout (context) {super (context); initview (context);} public scroller get Scroller () {return mscroller;} // initialize viewpublic void initview (context) {This. context = context; this. menuwidth = menu_border_width; this. mscroller = new scroller (context, animationutils. loadinterpolator (context, android. r. anim. overshoot_interpolator); Final viewconfiguration configuration = viewconfiguration. get (context); mtouchslop = configuration. getscaledtouchslop (); mcurstate = middle; // initial Convert the default status}/*** get the screen width ** @ Param context * @ return */private int getviewwidthinpix (context) {int viewwidthinpix =-1; if (viewwidthinpix =-1) {windowmanager manager = (windowmanager) context. getsystemservice (context. window_service); viewwidthinpix = manager. getdefadisplay display (). getwidth ();} return viewwidthinpix;} @ overrideprotected void onlayout (Boolean changed, int left, int top, int right, int Bottom) {super. onlayout (changed, left, top, right, bottom); For (INT I = 0; I <getchildcount (); I ++) {view child = getchildat (I ); child. layout (child. getleft () + movewidth, child. gettop (), child. getright () + movewidth, child. getbottom () ;}}@ overridepublic void computescroll () {If (mscroller. computescroloffset () {scrollto (mscroller. getcurrx (), 0); postinvalidate () ;}@overridepublic Boolean oninterce Pttouchevent (motionevent eV) {// todo auto-generated method stub // log. I (TAG, "onintercepttouchevent ------>" + eV. getaction (); Final int action = eV. getaction (); If (Action = motionevent. action_move) & (misbeingdragged) {return true; // The interception is not passed to the Child view} switch (Action & motionevent. action_mask) {Case motionevent. action_down: {final float x = eV. getx (); Final float y = eV. gety (); If (! Inchild (INT) x, (INT) y) {misbeingdragged = false; break; // exceeds the boundary, return false passed to the sub-view for processing}/** remember location of down touch. action_down always refers to * pointer index 0. */mlastmotionx = x; mlastmotiony = y; mactivepointerid = eV. getpointerid (0);/** if being flinged and user touches the screen, initiate drag; * Otherwise don't. mscroller. isfinished shoshould be false when being * flinged. */misbei Ngdragged =! Mscroller. isfinished (); break;} case motionevent. action_move: {/** misbeingdragged = false, otherwise the shortcut wowould have * caught it. check whether the user has moved far enough from his * Original down touch. * // ** locally do absolute value. mlastmotiony is set to the Y value of * The down event. */FINAL int activepointerid = mactivepointerid; If (activepointerid = invalid_pointer) {// if we Don't have a valid, the touch down wasn' t on // content. break;} final int pointerindex = eV. findpointerindex (activepointerid); Final float x = eV. getx (pointerindex); Final float y = eV. gety (pointerindex); Final int xdiff = (INT) math. ABS (X-mlastmotionx); Final int ydiff = (INT) math. ABS (Y-mlastmotiony); If (xdiff> mtouchslop & ydiff <xdiff) {misbeingdragged = true;} break;} case motionevent. acti On_cancel: Case motionevent. action_up: misbeingdragged = false; mactivepointerid = invalid_pointer; scrolltoscreen (); break;} return misbeingdragged;} @ overridepublic Boolean ontouchevent (motionevent event) {// log. I (TAG, "ontouchevent ---- >>>>" + event. getaction (); If (event. getaction () = motionevent. action_down &&! Inchild (INT) event. getx (), (INT) event. gety () {// don't handle edge touches immediately -- they may actually belong // to one of our // descendants. return false;} switch (event. getaction () & motionevent. action_mask) {Case motionevent. action_down: Return true; // This view can digest case motionevent. action_move: Final int activepointerindex = event. findpointerindex (mactivepointerid); Final float x = event. getx (Act Ivepointerindex); Final float y = event. gety (activepointerindex); Final int distancex = (INT)/* Math. ABS */-(x-mlastmotionx); // The New brotherview needs to be displayed during the sliding process. Otherwise, the previous brotherview is displayed, when the new brotherview is released, the new brotherview is suddenly changed, affecting the experience of IF (distancex <0 & getscrollx () <0 & leftlayout! = NULL) {setbrothervisibility (left);} else if (distancex> 0 & getscrollx ()> 0 & rightlayout! = NULL) {setbrothervisibility (right);} else {setbrothervisibility (middle);} scrollby (INT) distancex, 0); mlastmotionx = x; mlastmotiony = y; break; case motionevent. action_up: misbeingdragged = false; mactivepointerid = invalid_pointer; scrolltoscreen (); break; default: return Super. ontouchevent (event);} return misbeingdragged;} @ overrideprotected void onscrollchanged (int l, int T, int oldl, int oldt) {// todo Auto-generated method stubsuper. onscrollchanged (L, t, oldl, oldt);} private void scrolltoscreen () {int scrolldistance = 0; If (math. ABS (getscrollx ()> getwidth ()/2) scrolldistance = (getscrollx ()> 0 )? Getwidth ()-menuwidth-getscrollx ():-(getwidth ()-menuwidth-math. ABS (getscrollx (); elsescrolldistance =-getscrollx (); int distance = scrolldistance + getscrollx (); log. I (TAG, "distance =" + distance); If (distance> 0) {mcurstate = right;} else if (distance <0) {mcurstate = left ;} else {mcurstate = middle;} mscroller. startscroll (getscrollx (), 0, scrolldistance, 0, math. ABS (scrolldistance )* 2); invalidate ();} public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {If (math. ABS (velocityx)> viewconfiguration. get (context ). getscaledminimumflingvelocity () {fling = true; scrolltoscreen ();} return fling;} private Boolean inchild (int x, int y) {If (getchildcount ()> 0) {final int scrollx = mscroller. getcurrx (); final view child = getchildat (0); return! (Scrollx + x <0 | scrollx + x> getwidth () | Y <0 | Y> getheight ();} return false ;} /*** set the currently displayed view ** @ Param whichpg */Public void setpage (INT whichpg) {int targetx = 0, movedistance = 0; If (whichpg = left) {targetx =-(getviewwidthinpix (context)-menuwidth); mcurstate = left;} else if (whichpg = right) {targetx = getviewwidthinpix (context)-menuwidth; mcurstate = right;} else {mcurstate = middle;} setbrothervisibility (whichpg); movedistance = targetx-getscrollx (); mscroller. startscroll (getscrollx (), 0, movedistance, 0, math. ABS (movedistance) * 2); invalidate ();}/*** returns the currently displayed view ** @ return */Public int getpage () {return mcurstate ;} public void addchildview (view child) {This. childlayout. addview (child);}/*** set brotherview ** @ Param left * @ Param right */Public void setbrotherlayout (linearlayout left, linearlayout right) {This. leftlayout = left; this. rightlayout = right;}/*** display or hide view based on the current status ** @ Param state */private void setbrothervisibility (INT state) {Switch (state) {case left: rightlayout. setvisibility (view. gone); leftlayout. setvisibility (view. visible); break; case right: rightlayout. setvisibility (view. visible); leftlayout. setvisibility (view. gone); break; case middle: break; default: break ;}}}

Okay, Source Code address: http://download.csdn.net/detail/weidi1989/5320596

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.