Android custom control GuideView guide Interface

Source: Internet
Author: User

Android custom control GuideView guide Interface

As personal technology is still relatively weak, we have expanded on the basis of this custom control to support both horizontal and vertical

The following is the main code;

 

GuideView:

 

Import android. annotation. suppressLint; import android. content. context; import android. content. res. typedArray; import android. graphics. point; import android. graphics. pointF; import android. util. attributeSet; import android. util. displayMetrics; import android. util. log; import android. view. motionEvent; import android. view. velocityTracker; import android. view. view; import android. view. viewGroup; import android. view. windowManager; import android. widget. scroller; import android. widget. toast; @ SuppressLint (ClickableViewAccessibility) public class GuideView extends ViewGroup {/***
  
   
  
 ** // ** Sliding direction */private int mOrientation = 0;/** Horizontal Direction */private int mHorientation = 0; /** vertical direction */private int mVertical = 1;/** screen width */private int mScreenWidth;/** screen height */private int mScreenHeight; /** slide status */private boolean isScrolling;/** slide helper class */private Scroller mScroller;/** record the current x/y value */private PointF mPointF; /** record the last x and y values */private PointF mLastPointF;/** start coordinate corresponding to Scroller */private Point mScrollS TartPoint;/** the ending coordinate */private Point * of Scroller;/** record the sliding distance */private PointF mDistancePointF; /** ScrollXY difference value */private Point mDistanceScrollPoint;/** acceleration detection */private VelocityTracker mVelocityTracker;/** callback function for screen switching */private OnPageChangeListener mOnPageChangeListener; /*** record current page */private int currentPage = 0; public GuideView (Context context, AttributeSet attrs, int defStyleAttr) {super (Context, attrs, defStyleAttr); // TODO Auto-generated constructor stub // obtain the custom attribute TypedArray mTypeArray = context. obtainStyledAttributes (attrs, R. styleable. guideView_orientation); mOrientation = mTypeArray. getInteger (R. styleable. guideView_orientation_orientation, mOrientation); mTypeArray. recycle (); // obtain the screen width and height initialScreen (context); mScroller = new Scroller (context); mPointF = new PointF (); mLastPointF = n Ew PointF (); mScrollStartPoint = new Point (); mScrollStopPoint = new Point (); mDistancePointF = new PointF (); mDistanceScrollPoint = new Point ();} public GuideView (Context context, attributeSet attrs) {this (context, attrs, 1); // TODO Auto-generated constructor stub} public GuideView (Context context) {this (context, null ); // TODO Auto-generated constructor stub} @ Overrideprotected void onMeasure (int widthMea SureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper. onMeasure (widthMeasureSpec, heightMeasureSpec); // obtain the sub-layout and remeasure the sub-layout width and height. int count = getChildCount (); for (int I = 0; I <count; I ++) {measureChild (getChildAt (I), widthMeasureSpec, heightMeasureSpec) ;}@ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {// TODO Auto-generated method stubif (changed) {// retest MarginLayoutParams params = (MarginLayoutParams) getLayoutParams (); int childCount = getChildCount (); if (mOrientation = mHorientation) {params. width = mScreenWidth * getChildCount (); setLayoutParams (params); for (int I = 0; I <childCount; I ++) {View view = getChildAt (I ); if (view. getVisibility ()! = View. GONE) {view. layout (I * mScreenWidth, t, I * mScreenWidth + mScreenWidth, B) ;}} else if (mOrientation = mVertical) {params. height = mScreenHeight * getChildCount (); setLayoutParams (params); for (int I = 0; I <childCount; I ++) {View view = getChildAt (I ); // if the view is not hidden, locate if (view. getVisibility ()! = View. GONE) {view. layout (l, I * mScreenHeight, r, I * mScreenHeight + mScreenHeight) ;}}}@ Overridepublic boolean onTouchEvent (MotionEvent event) {// TODO Auto-generated method stub // first-line event determination interception if (currentPage = getChildCount ()-1) {Toast. makeText (getContext (), finish, Toast. LENGTH_SHORT ). show (); return super. onTouchEvent (event);} if (isScrolling) return super. onTouchEvent (event); mPointF. x = event. getX (); MPointF. y = event. getY (); // initialize the accelerometer detector initialVelocity (event); if (event. getAction () = MotionEvent. ACTION_DOWN) {// records the coordinate information Log when the user touches the page. I (info, ******** mPoint value ***** + x: + mPointF. x + y: + mPointF. y); getStartScrollXY (); mLastPointF. x = mPointF. x; mLastPointF. y = mPointF. y;} else if (event. getAction () = MotionEvent. ACTION_MOVE) {Log. I (info, ******** mLastPoint value ***** + x: + mLastPointF. x + y: + mLastPointF. y); Log. I (Info, ******* mPoint value ***** + x: + mPointF. x + y: + mPointF. y); Log. I (info, **************************************** ***); log. I (info, **************************************** ***); /*** Stops the animation. contrary to * {@ link # forceFinished (boolean)}, aborting the animating cause * the scroller to move to the final x and y position Source Code Description: * mScroller. abortAnimation () if the slide is not over, terminate the slide. ** @ See # forceFinished (boolean) */if (! MScroller. isFinished () {mScroller. abortAnimation ();} mDistancePointF. x = mLastPointF. x-mPointF. x; mDistancePointF. y = mLastPointF. y-mPointF. y; Log. I (info, ******** mDistancePointF value ****** + dx: + mDistancePointF. x + dy: + mDistancePointF. y); getStopScrollXY (); // first determine the sliding direction and determine the sliding distance scrollBy (x, y) // 1. Y axis --- slide upwards -- next view // 2. Y axis --- slide down -- previous view // 3. X axis --- sliding left -- next view // 4. X axis --- slide to the right -- the previous view/*** 320*480-8 mlasty =-1 0 currenty =-2 * distance = mlasty-currenty =-8 0 scrolly + distance <0? ** When all the conditions are met, determine the view to slide up and load the next view ** re-define the distanceY value for ScrollBy (x, y) Call ** additional instructions: * getScrollX () note: = x coordinate in the upper left corner of the display area of the mobile phone screen minus x coordinate in the upper left corner of the MultiViewGroup view = 320 ** getScrollY () Description: = y coordinate in the upper left corner of the mobile phone screen display area Minus y coordinate in the upper left corner of the MultiViewGroup view = 0 (* because the height of the subview is the same as that of the mobile phone screen) * ***/if (mOrientation = mHorientation) {if (mDistancePointF. x> 0 & mScrollStopPoint. x + mDistancePointF. x> getWidth ()-mScreenWidth) {mDistancePointF. x = getWidth ()-mScreenW Idth-mScrollStopPoint. x;} else if (mDistancePointF. x <0 & mScrollStopPoint. x + mDistancePointF. x <0) {mDistancePointF. x =-mScrollStopPoint. x;} scrollBy (int) mDistancePointF. x, 0);} else if (mOrientation = mVertical) {if (mDistancePointF. y <0 & mScrollStopPoint. y + mDistancePointF. y <0) {mDistancePointF. y =-mScrollStopPoint. y;} if (mDistancePointF. y> 0 & mScrollStopPoint. y + mDistancePoint F. y> getHeight ()-mScreenHeight) {mDistancePointF. y = getHeight ()-mScreenHeight-mScrollStopPoint. y;} scrollBy (0, (int) mDistancePointF. y);} mLastPointF. x = mPointF. x; mLastPointF. y = mPointF. y;} else if (event. getAction () = MotionEvent. ACTION_UP) {getStopScrollXY (); getDistanceScrollXY (); // compare the moving direction trend. // you can determine whether the sliding is upward or downward. if (checkDirection ()) {// slide more if (isScrollToNext () {// slide to the next page if (mOrientation = mHorient Ation) {mScroller. startScroll (getScrollX (), 0, mScreenWidth-mDistanceScrollPoint. x, 0);} else if (mOrientation = mVertical) {mScroller. startScroll (0, getScrollY (), 0, mScreenHeight-mDistanceScrollPoint. y) ;}} else {// cannot slide to the next page if (mOrientation = mHorientation) {mScroller. startScroll (getScrollX (), 0,-mDistanceScrollPoint. x, 0);} else if (mOrientation = mVertical) {mScroller. startScroll (0, getScrollY (), 0,-mDista NceScrollPoint. y) ;}} else {// slide down, refresh if (isScrollToprivew () {// slide to the previous page if (mOrientation = mHorientation) {mScroller. startScroll (getScrollX (), 0,-mScreenWidth-mDistanceScrollPoint. x, 0);} else if (mOrientation = mVertical) {mScroller. startScroll (0, getScrollY (), 0,-mScreenHeight-mDistanceScrollPoint. y) ;}} else {// cannot slide to the previous page if (mOrientation = mHorientation) {mScroller. startScroll (getScrollX (), 0,-mDistanceScrol LPoint. x, 0);} else if (mOrientation = mVertical) {mScroller. startScroll (0, getScrollY (), 0,-mDistanceScrollPoint. y) ;}}} isScrolling = true; postInvalidate (); recycleVelocity () ;}return true ;} /*** Called by a parent to request that a child update its values for mScrollX * and mScrollY if necessary. this will typically be done if the child is * animating a scroll using a {@ link android. widget. scroller S Croroller} * object. ** to facilitate screen sliding control, the Android framework provides the computeScroll () method to control the process. This * method is called during the draw () process when a View is drawn. Therefore, when we use the Scroller instance together, we can obtain the current offset coordinates and manually offset the View/ViewGroup to this place. * The following is a prototype of the computeScroll () method. This method is located in ViewGroup. in the java class * // @ Overridepublic void computeScroll () {// TODO Auto-generated method stubsuper. computeScroll (); if (mOrientation = mVertical) {if (mScroller. computescroloffset () {scrollTo (0, mScroller. getCurrY (); postInvalidate ();} else {int position = getScrollY ()/mScreenHeight; if (position! = CurrentPage) {if (mOnPageChangeListener! = Null) {currentPage = position; mOnPageChangeListener. onPageChange (currentPage) ;}}} else if (mOrientation = mHorientation) {if (mScroller. computescroloffset () {scrollTo (mScroller. getCurrX (), 0); postInvalidate ();} else {int position = getScrollX ()/mScreenWidth; if (position! = CurrentPage) {if (mOnPageChangeListener! = Null) {currentPage = position; mOnPageChangeListener. onPageChange (currentPage) ;}}} isScrolling = false ;} /*********************************** Method ** **************************************** * ** // *** obtain the screen width and height */public void initialScreen (Context context) {WindowManager mWindowManager = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics (); m WindowManager. getdefadisplay display (). getMetrics (outMetrics); mScreenWidth = outMetrics. widthPixels; mScreenHeight = outMetrics. heightPixels;}/*** initialize the acceleration detector ** @ param event */private void initialVelocity (MotionEvent event) {if (mVelocityTracker = null) {mVelocityTracker = VelocityTracker. obtain ();} mVelocityTracker. addMovement (event);}/*** initialize scrollX scrollY */private void getStartScrollXY () {mScrollS TartPoint. x = getScrollX (); mScrollStartPoint. y = getScrollY ();}/*** ScrollX ScrollY */private void getStopScrollXY () {mScrollStopPoint. x = getScrollX (); mScrollStopPoint. y = getScrollY ();}/*** compares the sliding ScrollX ScrollY difference */private void getDistanceScrollXY () {mDistanceScrollPoint. x = mScrollStopPoint. x-mScrollStartPoint.x; mDistanceScrollPoint. y = mScrollStopPoint. y-mScrollStartPoint.y;}/*** check the slide direction * @ Return true load more false refresh */public boolean checkDirection () {boolean mDirection = false; if (mOrientation = mVertical) {mDirection = mDistanceScrollPoint. y> 0? True: false;} else if (mOrientation = mHorientation) {mDirection =-mDistanceScrollPoint. x <0? True: false;} return mDirection;}/*** determine whether to slide to the next Screen Based on the sliding distance * loading multiple * @ return */private boolean isScrollToNext () {boolean isScrollTo = false; if (mOrientation = mVertical) {isScrollTo = mDistanceScrollPoint. y> mScreenHeight/2 | Math. abs (getVelocity ()> 600;} else if (mOrientation = mHorientation) {isScrollTo = mDistanceScrollPoint. x> mScreenWidth/2 | Math. abs (getVelocitx ()> 600;} return isS CrollTo;}/*** determine whether to slide to the previous screen based on the sliding distance * refresh * @ return */private boolean isScrollToprivew () {boolean isScrollTo = false; if (mOrientation = mVertical) {isScrollTo =-mDistanceScrollPoint. y> mScreenHeight/2 | Math. abs (getVelocity ()> 600;} else if (mOrientation = mHorientation) {isScrollTo =-mDistanceScrollPoint. x> mScreenWidth/2 | Math. abs (getVelocitx ()> 600;} return isScrollTo;}/*** get the addition of x Speed ** @ return */private int getVelocitx () {mVelocityTracker. computeCurrentVelocity (1000); int velocitx = (int) mVelocityTracker. getXVelocity (1000); velocitx = (int) mVelocityTracker. getXVelocity (1000); return velocitx;}/*** get the acceleration in the y direction ** @ return */private int getVelocity () {mVelocityTracker. computeCurrentVelocity (1000); int velocity = (int) mVelocityTracker. getYVelocity (1000); velocity = (int) mVeloc ItyTracker. getYVelocity (1000); return velocity;}/*** release resource */private void recycleVelocity () {if (mVelocityTracker! = Null) {mVelocityTracker. recycle (); callback = null;}/*** set the callback interface ** @ param onPageChangeListener */public void listener (OnPageChangeListener onPageChangeListener) {mOnPageChangeListener = onPageChangeListener ;} /*** callback interface ** @ author zhy **/public interface OnPageChangeListener {void onPageChange (int currentPage );}}

 

 

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.