Curved Menu (Android)
Preface: The company needs to write a curved menu of its own!
Effect:
Development environment: androidstudio2.2.1+gradle-2.14.1
Knowledge Involved: 1. Custom controls, 2. Event distribution, etc.
Part of the code:
Public classHomepagemenulayoutextendsViewGroup {Privatecontext Context; //the text of the menu item Privatestring[] Mitemtexts =NULL; Private intStatusheight;//Status Bar Height PublicHomepagemenulayout (Context context, AttributeSet attrs) {Super(context, attrs); This. Context =context; Statusheight=screenutils.getstatusheight (context); } /*** Set the width height of the layout and the Policy menu item width High*/ intReswidth = 0; intResheight = 0; intMradius = 0; @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { //layout wide height size set to screen size//set the size of the layoutsetmeasureddimension (Widthmeasurespec, Heightmeasurespec); /*** Obtain measurement mode and measured value separately according to the parameters passed in.*/ intwidth =measurespec.getsize (WIDTHMEASURESPEC); Resheight=measurespec.getsize (HEIGHTMEASURESPEC); Reswidth=measurespec.getsize (WIDTHMEASURESPEC); //Get RadiusMradius = (int) (RESHEIGHT/2-2 *statusheight); //Set Item size intChildsize = (int) (Mradius * 1/2); //menu item Measurement mode-precise mode intChildmode =measurespec.exactly; for(inti = 0; I < Getchildcount (); i++) { FinalView Child =Getchildat (i); if(child.getvisibility () = =GONE) { Continue; } //calculate the size of the menu item, and set the mode to measure the item intMakemeasurespec =-1; Makemeasurespec=Measurespec.makemeasurespec (childsize, Childmode); Child.measure (Makemeasurespec, Makemeasurespec); } } /*** The angle of the item layout*/ Private int[] Widthall =NULL; /*** The location of the item: the first parameter 1: This parameter indicates whether the size or position of the current viewgroup has changed * 2. When the drawing cursor is in the horizontal position * 3. Current drawing cursor Ordinate position*/@Overrideprotected voidOnLayout (BooleanChangedintLintTintRintb) {intLeft , top; intCwidth = (int) (Mradius * 1/2); Final intChildCount =Getchildcount (); //calculate the distance from the center point to the center of the menu item floatTMP = MRADIUS-CWIDTH/2; //Traverse to set the location of the MenuItem for(inti = 0; i < ChildCount; i++) { FinalView Child =Getchildat (i); if(child.getvisibility () = =GONE) { Continue; } Left= (int) (Mradius * Math.Cos (Math.toradians (widthall[i)))-65; Top= (int) (Mradius-(RESHEIGHT/2-2 * statusheight) * Math.sin (Math.toradians (Widthall[i]))-statusheight); Child.layout (left, top, left)+ cwidth, top +cwidth); } } Public InterfaceOnmenuitemclicklistener {voidItemClick (View view,intPOS); } Public voidSetonmenuitemclicklistener (Onmenuitemclicklistener monmenuitemclicklistener) { This. Monmenuitemclicklistener =Monmenuitemclicklistener; } //Number of Menus Private intMmenuitemcount; /*** Set icon and text for menu items*/ Public voidsetmenuitemiconsandtexts (string[] mitemtexts) { This. mitemtexts =mitemtexts; This. Mmenuitemcount =mitemtexts.length; Resultangle (); Addmenuitems (); } Private voidResultangle () {Switch( This. Mmenuitemcount) { Case3: Widthall=constants.item3; Break; Case4: Widthall=constants.item4; Break; Case5: Widthall=CONSTANTS.ITEM5; Break; Case6: Widthall=Constants.item6; Break; Case7: Widthall=Constants.item7; Break; Case8: Widthall=CONSTANTS.ITEM8; Break; Case9: Widthall=constants.item9; Break; Case10: Widthall=CONSTANTS.ITEM10; Break; default: Break; } } /*** Set icon and text for menu items*/ Public voidsetmenuitemiconsandtexts () {addmenuitems (); } Private intMmenuitemlayoutid =r.layout.homepage_item_layout; /*** MenuItem's Click event Interface*/ PrivateOnmenuitemclicklistener Monmenuitemclicklistener; Private floatYPosition = 0; /*** Add Menu item*/ Private voidAddmenuitems () {layoutinflater minflater=Layoutinflater.from (GetContext ()); /*** Initialize view based on user-set parameters*/ for(inti = 0; i < Mmenuitemcount; i++) { Final intj =i; View View= Minflater.inflate (Mmenuitemlayoutid, This,false); FinalImageView IV =(ImageView) view. Findviewbyid (R.ID.HOMEPAGE_PAGER1_ITEM_IMG); FinalTextView TV =(TextView) view. Findviewbyid (R.ID.HOMEPAGE_PAGER1_ITEM_TV); if(iv! =NULL) {Iv.setimageresource (r.mipmap.menu_ture); } if(TV! =NULL) {Tv.settext (mitemtexts[i]); } View.findviewbyid (R.id.homepage_item_layout). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {}}); View.findviewbyid (r.id.homepage_item_layout). Setontouchlistener (NewOntouchlistener () {@Override Public BooleanOnTouch (View V, motionevent event) {if(event.getaction () = =Motionevent.action_down) {YPosition= Event.gety ();//get the position pressedIv.setimageresource (R.mipmap.menu); } Else if(event.getaction () = =motionevent.action_up) {Iv.setimageresource (r.mipmap.menu_ture); floatDisplacement = Math.Abs (YPosition-event.gety ()); //precisely pressed position to respond if(Monmenuitemclicklistener! =NULL&&displacement<25) {Monmenuitemclicklistener.itemclick (v,j); } } Else if(event.getaction () = = Motionevent.action_cancel | | event.getaction () = =motionevent.action_pointer_up) {Iv.setimageresource (r.mipmap.menu_ture); } return true; } }); AddView (view); } }}
SOURCE Download ...
Curved Menu (Android)