If you write an android desktop sliding switch Screen Control (1)

Source: Internet
Author: User
Tags xdiff

If you write an android desktop sliding switch Screen Control (1)

First, this control should inherit ViewGroup:

Initialization:

Public class MyGroup extends ViewGroup {private Scroller mScroller; private float mOriMotionX; private float handler; private VelocityTracker handler; private int mTouchState = running; private static final int TOUCH_STATE_REST = 0; private int mTouchSlop; private int mMaximumVelocity; private static final int TOUCH_STATE_SCROLLING = 1; private float 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, AttributeSet attrs) {super (context, attrs); initWorkspace ();} private void initWorkspace () {mScroller = new Scroller (getContext (); setCurrentScreen (0); final ViewConfiguration configuration = ViewConfiguration. get (getContext (); mTouchSlop = configuration. getScaledTouchSlop (); // This defines the minimum pixel distance of the control in scroll from mMaximumVelocity = configuration. getScaledMaximumFlingVelocity (); // rate, the number of pixels that fling slides per second}


Override onmeasure first:

@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 paddingTop = 0; int childLeft = paddingleft; final int count = getChildCount (); for (int I = 0; I <count; I ++) {final View child = getChildAt (I); if (child. getVisibility ()! = View. GONE) {final int childWidth = child. getMeasuredWidth (); final int childHeight = child. getMeasuredHeight (); child. layout (childLeft, paddingTop, childLeft + childWidth, childHeight + paddingTop );ChildLeft + = child. getMeasuredWidth (); // The distance between the left of the next child and the left of the first child is exactly the width of the first child}}}

Then write the touch event of the View:

OnInterceptTouchEvent is passed to the view in the control only when the false event is returned. That is, the ontouch event of the view can capture the onTouchEvent in the View and return it to true to execute multiple touch events, event can be passed

@ Overridepublic boolean onInterceptTouchEvent (MotionEvent ev) {final int action = ev. getAction (); // if it is a move event, the mTouchState is TOUCH_STATE_REST, which prevents the child control from sliding with its fingers, in this case, 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, the finger slides in if (xMoved) {mTouchState = success ;} break; case MotionEvent. ACTION_DOWN: mLastMotionX = x; // mScroller. if isFinished () is true, it indicates that the sliding is over. At this time, we set the status to TOU. Ch_state_re1_touchstate = 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 the static state, true is returned, so that the event will not be passed to onTouchEvent. return mTouchState! = TOUCH_STATE_REST ;}

The reason why true is returned when sliding is that the ontouch event in the control is not required at this time.

@ Overridepublic boolean onTouchEvent (MotionEvent ev) {if (mVelocityTracker = null) {mVelocityTracker = VelocityTracker. obtain ();} mVelocityTracker. addMovement (ev); int mScrollX = this. getScrollX (); // mScrollX indicates the distance on the X axis. sliding to the left is positive. At this time, the screen moves final int action = ev to the right. 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 indicates that a half screen can be swiped on the first or last page. // mLastMotionX is larger than x if the screen is backward and forward, deltaX is positive int deltaX = (int) (mLastMotionX-x); mLastMotionX = x; System. out. println ("===== deltaX =" + deltaX );/ /DeltaX <0 indicates moving forward if (deltaX <0) {// This is sliding to the right, and the screen moves scrollBy (Math. max (-mScrollX-buffer, deltaX), 0);} else {int availableToScroll = 0; if (getChildCount ()> 0) {// at this time, no item may be added to the Workspace, count = 0System. out. println ("==== rihgt =" + (getChildAt (getChildCount ()-1 ). getRight () + "avail =" + (getChildAt (getChildCount ()-1 ). getRight ()-mScrollX-getWidth (); // getChildAt (getChildCount ()-1 ). getRight () is for all vi The width of the new view. Here, three views are added. If one view is 1080, the value is 3240 availableToScroll = getChildAt (getChildCount ()-1 ). getRight ()-mScrollX-getWidth (); // availableToScroll + maximum sliding distance of buffer, deltax is the sliding distance of scrollBy (Math. min (availableToScroll + buffer, deltaX), 0) ;}} return true; case MotionEvent. ACTION_UP: final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker. computeCurrentVelocity (DEFAULT_VALUE, mMaximumV Elocity); int velocityX = (int) velocityTracker. getXVelocity (); // velocityX indicates the speed at which the finger slides. We will compare it with the given value SNAP_VELOCITY if (velocityX> SNAP_VELOCITY & mCurrentScreen> 0) {// The finger slides forward at this time, the screen is moved backward to snapToScreen (mCurrentScreen-1);} else if (velocityX <-SNAP_VELOCITY & mCurrentScreen <getChildCount ()-1) {// move rightsnapToScreen (mCurrentScreen + 1);} else {snapToDestination (mLastMotionX <mOriMotionX);} if (mV ElocityTracker! = Null) {mVelocityTracker. recycle (); mVelocityTracker = null;} mTouchState = TOUCH_STATE_REST; if (Math. abs (mLastDownX-x)> 10) {return true;} return false; case MotionEvent. ACTION_CANCEL: mTouchState = TOUCH_STATE_REST; return false; default: break;} return true ;}

/** The sliding distance starts to change the screen when the screen width is a few minutes away. * // *** SnapToDestination. * mLastMotionX <mOriMotionX (mLastMotion <mOriMotionX) indicates that the mobile phone slides backward, but the screen moves forward, and vice versa. * If forward is true, the screen moves forward, add scrollX to the screen width of * scrollX/screenWidth to determine the current screen * @ param forward: forward or backward. */public void snapToDestination (boolean forward) {final int screenWidth = getWidth (); int scrollX = getScrollX (); if (forward) {scrollX + = screenWidth-screenWidth/3 ;} else {scrollX + = s CreenWidth/3;} System. out. println ("====== screenWidth =" + screenWidth + "scrollX/screenWidth =" + (scrollX/screenWidth); snapToScreen (scrollX/screenWidth );} /*** if the distance to be swiped is calculated: (whichScreen * getWidth () is the X coordinate after sliding, this. getScrollX () is the current coordinate, and the two are subtracted from the sliding distance * Math. abs (delta) * 2 is the sliding duration */public void snapToScreen (int whichScreen) {whichScreen = Math. max (0, Math. min (whichScreen, getChildCount ()-1); boolean chang IngScreens = 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 cannot restore the original invalidate () when moving a page ();}

Code: http://download.csdn.net/detail/baidu_nod/7731631

Related Article

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.