Recently, I resigned, in the search for work, unfortunately encountered this job-hunting season, in addition to graduate job-hunting peak, resume has been sent without a few copies of it, baby heart that is a bitter ah! Over the past code, I accidentally found an interesting control, then I have not written the habit of blogging, now fill in, First look:
Then look at the usage code:
Stellarmap Stellarmap = (stellarmap) Findviewbyid (r.id.stellar);//Set data Recommendadapter adapter = new Recommendadapter ( ); Stellarmap.setadapter (adapter);//home Check Stellarmap.setgroup (0, true);//split screen stellarmap.setregularity (15, 20);
Class Recommendadapter implements Adapter {/** default group number */public static final intpagesize=; @Overridepublic int Getgroupcoun T () {//data grouping int groupcount = Data.size ()/pagesize;//last Set if (data.size ()% PAGESIZE! = 0) {return groupcount + 1;} return GroupCount;} @Overridepublic int GetCount (int group) {///last group if (data.size ()% PAGESIZE! = 0) {if (group = = GetGroupCount ()-1) {Retur n data.size ()% PAGESIZE;}} return PAGESIZE;} @Overridepublic View GetView (int group, int position, view Convertview) {TextView TV = new TextView (mainactivity.this); int index = Group * PAGESIZE + position;tv.settext (data.get (index));//stochastic size random random = new random ();//14-17int size = RA Ndom.nextint (4) + 14;tv.settextsize (size),///random color int alpha = 255;int red = random.nextint (+) + 30;int green = Random.nex TInt + 30;int blue = Random.nextint (+) + 30;int ARGB = Color.argb (alpha, red, green, blue); Tv.settextcolor (ARGB); re Turn TV;} @Overridepublic int Getnextgrouponpan (int group, float degree) {if (group = = GETGRoupcount ()-1) {group =-1;} return group + 1;} @Overridepublic int getnextgrouponzoom (int group, Boolean iszoomin) {if (group = = GetGroupCount ()-1) {group =-1;} return group + 1;}}
The code is very simple, I say briefly, GetGroupCount returns a total number of groups, GetCount returns a set of how many elements, GetView will not say, Getnextgrouponpan return the next number of groups to enlarge the animation, Getnextgrouponzoom returns the next number of groups that need the wrong small animation.
The next is the dinner, we see the implementation of Stellarmap, Stellarmap inherited from Framelayout:
/** Construction Method */public Stellarmap (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); init ();} Public Stellarmap (context context, AttributeSet Attrs) {Super (context, attrs); init ();} Public Stellarmap (Context context) {super (context); init ();}
This should be familiar to everyone, custom view needs to implement the three construction methods.
/** initialization method */private void init () {mgroupcount = 0;mhidengroupindex = -1;mshowngroupindex = -1;mhidengroup = new Randomlayou T (GetContext ()); mshowngroup = new Randomlayout (GetContext ()); AddView (Mhidengroup, New Layoutparams ( Layoutparams.fill_parent, Layoutparams.fill_parent)); mhidengroup.setvisibility (View.gone); AddView (MShownGroup, New Layoutparams (Layoutparams.fill_parent, layoutparams.fill_parent)); mgesturedetector = new GestureDetector (this); Setontouchlistener (this);//Set Animation Mzoominnearanim = Animationutil.createzoominnearanim (); Mzoominnearanim.setanimationlistener (this); Mzoominawayanim = Animationutil.createzoominawayanim (); Mzoominawayanim.setanimationlistener (this); Mzoomoutnearanim = Animationutil.createzoomoutnearanim (); Mzoomoutnearanim.setanimationlistener (this); Mzoomoutawayanim = Animationutil.createzoomoutawayanim (); Mzoomoutawayanim.setanimationlistener (this);}
The code is very clear, simply say, Mgroupcount is the number of groups, Mhidengroupindex is the number of hidden groups, Mshowngroupindex is the number of display of the group, and created two randomlayout, it inherits from ViewGroup , used to implement the random placement of the view, then create gesture monitoring and touch monitoring, and here are four different animations.
Following the order of code execution, the next step is to set adapter:
/** set this Adapter */public void Setadapter (Adapter Adapter) {madapter = Adapter;mgroupcount = Madapter.getgroupcount (); if (m GroupCount > 0) {mshowngroupindex = 0;} Setchildadapter ();}
It is seen that the number of groups is initialized and the Setchildadapter method is called:
/** set adapter */private void Setchildadapter () {if (null = = Madapter) {return;} for child group Mhidengroupadapter = new Randomlayout.adapter () {///Remove the View object from this Adapter to Hidengroup adapter@overridepublic view GetView ( int position, View Convertview) {return Madapter.getview (mhidengroupindex, Position, Convertview);} @Overridepublic int GetCount () {return madapter.getcount (Mhidengroupindex);}}; Mhidengroup.setadapter (mhidengroupadapter); mshowngroupadapter = new Randomlayout.adapter () {// Remove the adapter View object to Showngroup's adapter@overridepublic view getView (int position, view Convertview) {return Madapter.getview (Mshowngroupindex, Position, Convertview);} @Overridepublic int GetCount () {return madapter.getcount (Mshowngroupindex);}}; Mshowngroup.setadapter (Mshowngroupadapter);}
This method creates a adapter for the child view, which is randomlayout, and we look at its implementation:
/** Construction Method */public Randomlayout (context context) {super (context); init ();}
/** initialization method */private void init () {mlayouted = FALSE;MRDM = new Random (); setregularity (1, 1); mfixedviews = new Hashset<vi Ew> (); mrecycledviews = new linkedlist<view> ();}
In the Init method, mlayouted Indicates whether the view has been onlayout,mfixedviews to hold the view that has been identified, Mrecycledviews records the recovered view for reuse, setregularity (1, 1 The method is simply initialized and will be re-invoked, and we'll say that the Setadapter method is fairly straightforward:
/** Set data source */public void Setadapter (Adapter Adapter) {this.madapter = Adapter;}
Then go back to using the code, the next sentence is Stellarmap.setgroup (0, true), we look at the implementation:
/** sets the animation for the specified group */public void setgroup (int groupindex, Boolean playanimation) {Switchgroup (Groupindex, Playanimation, Mzoominnearanim, Mzoominawayanim);}
/** sets the incoming and outgoing animation to the next group */private void Switchgroup (int newgroupindex, Boolean playanimation, Animation inanim,animation Outanim) {if (Newgroupindex < 0 | | Newgroupindex >= mgroupcount) {return;} Sets the current display group corner mark to hidden Mhidengroupindex = mshowngroupindex;//Sets the next group corner to the displayed mshowngroupindex = newgroupindex;// Exchange two Grouprandomlayout temp = Mshowngroup;mshowngroup = Mhidengroup;mshowngroup.setadapter (Mshowngroupadapter); Mhidengroup = Temp;mhidengroup.setadapter (mhidengroupadapter);//Refresh the displayed Groupmshowngroup.refresh ();// Show Groupmshowngroup.setvisibility (view.visible);//Start animation if (playanimation) {if (mshowngroup.haslayouted ()) { Mshowngroup.startanimation (Inanim);} Mhidengroup.startanimation (Outanim);} else {mhidengroup.setvisibility (view.gone);}}
The Switchgroup method is the core approach of Stellarmap, by exchanging the corners and adapter of show and hide, and by switching between show and hide, and turn on over animation.
The last line of code, stellarmap.setregularity (15, 20) Method:
/** sets the rules for hiding groups and displaying groups of x and y */public void setregularity (int xregularity, int yregularity) {mhidengroup.setregularity ( Xregularity, yregularity); Mshowngroup.setregularity (xregularity, yregularity);}
To set the screen segmentation, and then see the Randomlayout setregularity method:
/** set mxregularity and mxregularity, determine the number of zones */public void setregularity (int xregularity, int yregularity) {if (Xregularity & Gt 1) {this.mxregularity = xregularity;} else {this.mxregularity = 1;} if (Yregularity > 1) {this.myregularity = yregularity;} else {this.myregularity = 1;} This.mareacount = mxregularity * The number of myregularity;//equals the number of X direction *y the number of directions this.mareadensity = new int[myregularity][ mxregularity];//a two-dimensional array of storage areas}
This saves the fast number of screen splits, creates a two-dimensional array, locates the specific location, and its onlayout is the key to the regional distribution:
/** determines the location of the sub-view, which is the key of the area distribution */@Overridepublic void OnLayout (Boolean changed, int l, int t, int r, int b) {Final int count = Getchildcount ();//determine its own wide-height int thisw = r-l-This.getpaddingleft ()-this.getpaddingright (); int thish = b-t-This.get Paddingtop ()-This.getpaddingbottom ();//The right and bottom of its own content area int contentright = R-getpaddingright (); int contentbottom = B-get Paddingbottom ();//In order to store the area into the collection list<integer> Availareas = new arraylist<integer> (mareacount); for (int i = 0; i < Mareacount; i++) {availareas.add (i);} int areacapacity = (count + 1)/Mareacount + 1; Area density, which indicates that a region can be placed in a few view,+1 to indicate at least one int availareacount = Mareacount; Number of available zones for (int i = 0; i < count; i++) {final View child = Getchildat (i); if (child.getvisibility () = = View.gone) {/ /Gone dropped view is not involved in the layout continue;} if (!mfixedviews.contains (child)) {///mfixedviews is used to hold a view that has been identified, there is no need to store the layoutparams params = (layoutparams) Child.getlayoutparams ();//measure the size of the child view int childwidthmeasurespec = Measurespec.makemeaSurespec (This.getmeasuredwidth (), measurespec.at_most);//the parameter int childheightmeasurespec for the child view preparation measurement = Measurespec.makemeasurespec (This.getmeasuredheight (), measurespec.at_most); Child.measure (ChildWidthMeasureSpec, CHILDHEIGHTMEASURESPEC);//the width and height int childw = child.getmeasuredwidth () after the sub-view measurement; int CHILDH = Child.getmeasuredheight () ///With its own height divided by the assigned value, you can calculate the width and height of each region float COLW = THISW/(float) mxregularity;float rowh = thish/(float) myregularity;while (a Vailareacount > 0) {//If the use area is greater than 0, you can try assigning an int arrayidx = Mrdm.nextint (availareacount) for the child View;//random a list in the position int areaidx = Availareas.get (ARRAYIDX);//Gets an area number based on the position in the list int col = areaidx% mxregularity;//calculates the position in the two-dimensional array int row = Areaidx/mxreg Ularity;if (Mareadensity[row][col] < areacapacity) {//zone density does not exceed qualification, place view into the region int xoffset = (int) colw-childw;//Area width and The difference in the width of the sub-view, the difference can be used to do the position within the region of the random if (xoffset <= 0) {//Description sub-view of the wide comparison of large xoffset = 1;} int yoffset = (int) rowh-childh;if (yoffset <= 0) {//Description sub view of high compare large Yoffset = 1;} Ok left, equal to area width * left area paraMs.mleft = Getpaddingleft () + (int) (COLW * col + mrdm.nextint (xoffset)); int rightedge = Contentright-childw;if (params. Mleft > Rightedge) {//plus the width of the child view cannot go beyond the right edge Params.mleft = Rightedge;} Params.mright = params.mleft + childw;params.mtop = getpaddingtop () + (int) (ROWH * row + mrdm.nextint (yoffset)); int Botto Medge = contentbottom-childh;if (Params.mtop > Bottomedge) {//plus the width of the child view cannot go beyond the right border Params.mtop = Bottomedge;} Params.mbottom = Params.mtop + childh;if (!isoverlap (params)) {//Determine if the mareadensity[row][col]++;//does not overlap with other view, Add the density of the area to 1child.layout (Params.mleft, Params.mtop, Params.mright, params.mbottom);//Layout Sub-viewmfixedviews.add (child); /Add to the already-laid set of break;} else {//if overlapping, remove the area, Availareas.remove (ARRAYIDX); availareacount--;}} else {//zone density exceeds qualification, remove Availareas.remove (ARRAYIDX) from optional region; availareacount--;}}} mlayouted = true;}
To be honest, this long code analysis is really a bit laborious, the necessary part I added a note, here is not much to say.
A gesture was added to the stellarmap to interact with the user when swiping:
@Overridepublic boolean onfling (motionevent E1, motionevent E2, float Velocityx, float velocityy) {int CenterX = Getmeasur Edwidth ()/2;int centery = Getmeasuredwidth ()/2;int x1 = (int) e1.getx ()-Centerx;int y1 = (int) e1.gety ()-Centery;i NT x2 = (int) e2.getx ()-Centerx;int y2 = (int) e2.gety ()-Centery;if ((x1 * x1 + y1 * y1) > (x2 * x2 + y2 * y2)) {zo Omout ();} else {zoomin ();} return true;}
/** sets the animation into group */public void ZoomIn () {final int nextgroupindex = Madapter.getnextgrouponzoom (Mshowngroupindex, true); Switchgroup (Nextgroupindex, True, Mzoominnearanim, Mzoominawayanim);} /** to group set out draw */public void Zoomout () {final int nextgroupindex = Madapter.getnextgrouponzoom (Mshowngroupindex, False) ; Switchgroup (Nextgroupindex, True, Mzoomoutnearanim, Mzoomoutawayanim);}
We finally recalled our Switchgroup method.
The demo is attached at the end of this article for reference.
Android custom control fly-in fly-out control