Recently in the study of custom controls, inadvertently saw the mobile phone on the "Android Market" desktop hover menu, feel very tall, so I simply look like do it again:
First look at the original image:
The knowledge involved:
① first the control is a custom viewgroup, you need to know the usage of onmeasure, onlayout in the custom control.
Onmeasure: Mainly calculates the width height of the current control based on the recommended width at the previous level, and then measures each child view in the current control.
OnLayout: lays out each child view in the current control.
② Junior High School trigonometric function, inverse trigonometric function.
For example:
In mathematics: Sina = pairs/Oblique here a angle value.
But in Java: Sina = pair/Skew here a radian value, so in Java to get an angle you need to use the Math.toradians (a) function to convert the angle to radians.
Okay, back to the code:
First, Activity_main.xml.
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "android:backg Round= "#e0000000" android:gravity= "center" > <cn.example.com.mycustomwidget_06_circlemenu_01.view. Customcircleview android:id= "@+id/custom_father_rl" android:background= "@drawable/gears_box_float_main_backg Round "android:layout_width=" 355dip "android:layout_height=" 301dip "> <relativelayout Android:id= "@id/custom_circle_mean_center" android:layout_width= "Wrap_content" Android:layout_heigh t= "Wrap_content" > <imageview android:layout_centerinparent= "true" android:b ackground= "@drawable/gears_box_float_big_hexagon" android:layout_width= "167dip" Android:layo ut_height= "187dip"/> <imageview Android:layout_centerinparent= "true" android:background= "@drawable/gears_box_float_big_hexagon_mas K "android:layout_width=" 152dip "android:layout_height=" 172dip "/> </relativ Elayout> </cn.example.com.mycustomwidget_06_circlemenu_01.view.CustomCircleView></LinearLayout>
Then the layout of each of our entries is the item file Circle_mean_item.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:o rientation= "vertical" android:gravity= "center" > <imageview android:id= "@id/custom_ Circle_mean_iv " android:background=" @drawable/gears_box_float_update_pressed " android:layout_width=" 20dip " android:layout_height=" 20dip "/> <textview android:textsize=" 13sp " android:id=" @id/custom_circle_mean_tv " android:textcolor=" @android: Color/white " android:text=" Upgrade " Android: Layout_width= "Wrap_content" android:layout_height= "Wrap_content"/></linearlayout>
Finally, a custom control that inherits ViewGroup Customcircleview
Package Cn.example.com.mycustomwidget_06_circlemenu_01.view;import Android.content.context;import Android.util.attributeset;import Android.util.displaymetrics;import Android.util.log;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.view.windowmanager;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.textview;import Java.util.zip.inflater;import cn.example.com.mycustomwidget_06_circlemenu_01.r;/* * * Created by Administrator on 2015/5/27. */public class Customcircleview extends viewgroup{private Onmeanitemclicklistenner listenner; ITEM size is one-eighth private static final float mean_item_size = 1/6f for the parent control size; The most intermediate ITEM size is one-sixth private static final float mean_item_center_size = 1/3f for the parent control size; The PADDING size between ITEM is One-twelveth private static final float mean_item_padding = 1/12f, which is the size of the parent control; private static final String TAG = CustomCircleView.class.getSimpleName (); diameter private float diameter;Mean item size float meanitemsize; Middle of the mean item size float meanitemcentersize; Public Customcircleview (context context, AttributeSet Attrs) {Super (context, attrs); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeas Urespec, Heightmeasurespec); int mwidth,mheight; int widthsize = measurespec.getsize (Widthmeasurespec); int widthmode = Measurespec.getmode (Widthmeasurespec); int heightsize = measurespec.getsize (Heightmeasurespec); int heightmode = Measurespec.getmode (Heightmeasurespec); if (widthmode! = measurespec.exactly | | Heightmode! = measurespec.exactly) {widthsize = Getsuggestedminimumwidth (); Heightsize = Getsuggestedminimumheight (); Widthsize = Widthsize = = 0? Getdefaultsize (): widthsize; Heightsize = Heightsize = = 0? Getdefaultsize (): heightsize; Setmeasureddimension (widthsize,heightsize); }else{mwidth = Mheight = Math.min (widthsize,heightsize); Setmeasureddimension (Mwidth,mheight); } int measurespec = 0; diameter = Math.min (Getmeasuredheight (), Getmeasuredwidth ()); meanitemsize = diameter * mean_item_size; meanitemcentersize = diameter * mean_item_center_size; int meanitemmode = measurespec.exactly; /** * measure mean item */int meanitemcount = Getchildcount () in turn; for (int i=0;i<meanitemcount;i++) {View Childview = Getchildat (i); if (childview.getid () = = R.id.custom_circle_mean_center) {measurespec = measurespec.makemeasurespec (int) Me Anitemcentersize,meanitemmode); Childview.measure (MEASURESPEC,MEASURESPEC); }else{Measurespec = Measurespec.makemeasurespec ((int) meanitemsize,meanitemmode); Childview.measure (MEASURESPEC,MEASURESPEC); }}} @Override Protected void OnLayout (Boolean changed, int l, int t, int r, int b) {/** * sequential layout */int mean ItemCount = Getchildcount (); LOG.D (TAG, "Meanitemcount:" +meanitemcount); float angle = 360/meanitemcount; float padding = diameter * mean_item_padding; float Itemangle = 0; Right triangle's hypotenuse float hypotenuse = (DIAMETER/2)-padding-(MEANITEMSIZE/2); for (int i=0;i<meanitemcount;i++) {View Childview = Getchildat (i); if (childview.getid () = = R.id.custom_circle_mean_center) {continue; }else{Itemangle%= 360; int left = (int) (DIAMETER/2 + (int) (Math.Cos (Math.toradians (itemangle)) * hypotenuse)-MEANITEMSIZE/2); int top = (int) (DIAMETER/2 + (int) (Math.sin (Math.toradians (itemangle)) * hypotenuse)-MEANITEMSIZE/2); int righe = (int) (left+meanitemsize); int buttom = (int) (Top+meanitemsize); Childview.layout (left, top, Righe, buttom); TODO: If you are drawing the last control, merge if (i = = meanItemCount-2) {itemangle+= (ANGLE+ANGLE/2); }else{Itemangle + = angle; }}} View Meancenteritem = Findviewbyid (R.id.custom_circle_mean_center); int cleft = (int) (DIAMETER/2-MEANITEMCENTERSIZE/2); int ctop = (int) (diameter/2-MEANITEMCENTERSIZE/2); int cright = (int) (cleft + meanitemcentersize); int cbuttom = (int) (ctop + meanitemcentersize); Meancenteritem.layout (Cleft, ctop, Cright, Cbuttom); Meancenteritem.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { Listenner.oncentermeanitemclick (); } }); }/** * Gets the default layout size * @return */private int getdefaultsize () {WindowManager WindowManager = (Window Manager) GetCOntext (). Getsystemservice (Context.window_service); Displaymetrics displaymetrics = new Displaymetrics (); Windowmanager.getdefaultdisplay (). Getmetrics (Displaymetrics); Return Math.min (Displaymetrics.widthpixels,displaymetrics.heightpixels); }/** * Set text and background * @param texts * @param resids */public void Settextsandresids (String texts[],int R Esids[]) {if (texts = = NULL | | resids = = NULL) {throw new IllegalArgumentException ("text and picture cannot be empty"); } int itemCount = Math.min (texts.length,resids.length); Layoutinflater inflater = Layoutinflater.from (GetContext ()); for (int i =0;i<itemcount;i++) {final int j = i; View Childview = inflater.inflate (R.layout.circle_mean_item,this,false); ImageView IV = (ImageView) Childview.findviewbyid (R.ID.CUSTOM_CIRCLE_MEAN_IV); TextView TV = (TextView) Childview.findviewbyid (R.ID.CUSTOM_CIRCLE_MEAN_TV); Iv.setbackgroundresOurce (Resids[i]); Tv.settext (Texts[i]); Iv.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { if (Listenner! = null) {Listenner.onmeanitemclick (j); } } }); AddView (Childview); }} public void Setonmeanitemclicklistener (Onmeanitemclicklistenner listenner) {this.listenner = Listenner; }/** * MENA Item Click event Interface */public interface onmeanitemclicklistenner{void Onmeanitemclick (int posi tion); void Oncentermeanitemclick (); }}
And finally, our mainactivity.
Package Cn.example.com.mycustomwidget_06_circlemenu_01;import Android.app.activity;import Android.os.Bundle; Import Android.widget.toast;import Cn.example.com.mycustomwidget_06_circlemenu_01.view. Customcircleview;import static Cn.example.com.mycustomwidget_06_circlemenu_01.view. Customcircleview.onmeanitemclicklistenner;public class Mainactivity extends Activity {private final string[] texts = n EW string[]{"Software Upgrade", "Scan", "Junk cleanup", "Share", "set"};//r.drawable.gears_box_float_update_normal private int[] resids = new int []{R.drawable.updata_selector, R.drawable.scan_selector, R.drawable.clean_selector, R.drawable.share_selector, r.drawable.setting_selector}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Customcircleview CCV = (customcircleview) Findviewbyid (R.ID.CUSTOM_FATHER_RL); Ccv.settextsandresidS (texts,resids); Ccv.setonmeanitemclicklistener (New Customcircleview.onmeanitemclicklistenner () {@Override public voi d onmeanitemclick (int position) {Toast.maketext (mainactivity.this,texts[position].tostring (), toast.length_s Hort). Show (); } @Override public void Oncentermeanitemclick () {Toast.maketext (mainactivity.this, "ce Nter click! ", Toast.length_short). Show (); } }); }}
This is the end!!! Look at it!
SOURCE Download: http://download.csdn.net/detail/yushanfenghailin/8759033
Reprint please indicate source: http://blog.csdn.net/yushanddddfenghailin/article/details/46299819
Custom Controls VI: High-imitation Android Market desktop hover Menu