Skip to content

Commit 0c59e91

Browse files
committed
Refactor the code of menu position counting
1 parent c53fe07 commit 0c59e91

File tree

3 files changed

+59
-79
lines changed

3 files changed

+59
-79
lines changed

androidarcmenu/src/main/java/com/hackplan/androidarcmenu/ArcMenuLayout.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.Context;
66
import android.graphics.Canvas;
77
import android.graphics.Point;
8+
import android.graphics.PointF;
89
import android.graphics.Rect;
910
import android.support.v4.view.MotionEventCompat;
1011
import android.util.AttributeSet;
@@ -18,6 +19,9 @@
1819

1920
import com.hackplan.androidarcmenu.ArcMenu.OnClickMenuListener;
2021

22+
import java.util.ArrayList;
23+
import java.util.List;
24+
2125
/**
2226
* Created by Dacer on 12/11/2016.
2327
*/
@@ -47,33 +51,38 @@ public boolean isShow() {
4751
return show;
4852
}
4953

50-
private float xFirst, yFirst, xEnd, yEnd;
51-
private int radius = (int)dpToPx(80f);
52-
private double radAlpha = Math.toRadians(90d);
54+
private ArrayList<PointF> menuPoints = new ArrayList<>();
55+
private int radius = (int) dpToPx(80f);
56+
private double arcRadians = Math.toRadians(90d);
57+
5358
public void show(ArcMenu arcMenu, int x, int y, boolean hideOnTouchUp) {
59+
if (getChildCount() <= 0) return;
5460
this.hideOnTouchUp = hideOnTouchUp;
5561
this.arcMenu = arcMenu;
5662
show = true;
63+
if (x == mScreenRect.centerX() && y == mScreenRect.centerY()) y += 1;
64+
5765
touchPoint.set(x, y);
58-
double radO = Math.PI - radAlpha/2 -
59-
Math.atan((touchPoint.x - mScreenRect.centerX()) / (touchPoint.y - mScreenRect.centerY()));
60-
double rad2 = Math.atan((touchPoint.x - mScreenRect.centerX()) / (touchPoint.y - mScreenRect.centerY())) - radAlpha/2;
61-
xFirst = touchPoint.x - ((float) (radius * Math.sin(radO)));
62-
yFirst = touchPoint.y + ((float) (Math.cos(radO) * radius));
63-
xEnd = touchPoint.x - ((float) (radius * Math.sin(rad2)));
64-
yEnd = touchPoint.y - ((float) (Math.cos(rad2) * radius));
66+
menuPoints.clear();
67+
double alpha = Math.atan((double) (y - mScreenRect.centerY()) / (x - mScreenRect.centerX()));
68+
if (x < mScreenRect.centerX()) alpha += Math.PI;
69+
for (int i=0; i<getChildCount(); i++) {
70+
double b;
71+
if (getChildCount() == 1) {
72+
b = arcRadians / 2;
73+
}else {
74+
b = i * (1D / (getChildCount() - 1)) * arcRadians;
75+
}
76+
menuPoints.add(PointUtils.getPoint(new PointF(x, y), radius,
77+
Math.PI + alpha - arcRadians / 2 + b));
78+
}
6579
requestLayout();
6680
}
67-
81+
6882
public void setOnClickMenuListener(OnClickMenuListener onClickMenuListener) {
6983
this.onClickMenuListener = onClickMenuListener;
7084
}
7185

72-
@Override
73-
protected void onDraw(Canvas canvas) {
74-
super.onDraw(canvas);
75-
}
76-
7786
@Override
7887
protected void onAttachedToWindow() {
7988
super.onAttachedToWindow();
@@ -84,34 +93,27 @@ protected void onAttachedToWindow() {
8493

8594
@Override
8695
protected void onLayout(boolean changed, int l, int t, int r, int b) {
87-
if (!show) return ;
96+
if (!show) return;
8897
int childCount = this.getChildCount();
8998
for (int i = 0; i < childCount; i++) {
9099
View child = this.getChildAt(i);
91100
child.setAlpha(0L);
92-
if (i == 0) {
93-
child.layout((int)xFirst - child.getMeasuredWidth()/2,
94-
(int)yFirst - child.getMeasuredHeight()/2,
95-
(int)xFirst + child.getMeasuredWidth()/2,
96-
(int)yFirst + child.getMeasuredHeight()/2);
97-
} else {
98-
99-
child.layout((int)xEnd - child.getMeasuredWidth()/2,
100-
(int)yEnd - child.getMeasuredHeight()/2,
101-
(int)(xEnd + child.getMeasuredWidth()/2),
102-
(int)(yEnd + child.getMeasuredHeight()/2));
103-
}
101+
child.layout((int) (menuPoints.get(i).x - child.getMeasuredWidth() / 2),
102+
(int) (menuPoints.get(i).y - child.getMeasuredHeight() / 2),
103+
(int) (menuPoints.get(i).x + child.getMeasuredWidth() / 2),
104+
(int) (menuPoints.get(i).y + child.getMeasuredHeight() / 2));
104105
}
105106
AnimatorUtils.showMenu(this, touchPoint, animListener);
106107
}
107108

108109
private int lastFocusIndex = -1;
110+
109111
@Override
110112
public boolean onTouchEvent(MotionEvent event) {
111113
float x = MotionEventCompat.getX(event, 0);
112114
float y = MotionEventCompat.getY(event, 0);
113115

114-
switch (event.getAction()){
116+
switch (event.getAction()) {
115117
case MotionEvent.ACTION_DOWN:
116118
hideOnTouchUp = true;
117119
break;
@@ -123,12 +125,12 @@ public boolean onTouchEvent(MotionEvent event) {
123125
AnimatorUtils.openMenu(this, lastFocusIndex, animListener);
124126
if (onClickMenuListener != null) {
125127
View clickedView = getChildAt(lastFocusIndex);
126-
onClickMenuListener.onClickArcMenu(arcMenu, (int)clickedView.getTag());
128+
onClickMenuListener.onClickArcMenu(arcMenu, (int) clickedView.getTag());
127129
}
128130
} else if (hideOnTouchUp) {
129131
AnimatorUtils.hideMenu(this, touchPoint);
130132
show = false;
131-
}else {
133+
} else {
132134
hideOnTouchUp = true;
133135
}
134136
lastFocusIndex = -1;
@@ -137,12 +139,13 @@ public boolean onTouchEvent(MotionEvent event) {
137139
if (!animFinished) break;
138140
tempRect = new Rect();
139141
boolean isOverMenu = false;
140-
for (int i=0; i<getChildCount(); i++) {
142+
for (int i = 0; i < getChildCount(); i++) {
141143
getChildAt(i).getGlobalVisibleRect(tempRect);
142-
if (tempRect.contains((int)x, (int)y)) {
144+
if (tempRect.contains((int) x, (int) y)) {
143145
isOverMenu = true;
144146
if (lastFocusIndex == i) break;
145-
if (lastFocusIndex != -1) AnimatorUtils.clearFocusChild(this, lastFocusIndex);
147+
if (lastFocusIndex != -1)
148+
AnimatorUtils.clearFocusChild(this, lastFocusIndex);
146149
AnimatorUtils.focusChild(this, i);
147150
lastFocusIndex = i;
148151
break;
@@ -163,9 +166,9 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
163166
int w = resolveSizeAndState(0, widthMeasureSpec, 1);
164167
int h = resolveSizeAndState(0, heightMeasureSpec, 0);
165168
int childCount = getChildCount();
166-
for(int i = 0 ; i < childCount ; i ++){
169+
for (int i = 0; i < childCount; i++) {
167170
View children = getChildAt(i);
168-
measureChild(children,widthMeasureSpec,heightMeasureSpec);
171+
measureChild(children, widthMeasureSpec, heightMeasureSpec);
169172
}
170173
setMeasuredDimension(w, h);
171174
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.hackplan.androidarcmenu;
2+
3+
import android.graphics.PointF;
4+
5+
class PointUtils {
6+
/**
7+
* Polar coordinates to Rectangular coordinates
8+
*
9+
* @param origin of the coordinate system
10+
* @param distance this really means "radius"
11+
* @param angle from the x-axis in radians; positive increases in the counterclockwise direction
12+
*/
13+
static PointF getPoint(PointF origin, float distance, double angle) {
14+
PointF newPoint = new PointF();
15+
newPoint.x = origin.x + (float) (distance * Math.cos(angle));
16+
newPoint.y = origin.y + (float) (distance * Math.sin(angle));
17+
return newPoint;
18+
}
19+
}

androidarcmenu/src/main/java/com/hackplan/androidarcmenu/Utils.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)