Extract worksapce from launcher and Slide left and right to switch the class of the screen page

Source: Internet
Author: User
Tags gety xdiff

 

You should be familiar with launcher's desktop slide. The best experience should be that you can display the desktop in different locations as your fingers slide,

It is much better than the gesture switching page implemented by viewflinger + animation ~~~~

After analyzing the workspace in the launcher, there is too much code to use (drag, hold ,,,), remove the redundant code in it to achieve the necessary sliding switching screen ....

Create a new scrolllayout class that inherits from viewgroup.

Override onmeasure and onlayout methods:

In the onmeasure method, obtain the layout method of scrolllayout (usually fill_parent), enumerate all the child views, and set their layout (fill_parent ), in this way, each subview in scrolllayout is a page that is full of screens that can be slide.

In the onlayout method, draw each sub-view horizontally. In this way, the height of the view is the same as that of the screen. The width is getchildcount ()-1 View of the screen width.

Add a scroroller to smoothly switch between pages,

Override onintercepttouchevent and ontouchevent to respond to the messages to be captured when the finger is pressed, such as the speed and distance of the stroke. Use the scrollby (int x, int y) method to get the content to be displayed when the slow sliding distance is small. Finally, when the finger is up, the system determines whether to slide one page to the left or one page to the right based on the stroke speed and span, make sure that each user operation ends with a whole sub-view.

Scrolllayout source code:

Package COM. yao_guet.test; <br/> Import android. content. context; <br/> Import android. graphics. canvas; <br/> Import android. util. attributeset; <br/> Import android. util. log; <br/> Import android. view. motionevent; <br/> Import android. view. velocitytracker; <br/> Import android. view. view; <br/> Import android. view. viewconfiguration; <br/> Import android. view. viewgroup; <br/> Import android. widget. scroller; <br />/** <Br/> * similar to the worksapce in launcher, you can slide left and right to switch the screen class <br/> * @ author Yao. <br/> * blog: http://blog.csdn.NET/Yao_GUET <br/> * Date: <br/> */<br/> public class scrolllayout extends viewgroup {<br/> Private Static final string tag = "scrolllayout"; <br/> private scroller mscroller; <br/> private velocitytracker mvelocitytracker; </P> <p> private int mcurscreen; <br/> private int mdefaultsc Reen = 0; </P> <p> Private Static final int touch_state_rest = 0; <br/> Private Static final int touch_state_scrolling = 1; </P> <p> Private Static final int snap_velocity = 600; </P> <p> private int mtouchstate = touch_state_rest; <br/> private int mtouchslop; <br/> private float mlastmotionx; <br/> private float mlastmotiony; <br/> Public scrolllayout (context, attributeset attrs) {<br/> th Is (context, attrs, 0); <br/> // todo auto-generated constructor stub <br/>}< br/> Public scrolllayout (context, attributeset attrs, int defstyle) {<br/> super (context, attrs, defstyle ); <br/> // todo auto-generated constructor stub <br/> mscroller = new scroller (context); </P> <p> mcurscreen = mdefaultscreen; <br/> mtouchslop = viewconfiguration. get (getcontext ()). getscaledtouchslop (); <br/ >}< Br/> @ override <br/> protected void onlayout (Boolean changed, int L, int T, int R, int B) {<br/> // todo auto-generated method stub <br/> If (changed) {<br/> int childleft = 0; <br/> final int childcount = getchildcount (); </P> <p> for (INT I = 0; I <childcount; I ++) {<br/> final view childview = getchildat (I); <br/> If (childview. getvisibility ()! = View. gone) {<br/> final int childwidth = childview. getmeasuredwidth (); <br/> childview. layout (childleft, 0, <br/> childleft + childwidth, childview. getmeasuredheight (); <br/> childleft + = childwidth; <br/>}< br/> @ override <br/> protected void onmeasure (INT widthmeasurespec, int heightmeasurespec) {<br/> log. E (TAG, "onmeasure"); <br/> super. onmeasure (widthmeasurespec, he Ightmeasurespec); </P> <p> final int width = measurespec. getsize (widthmeasurespec); <br/> final int widthmode = measurespec. getmode (widthmeasurespec); <br/> If (widthmode! = Measurespec. Exactly) {<br/> throw new illegalstateexception ("scrolllayout only canmcurscreen run at exactly mode! "); <Br/>}</P> <p> final int heightmode = measurespec. getmode (heightmeasurespec); <br/> If (heightmode! = Measurespec. Exactly) {<br/> throw new illegalstateexception ("scrolllayout only can run at exactly mode! "); <Br/>}</P> <p> // The children are given the same width and height as the scrolllayout <br/> final int COUNT = getchildcount (); <br/> for (INT I = 0; I <count; I ++) {<br/> getchildat (I ). measure (widthmeasurespec, heightmeasurespec); <br/>}< br/> // log. E (TAG, "moving to screen" + mcurscreen); <br/> scrollto (mcurscreen * width, 0 ); <br/>}</P> <p>/** <br/> * According to the position of curre NT layout <br/> * scroll to the destination Page. <br/> */<br/> Public void snaptodestination () {<br/> final int screenwidth = getwidth (); <br/> final int destscreen = (getscrollx () + screenwidth/2)/screenwidth; <br/> snaptoscreen (destscreen); <br/>}</P> <p> Public void snaptoscreen (INT whichscreen) {<br/> // get the valid layout page <br/> whichscreen = math. max (0, math. min (whichscreen, getchi Ldcount ()-1); <br/> If (getscrollx ()! = (Whichscreen * getwidth () {</P> <p> final int Delta = whichscreen * getwidth ()-getscrollx (); <br/> mscroller. startscroll (getscrollx (), 0, <br/> delta, 0, math. ABS (DELTA) * 2); <br/> mcurscreen = whichscreen; <br/> invalidate (); // redraw the layout <br/>}</P> <p> Public void settoscreen (INT whichscreen) {<br/> whichscreen = math. max (0, math. min (whichscreen, getchildcount ()-1); <br/> mcursc Reen = whichscreen; <br/> scrollto (whichscreen * getwidth (), 0); <br/>}</P> <p> Public int getcurscreen () {<br/> return mcurscreen; <br/>}</P> <p> @ override <br/> Public void computescroll () {<br/> // todo auto-generated method stub <br/> If (mscroller. computescroloffset () {<br/> scrollto (mscroller. getcurrx (), mscroller. getcurry (); <br/> postinvalidate (); <br/>}< br/> @ override <br/> Public Boolean ontouchevent (motionevent event) {<br/> // todo auto-generated method stub </P> <p> If (mvelocitytracker = NULL) {<br/> mvelocitytracker = velocitytracker. obtain (); <br/>}< br/> mvelocitytracker. addmovement (event); </P> <p> final int action = event. getaction (); <br/> final float x = event. getx (); <br/> final float y = event. gety (); </P> <p> switch (Action) {<br/> case motionevent. Action_down: <br/> log. E (TAG, "event down! "); <Br/> If (! Mscroller. isfinished () {<br/> mscroller. abortanimation (); <br/>}< br/> mlastmotionx = x; <br/> break; </P> <p> case motionevent. action_move: <br/> int deltax = (INT) (mlastmotionx-x); <br/> mlastmotionx = x; </P> <p> scrollby (deltax, 0 ); <br/> break; </P> <p> case motionevent. action_up: <br/> log. E (TAG, "Event: Up"); <br/> // If (mtouchstate = touch_state_scrolling) {<br/> final velocitytracker Velocitytracker = mvelocitytracker; <br/> velocitytracker. computecurrentvelocity (1000); <br/> int velocityx = (INT) velocitytracker. getxvelocity (); <br/> log. E (TAG, "velocityx:" + velocityx); </P> <p> If (velocityx> snap_velocity & mcurscreen> 0) {<br/> // fling enough to move left <br/> log. E (TAG, "snap left"); <br/> snaptoscreen (mcurscreen-1); <br/>} else if (velocityx <-snap_velocity <br/> & Mcurscreen <getchildcount ()-1) {<br/> // fling enough to move right <br/> log. E (TAG, "snap right"); <br/> snaptoscreen (mcurscreen + 1); <br/>}else {<br/> snaptodestination (); <br/>}< br/> If (mvelocitytracker! = NULL) {<br/> mvelocitytracker. recycle (); <br/> mvelocitytracker = NULL; <br/>}< br/> //} <br/> mtouchstate = touch_state_rest; <br/> break; <br/> case motionevent. action_cancel: <br/> mtouchstate = touch_state_rest; <br/> break; <br/>}</P> <p> return true; <br/>}< br/> @ override <br/> Public Boolean onintercepttouchevent (motionevent eV) {<br/> // todo auto-generated method stub <br/> log. E (Tag, "onintercepttouchevent-slop:" + mtouchslop); </P> <p> final int action = eV. getaction (); <br/> If (Action = motionevent. action_move) & <br/> (mtouchstate! = Touch_state_rest) {<br/> return true; <br/>}</P> <p> final float x = eV. getx (); <br/> final float y = eV. gety (); </P> <p> switch (Action) {<br/> case motionevent. action_move: <br/> final int xdiff = (INT) math. ABS (mlastmotionx-x); <br/> If (xdiff> mtouchslop) {<br/> mtouchstate = touch_state_scrolling; </P> <p >}< br/> break; </P> <p> case motionevent. action_down: <br/> mlastmotionx = x; <br/> ML Astmotiony = y; <br/> mtouchstate = mscroller. isfinished ()? Touch_state_rest: touch_state_scrolling; <br/> break; </P> <p> case motionevent. action_cancel: <br/> case motionevent. action_up: <br/> mtouchstate = touch_state_rest; <br/> break; <br/>}</P> <p> return mtouchstate! = Touch_state_rest; <br/>}</P> <p>} 

Test Program layout:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <COM. yao_guet.test.scrolllayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Id = "@ + ID/scrolllayouttest" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <linearlayout <br/> Android: background = "# ff00" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </linearlayout> </P> <p> <framelayout <br/> Android: Background = "# f0f0" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </framelayout> </P> <p> <framelayout <br/> Android: background = "# f00f" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> </framelayout> </P> <p> <linearlayout <br/> Android: background = "# ff00" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <button <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "button1"/> <br/> </linearlayout> <br/> <linearlayout <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> <button <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "button2"/> <br/> </linearlayout> <br/> </COM. yao_guet.test.scrolllayout> 

 

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.