Android custom control Guideview Boot interface

Source: Internet
Author: User

Recently read Hyman's blog: http://blog.csdn.net/lmj623565791/article/details/23692439, because personal technology is still relatively weak, on the basis of this custom control to do a development, Support for 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 {/** * <attr name=" orientation "> <enum nam E= "Horizontal" value= "1"/> <enum * name= "vertical" value= "2"/> </attr> **//** sliding direction */private int Morienta tion = 0;/** horizontal direction */private int mhorientation = 0;/** vertical direction */private int mvertical = 1;/** screen width */private int mscreenwidth ;/** screen height */private int mscreenheight;/** sliding State */private boolean isscrolling;/** Slide helper class */private Scroller mscroller;/** record The value of the current X/y */private PoIntF mpointf;/** records the last X, y values */private PointF mlastpointf;/** Scroller corresponding start coordinates */private point mscrollstartpoint;/** scrol Ler corresponds to the end coordinate */private point mscrollstoppoint;/** record the distance of the slide */private PointF mdistancepointf;/**scrollxy Difference */private Point mdistancescrollpoint;/** acceleration Detection */private velocitytracker mvelocitytracker;/** When switching the screen callback function */private Onpagechangelistener monpagechangelistener;/** * Record current page */private int currentpage = 0;public Guideview (context context, at Tributeset attrs, int defstyleattr) {Super (context, attrs, defstyleattr);//TODO auto-generated constructor stub//get custom genus Sex Typedarray Mtypearray = context.obtainstyledattributes (Attrs, r.styleable.guideview_orientation); mOrientation = Mtypearray.getinteger (R.styleable.guideview_orientation_orientation, morientation); mTypeArray.recycle ();// Get the screen width height initialscreen (context); Mscroller = new Scroller (context); mpointf = new PointF (); mlastpointf = new 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}pu Blic Guideview (Context context) {This (context, null);//TODO auto-generated constructor stub} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//TODO auto-generated method Stubsuper.onmeasure ( Widthmeasurespec, Heightmeasurespec);//Get sub-layout, re-measure sub-layout height int count = Getchildcount (); for (int i = 0; i < count; i++) {mea Surechild (Getchildat (i), Widthmeasurespec, Heightmeasurespec);}} @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {//TODO auto-generated method Stubif (Chang ed) {//re-measure the location of layout 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);//view is not hidden, reposition if (view.getvisibility ()! = View.gone) {view.layout (L, I * mscreenheigh T, R, I * mscreenheight+ mscreenheight);}}}} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method stub//first event to intercept 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) {//record the coordinates information LOG.I when the user touches ("info", "* * Mpoint value**** "+" x: "+mpointf.x+" y: "+mpointf.y"; Getstartscrollxy (); mlastpointf.x = Mpointf.x;mlastpointf.y = MPOINTF.Y;} Elseif (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 Po Sition Source Code Description: * Mscroller.abortanimation () If the swipe is not finished, then stop the swipe. * * @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 direction of the slide determines the distance Scrollby (x, y)//1.Y axis---swipe up--next view//2.Y axis---Swipe down--previous view//3.x axis---swipe left--next view// 4.x axis---Swipe right-previous view/** * 320*480-8 mlasty=-10 currenty=-2 * distance=mlasty-currenty=-8 ' 0 scrolly+distance<0? * * When the conditions are met, determine the view swipe up, load the next view * * Redefine the value of Distancey for Scrollby (x, y) calls * Supplemental instructions: * GETSCROLLX () Description: = Phone screen display area upper left corner x coordinate minus Multivie Wgroup View upper left corner x coordinate =320 * * getscrolly () Description: = Phone screen display area upper left corner y coordinate minus multiviewgroup view upper left corner y coordinate =0 (* Because the child view height is the same as the screen height of the phone) * **/IF (Morien tation = = mhorientation) {if (mdistancepointf.x > 0&& mscrollstoppoint.x + mdistancepointf.x > GetWidth ()-M ScreenWidth) {mdistancepointf.x = GetWidth ()-mscreenwidth-mscrollstoppoint.x;} else if (Mdistancepointf.x < 0&& Amp 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 + mdistancepointf.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 sliding direction trend// Decide whether to slide on or slide if (checkdirection ()) {//Slide "load More" if (Isscrolltonext ()) {//can slide to the next 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 next page if (morientation==mhorientation) {Mscroller.startscroll (Getscrollx (), 0,-mdistancescrollpoint.x,0) ;} else if (morientation==mvertical) {mscroller.startscroll (0, getscrolly (), 0,-mdistancescrollpoint.y);}}} else{//"Swipe, refresh" if (Isscrolltoprivew ()) {//can 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 previous if (morientation==mhorientation) {Mscroller.startscroll (GETSCROLLX (), 0,-mdistancescrollpoint.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, a child update its values for MSCROLLX * and mscrolly if necessary. This would typically be do if the child is * animating a scroll using a {@link Android.widget.Scroller Scroller} * Object . * * for easy control of sliding screen control, the Android framework provides the Computescroll () method to control this process. When the view is drawn, the * method is called in the Draw () procedure. Therefore, with the use of the scroller instance, we can get the current offset coordinates and manually offset the view/viewgroup to that point. * The Computescroll () method is prototyped as follows, the method is located in the Viewgroup.java class */@Overridepublic void Computescroll () {//TODO auto-generated method Stu Bsuper.computescroll (); if (morientation== mvertical) {if (Mscroller.computescrolloffset ()) {scrollTo (0, Mscroller.getcurry ());p ostinvalidate ();} else {int position = getscrolly ()/mscreenheight;if(Position! = currentpage) {if (Monpagechangelistener! = null) {currentpage = Position;monpagechangelistener.onpagechange (CurrentPage);}}}} else if (morientation== mhorientation) {if (Mscroller.computescrolloffset ()) {ScrollTo (Mscroller.getcurrx (), 0); Postinvalidate ();} else {int position = GETSCROLLX ()/mscreenwidth;if (position! = currentpage) {if (Monpagechangelistener! = null) {current Page = Position;monpagechangelistener.onpagechange (currentpage);}}} Isscrolling = false;} /************************************ Method *********************************************//** * Get screen width high */public void Initialscreen (Context context) {WindowManager Mwindowmanager = (windowmanager) context.getsystemservice ( Context.window_service);D isplaymetrics outmetrics = new Displaymetrics (); Mwindowmanager.getdefaultdisplay (). Getmetrics (outmetrics); mscreenwidth = Outmetrics.widthpixels;mscreenheight = Outmetrics.heightpixels;} /** * Initialize Accelerometer detector * * @param event */private void Initialvelocity (Motionevent event) {if (Mvelocitytracker = = null) {mvelocitytracker = Velocitytracker.obtain ();} Mvelocitytracker.addmovement (event);} /** * Initialize SCROLLX scrolly */private void Getstartscrollxy () {mscrollstartpoint.x = Getscrollx (); mscrollstartpoint.y = Getscrolly ();} /** * Stop sliding after scrollx scrolly */private void Getstopscrollxy () {mscrollstoppoint.x = Getscrollx (); mscrollstoppoint.y = Getscrolly ();} /** * Compare sliding scrollx scrolly difference */private void Getdistancescrollxy () {mdistancescrollpoint.x = Mscrollstoppoint.x-mscrollstartpoint.x;mdistancescrollpoint.y = Mscrollstoppoint.y-mscrollstartpoint.y;} /** * Check Slide direction * @return True load more false flush */public Boolean checkdirection () {boolean mdirection =false;if (morientation = = mvertical) {mdirection = mdistancescrollpoint.y > 0? true:false;} else if (morientation== mhorientation) {mDirecti On =-Mdistancescrollpoint.x < 0? True:false;} return mdirection;} /** * Based on the sliding distance to determine whether you can slide to the next screen * load with multi * @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 Isscrollto;}  /** * Based on the sliding distance to determine whether you can swipe to the previous screen * 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 acceleration in x direction * * @return */private int getvelocitx () {mvelocitytracker.computecurrentvelocity (+); int velocitx = (in T) mvelocitytracker.getxvelocity (+); velocitx = (int) mvelocitytracker.getxvelocity (+); return VELOCITX;} /** * Get acceleration in y direction * * @return */private int getvelocity () {mvelocitytracker.computecurrentvelocity (+); intvelocity = (int) mvelocitytracker.getyvelocity (n); velocity = (int) mvelocitytracker.getyvelocity (+); return Velocity;  /** * Frees resource */private void Recyclevelocity () {if (Mvelocitytracker! = null) {mvelocitytracker.recycle (); mvelocitytracker = null;}} /** * Set Callback interface * * @param onpagechangelistener */public void Setonpagechangelistener (Onpagechangelistener onpagechangelist Ener) {monpagechangelistener = Onpagechangelistener;} /** * Callback Interface * * @author zhy * */public interface Onpagechangelistener {void onpagechange (int currentpage);}}

Attrs:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable    name= "Guideview_ Orientation ">        <attr name=" orientation ">            <enum name=" horientation "value=" 0 "/>            <enum Name= "Vertical" value= "1"/>        </attr>    </declare-styleable></resources>

Source: http://download.csdn.net/detail/anddroid_lanyan/8660753

There are still legacy issues in the Project: (Cancel page intercept event) When I change the background color to white, horizontal layout, left and right sliding repeatedly, there will be white side, still do not understand the specific problem, who if know the trouble to tell.

After changing this custom control, I find that one thing, as long as you are willing to insist on doing something, you will have some gains.

Android custom control Guideview Boot interface

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.