@@ -38,6 +38,7 @@ public class CircleMenuView extends FrameLayout implements View.OnClickListener
38
38
private static final int DEFAULT_BUTTON_SIZE = 56 ;
39
39
private static final float DEFAULT_DISTANCE = DEFAULT_BUTTON_SIZE * 1.5f ;
40
40
private static final float DEFAULT_RING_SCALE_RATIO = 1.3f ;
41
+ private static final float DEFAULT_CLOSE_ICON_ALPHA = 0.3f ;
41
42
42
43
private final List <View > mButtons = new ArrayList <>();
43
44
private final Rect mButtonRect = new Rect ();
@@ -204,20 +205,12 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
204
205
return ;
205
206
}
206
207
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
-
215
208
mMenuButton .getContentRect (mButtonRect );
216
209
217
210
mRingView .setStrokeWidth (mButtonRect .width ());
218
211
mRingView .setRadius (mRingRadius );
219
212
220
- final LayoutParams lp = (LayoutParams ) mRingView .getLayoutParams ();//new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
213
+ final LayoutParams lp = (LayoutParams ) mRingView .getLayoutParams ();
221
214
lp .width = right - left ;
222
215
lp .height = bottom - top ;
223
216
mRingView .setLayoutParams (lp );
@@ -251,6 +244,7 @@ public void onAnimationEnd(Animator animation) {
251
244
private void initLayout (@ NonNull Context context ) {
252
245
LayoutInflater .from (context ).inflate (R .layout .circle_menu , this , true );
253
246
247
+ setWillNotDraw (true );
254
248
setClipChildren (false );
255
249
setClipToPadding (false );
256
250
@@ -328,6 +322,20 @@ private void initButtons(@NonNull Context context, @NonNull List<Integer> icons,
328
322
}
329
323
}
330
324
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
+
331
339
private Animator getButtonClickAnimation (final @ NonNull FloatingActionButton button ) {
332
340
final int buttonNumber = mButtons .indexOf (button ) + 1 ;
333
341
final float stepAngle = 360f / mButtons .size ();
@@ -417,7 +425,7 @@ public void onAnimationEnd(Animator animation) {
417
425
private Animator getOpenMenuAnimation () {
418
426
mClosedState = false ;
419
427
420
- final ObjectAnimator alphaAnimation = ObjectAnimator .ofFloat (mMenuButton , "alpha" , 0.3f );
428
+ final ObjectAnimator alphaAnimation = ObjectAnimator .ofFloat (mMenuButton , "alpha" , DEFAULT_CLOSE_ICON_ALPHA );
421
429
422
430
final Keyframe kf0 = Keyframe .ofFloat (0f , 0f );
423
431
final Keyframe kf1 = Keyframe .ofFloat (0.5f , 60f );
@@ -457,18 +465,7 @@ public void onAnimationStart(Animator animation) {
457
465
public void onAnimationUpdate (ValueAnimator valueAnimator ) {
458
466
final float fraction = valueAnimator .getAnimatedFraction ();
459
467
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 );
472
469
}
473
470
});
474
471
@@ -632,4 +629,59 @@ public EventListener getEventListener() {
632
629
return mListener ;
633
630
}
634
631
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
+
635
687
}
0 commit comments