If you write an Android desktop slide to toggle the screen control (i)

Source: Internet
Author: User

First, this control should be inherited ViewGroup:

Initialization

public class MyGroup extends viewgroup{private Scroller mscroller;private float morimotionx;private float Mlastmotionx; Private Velocitytracker mvelocitytracker;private int mtouchstate = touch_state_rest;private static final int touch_state _rest = 0;private int mtouchslop;private int mmaximumvelocity;private static final int touch_state_scrolling = 1;private F Loat mlastdownx;private static final int default_value = 1000;private int mnextscreen = -1;private Boolean mflaglimitup = false;private static final int snap_velocity = 700;private int mcurrentscreen;public mygroup (context context, Attributese T attrs) {Super (context, attrs); Initworkspace ();} private void Initworkspace () {Mscroller = new Scroller (GetContext ()); Setcurrentscreen (0); final viewconfiguration Configuration = Viewconfiguration.get (GetContext ()); mtouchslop = Configuration.getscaledtouchslop ();// This is the minimum pixel distance that defines the control in scroll mmaximumvelocity = Configuration.getscaledmaximumflingvelocity (); Rate, fling a value of how many pixels to slide per second}


First rewrite the onmeasure:

@Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec,  HEIGHTMEASURESPEC); final int width = measurespec.getsize (widthmeasurespec); final int count = Getchildcount (); for (int i = 0; I < count; i++) {Getchildat (i). Measure (Widthmeasurespec, Heightmeasurespec);}}

OnLayout:

@Overrideprotected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {int paddingleft = 0;int Paddi Ngtop = 0;int Childleft = paddingleft;final int count = Getchildcount (); for (int i = 0; i < count; i++) {final View chi LD = Getchildat (i); if (child.getvisibility ()! = View.gone) {final int childwidth = Child.getmeasuredwidth (); final int Chil Dheight = Child.getmeasuredheight (); Child.layout (Childleft, paddingtop, Childleft + childwidth,childheight + Paddingtop); <strong>childleft + = Child.getmeasuredwidth ();  The distance between the left margin of the next child and the left margin of the first child is exactly the width</strong> of the first child;}}}

Then write the touch event for the view:

Onintercepttouchevent only the return false event is passed to the view in the control, that is, the view's Ontouch event can capture the ontouchevent in view to return true in order to perform multiple touch events. Events to be delivered.

@Overridepublic boolean onintercepttouchevent (motionevent ev) {final int action = Ev.getaction ();//If it is a move event, Mtouchstate is touch_state_rest as a stationary state, this is to prevent the child controls from sliding and using their fingers to slip, in which case the event does not respond if (action = = motionevent.action_move) & & (Mtouchstate! = touch_state_rest)) {return true;} Final float x = ev.getx (); switch (action) {case MotionEvent.ACTION_MOVE:final int xdiff = (int) math.abs (X-mlastmotionx) ; final int touchslop = Mtouchslop;boolean xmoved = xdiff > touchslop;//If xmoved is true indicates that the finger is sliding if (xmoved) {mtouchstate = T ouch_state_scrolling;} Break;case MotionEvent.ACTION_DOWN:mLastMotionX = x;//mscroller.isfinished () is true to indicate that the slide is over, when we set the status to Touch_state_ Restmtouchstate = mscroller.isfinished ()? Touch_state_rest:touch_state_scrolling;break;case MotionEvent.ACTION_CANCEL:case motionevent.action_up: Mtouchstate = Touch_state_rest;break;default:break;} If it is not in a stationary state, all returns true so that the event is not passed to ontouchevent with return mtouchstate! = touch_state_rest;}

The reason to return true when sliding is that there is no need to respond to the Ontouch event inside the control

@Overridepublic boolean ontouchevent (motionevent ev) {if (Mvelocitytracker = = null) {Mvelocitytracker = Velocitytracker.obtain ();} Mvelocitytracker.addmovement (EV); int mscrollx = THIS.GETSCROLLX (); MSCROLLX represents the distance on the x-axis, sliding left to positive, this time the screen moves to the right final int action = Ev.getaction (), final float x = Ev.getx (), final float y = ev.gety (); Switch (action) {case MotionEvent.ACTION_DOWN:mOriMotionX = X;mlastmotionx = X;if (!mscroller.isfinished ()) { Mscroller.abortanimation ();} Morimotionx = X;mlastmotionx = X;mlastdownx = X;return true;case MotionEvent.ACTION_MOVE:System.out.println ("= = = Action Move mscrollx= "+MSCROLLX); final int buffer = getwidth ()/2; This means that on the first page or the last page can also slide half the screen//if it is sliding backwards, the screen forward, then Mlastmotionx is larger than X, deltax is positive int deltax = (int) (MLASTMOTIONX-X); Mlastmotionx = x; System.out.println ("=====deltax=" +deltax);//deltax<0 means slide forward if (DeltaX < 0) {//This is to the right, the screen moves to the left Scrollby (Math.max (-mscrollx-buffer, DeltaX), 0);} Else{int Availabletoscroll = 0;if (Getchildcount () > 0) {//At this time workspace may not add any Item,count = = 0system.out.println ("====rihgt=" + (Getchildat (Getchildcount ()-1). GetRight ()) + "avail=" + (ge Tchildat (Getchildcount ()-1). GetRight ()-mscrollx-getwidth ()); Getchildat (Getchildcount ()-1). GetRight () for all the view plus the width, here add 3 view, a view of 1080, then this value is 3240 availabletoscroll =     Getchildat (Getchildcount ()-1). GetRight ()-mscrollx-getwidth (); Availabletoscroll + buffer can slide the maximum distance, DeltaX is the sliding distance Scrollby (math.min (availabletoscroll + buffer, deltax), 0);}} return true;case MotionEvent.ACTION_UP:final velocitytracker velocitytracker = Mvelocitytracker; Velocitytracker.computecurrentvelocity (default_value,mmaximumvelocity); int velocityx = (int) Velocitytracker.getxvelocity ();//velocityx is the rate at which the finger slips, we compare snap_velocity with the given value if (Velocityx > Snap_velocity & & Mcurrentscreen > 0) {//This time the finger is sliding forward, the screen is moving backwards snaptoscreen (mCurrentScreen-1);} else if (velOcityx <-snap_velocity&& Mcurrentscreen < Getchildcount ()-1) {//move rightsnaptoscreen (Mcurrentscreen + 1);} else {snaptodestination (Mlastmotionx < Morimotionx);} if (mvelocitytracker! = null) {mvelocitytracker.recycle (); mvelocitytracker = null;} Mtouchstate = Touch_state_rest;if (Math.Abs (mlastdownx-x) >) {return true;} return false;case MotionEvent.ACTION_CANCEL:mTouchState = Touch_state_rest;return false;default:break;} return true;}

/** Slide the distance, a few minutes away from the screen, you start to perform a screen change action. *//** * snaptodestination. * Mlastmotionx < Morimotionx (Mlastmotion < Morimotionx) indicates that this is the hand sliding backwards, but the screen is forward and vice versa * forward is true for moving forward, At this point Scrollx plus two-thirds of the screen width * scrollx/screenwidth To determine the current screen * @param forward is forward or backward. */public void Snaptodestination (Boolean forward) {final int screenwidth = getwidth (); int scrollx = GETSCROLLX (); if (Forwar D) {scrollx + = SCREENWIDTH-SCREENWIDTH/3;} else {scrollx + = SCREENWIDTH/3;} System.out.println ("======screenwidth=" +screenwidth+ "scrollx/screenwidth=" + (scrollx/screenwidth)); Snaptoscreen (scrollx/screenwidth);} /** * If calculating the distance to slide: (Whichscreen * getwidth ()) is the sliding x-coordinate, THIS.GETSCROLLX () is the current coordinate, and the two are subtracted from the sliding distance * Math.Abs (Delta) * 2 is the duration of the slide * * public void Snaptoscreen (int whichscreen) {whichscreen = Math.max (0, Math.min (Whichscreen, Getchildcount ()-1)); Boolean  Changingscreens = whichscreen! = Mcurrentscreen;mnextscreen = Whichscreen;int mscrollx = This.getScrollX (); final int NewX = Whichscreen * GetWidth (); final int DeltA = NEWX-MSCROLLX; System.out.println ("====snaptoscreen delta=" +delta); Mscroller.startscroll (MSCROLLX, 0, Delta, 0, Math.Abs (Delta) * 2) //invalidate is very important, otherwise you can move a little page cannot reply to the original invalidate ();}


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.