Arc menu (Android), arc menu android
Arc menu (Android)
Preface:The company needs to write an arc menu!
Effect:
Development Environment: AndroidStudio2.2.1 + gradle-2.14.1
Knowledge:1. Custom Controls, 2. Event distribution, etc.
Some code:
Public class HomePageMenuLayout extends ViewGroup {private Context context; // The text private String [] mItemTexts = null; private int StatusHeight; // The height of the status bar is public HomePageMenuLayout (Context context, attributeSet attrs) {super (context, attrs); this. context = context; StatusHeight = ScreenUtils. getStatusHeight (context);}/*** set the layout width and height, and set the menu item width and height */int resWidth = 0; int resHeight = 0; int mRadi Us = 0; @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {// set the layout width and height to the screen size // set the layout size setMeasuredDimension (widthMeasureSpec, heightMeasureSpec);/*** obtain the Measurement Mode and measurement value based on the input parameters */int width = MeasureSpec. getSize (widthMeasureSpec); resHeight = MeasureSpec. getSize (heightMeasureSpec); resWidth = MeasureSpec. getSize (widthMeasureSpec); // obtain the radius mRadius = (int) (resHeight/2- 2 * StatusHeight); // set item size int childSize = (int) (mRadius * 1/2); // menu item Measurement Mode -- exact mode int childMode = MeasureSpec. EXACTLY; for (int I = 0; I <getChildCount (); I ++) {final View child = getChildAt (I); if (child. getVisibility () = GONE) {continue;} // calculates the size of the menu item, and measures the item in the configured mode. int makeMeasureSpec =-1; makeMeasureSpec = MeasureSpec. makeMeasureSpec (childSize, childMode); child. me Asure (makeMeasureSpec, makeMeasureSpec);}/*** item layout angle */private int [] widthall = null;/*** set the Item location: first parameter 1: This parameter indicates whether the size or position of the current ViewGroup has changed * 2. current drawing cursor x 3. current drawing cursor ordinate position */@ Override protected void onLayout (boolean changed, int l, int t, int r, int B) {int left, top; int cWidth = (int) (mRadius * 1/2); final int childCount = getChildCount (); // calculate the float tmp = mRadi distance from the center of the center to the center of the menu item Us-cWidth/2; // traverse to set the menuitem location for (int I = 0; I <childCount; I ++) {final View 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 interface OnMenuItemClickListener {void itemClick (View view, int pos);} public void setOnMenuItemClickListener (OnMenuItemClickListener mOnMenuItemClickListener) {this. mOnMenuItemClickListener = mOnMenuItemClickListener;} // Number of menus private int mMenuItemCount;/*** set the icon and text of the menu bar */public void setMenuItemIconsAndTexts (String [] mItemTexts) {this. mItemTexts = mItemTexts; this. mMenuItemCou Nt = mItemTexts. length; resultAngle (); addMenuItems ();} private void resultAngle () {switch (this. mMenuItemCount) {case 3: widthall = Constants. ITEM3; break; case 4: widthall = Constants. ITEM4; break; case 5: widthall = Constants. ITEM5; break; case 6: widthall = Constants. ITEM6; break; case 7: widthall = Constants. ITEM7; break; case 8: widthall = Constants. ITEM8; break; case 9: widthall = Consta NT. ITEM9; break; case 10: widthall = Constants. ITEM10; break; default: break;}/*** set the icon and text of the menu bar */public void setMenuItemIconsAndTexts () {addMenuItems ();} private int mMenuItemLayoutId = R. layout. listener;/*** MenuItem Click Event interface */private OnMenuItemClickListener mOnMenuItemClickListener; private float yPosition = 0;/*** add menu item */private void addMenuItems () {LayoutInflater m Inflater = LayoutInflater. from (getContext ();/*** initialize view */for (int I = 0; I <mMenuItemCount; I ++) based on the parameters set by the user) {final int j = I; View view = mInflater. inflate (mMenuItemLayoutId, this, false); final ImageView iv = (ImageView) view. findViewById (R. id. homepage_pager1_item_img); final TextView 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 (new OnClickListener () {@ Override public void onClick (View v) {}}); view. findViewById (R. id. homepage_item_layout ). setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {if (event. getAction () = MotionEvent. ACTION_DOWN) {yPosition = event. getY ();// Obtain the position iv. setImageResource (R. mipmap. menu);} else if (event. getAction () = MotionEvent. ACTION_UP) {iv. setImageResource (R. mipmap. menu_ture); float displacement = Math. abs (yPosition-event. getY (); // respond to the 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 );}}}
Download source code...