Skip to content

Commit 184e76c

Browse files
committed
Merge branch 'develop'
2 parents 92faa67 + 7c811a3 commit 184e76c

File tree

2 files changed

+77
-22
lines changed

2 files changed

+77
-22
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ menu.setEventListener(new CircleMenuView.EventListener() {
119119
});
120120
```
121121

122+
You can use `open(boolean animate)` and `close(boolean animate)` methods,
123+
to open and close menu programmatically
124+
122125
Here are the attributes you can specify through XML or related setters:
123126
* `button_icons` - Array of buttons icons.
124127
* `button_colors` - Array of buttons colors.

circle-menu/src/main/java/com/ramotion/circlemenu/CircleMenuView.java

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class CircleMenuView extends FrameLayout implements View.OnClickListener
3838
private static final int DEFAULT_BUTTON_SIZE = 56;
3939
private static final float DEFAULT_DISTANCE = DEFAULT_BUTTON_SIZE * 1.5f;
4040
private static final float DEFAULT_RING_SCALE_RATIO = 1.3f;
41+
private static final float DEFAULT_CLOSE_ICON_ALPHA = 0.3f;
4142

4243
private final List<View> mButtons = new ArrayList<>();
4344
private final Rect mButtonRect = new Rect();
@@ -204,20 +205,12 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
204205
return;
205206
}
206207

207-
final float x = mMenuButton.getX();
208-
final float y = mMenuButton.getY();
209-
210-
for (View button: mButtons) {
211-
button.setX(x);
212-
button.setY(y);
213-
}
214-
215208
mMenuButton.getContentRect(mButtonRect);
216209

217210
mRingView.setStrokeWidth(mButtonRect.width());
218211
mRingView.setRadius(mRingRadius);
219212

220-
final LayoutParams lp = (LayoutParams) mRingView.getLayoutParams();//new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
213+
final LayoutParams lp = (LayoutParams) mRingView.getLayoutParams();
221214
lp.width = right - left;
222215
lp.height = bottom - top;
223216
mRingView.setLayoutParams(lp);
@@ -251,6 +244,7 @@ public void onAnimationEnd(Animator animation) {
251244
private void initLayout(@NonNull Context context) {
252245
LayoutInflater.from(context).inflate(R.layout.circle_menu, this, true);
253246

247+
setWillNotDraw(true);
254248
setClipChildren(false);
255249
setClipToPadding(false);
256250

@@ -328,6 +322,20 @@ private void initButtons(@NonNull Context context, @NonNull List<Integer> icons,
328322
}
329323
}
330324

325+
private void offsetAndScaleButtons(float centerX, float centerY, float angleStep, float offset, float scale) {
326+
for (int i = 0, cnt = mButtons.size(); i < cnt; i++) {
327+
final float angle = angleStep * i - 90;
328+
final float x = (float) Math.cos(Math.toRadians(angle)) * offset;
329+
final float y = (float) Math.sin(Math.toRadians(angle)) * offset;
330+
331+
final View button = mButtons.get(i);
332+
button.setX(centerX + x);
333+
button.setY(centerY + y);
334+
button.setScaleX(1.0f * scale);
335+
button.setScaleY(1.0f * scale);
336+
}
337+
}
338+
331339
private Animator getButtonClickAnimation(final @NonNull FloatingActionButton button) {
332340
final int buttonNumber = mButtons.indexOf(button) + 1;
333341
final float stepAngle = 360f / mButtons.size();
@@ -417,7 +425,7 @@ public void onAnimationEnd(Animator animation) {
417425
private Animator getOpenMenuAnimation() {
418426
mClosedState = false;
419427

420-
final ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(mMenuButton, "alpha", 0.3f);
428+
final ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(mMenuButton, "alpha", DEFAULT_CLOSE_ICON_ALPHA);
421429

422430
final Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
423431
final Keyframe kf1 = Keyframe.ofFloat(0.5f, 60f);
@@ -457,18 +465,7 @@ public void onAnimationStart(Animator animation) {
457465
public void onAnimationUpdate(ValueAnimator valueAnimator) {
458466
final float fraction = valueAnimator.getAnimatedFraction();
459467
final float value = (float)valueAnimator.getAnimatedValue();
460-
461-
for (int i = 0; i < buttonsCount; i++) {
462-
final float angle = angleStep * i - 90;
463-
final float x = (float) Math.cos(Math.toRadians(angle)) * value;
464-
final float y = (float) Math.sin(Math.toRadians(angle)) * value;
465-
466-
final View button = mButtons.get(i);
467-
button.setX(centerX + x);
468-
button.setY(centerY + y);
469-
button.setScaleX(1.0f * fraction);
470-
button.setScaleY(1.0f * fraction);
471-
}
468+
offsetAndScaleButtons(centerX, centerY, angleStep, value, fraction);
472469
}
473470
});
474471

@@ -632,4 +629,59 @@ public EventListener getEventListener() {
632629
return mListener;
633630
}
634631

632+
private void openOrClose(boolean open, boolean animate) {
633+
if (mIsAnimating) {
634+
return;
635+
}
636+
637+
if (open && !mClosedState) {
638+
return;
639+
}
640+
641+
if (!open && mClosedState) {
642+
return;
643+
}
644+
645+
if (animate) {
646+
mMenuButton.performClick();
647+
} else {
648+
mClosedState = !open;
649+
650+
final float centerX = mMenuButton.getX();
651+
final float centerY = mMenuButton.getY();
652+
653+
final int buttonsCount = mButtons.size();
654+
final float angleStep = 360f / buttonsCount;
655+
656+
final float offset = open ? mDistance : 0f;
657+
final float scale = open ? 1f : 0f;
658+
659+
mMenuButton.setImageResource(open ? mIconClose : mIconMenu);
660+
mMenuButton.setAlpha(open ? DEFAULT_CLOSE_ICON_ALPHA : 1f);
661+
662+
final int visibility = open ? View.VISIBLE : View.INVISIBLE;
663+
for (View view: mButtons) {
664+
view.setVisibility(visibility);
665+
}
666+
667+
offsetAndScaleButtons(centerX, centerY, angleStep, offset, scale);
668+
}
669+
}
670+
671+
/**
672+
* Open menu programmatically
673+
* @param animate open with animation or not
674+
*/
675+
public void open(boolean animate) {
676+
openOrClose(true, animate);
677+
}
678+
679+
/**
680+
* Close menu programmatically
681+
* @param animate close with animation or not
682+
*/
683+
public void close(boolean animate) {
684+
openOrClose(false, animate);
685+
}
686+
635687
}

0 commit comments

Comments
 (0)