35
35
*/
36
36
public class CircleMenuView extends FrameLayout implements View .OnClickListener {
37
37
38
+ private static int DEFAULT_BUTTON_SIZE = 56 ;
39
+ private static float DEFAULT_DISTANCE = DEFAULT_BUTTON_SIZE * 1.5f ;
40
+ private static float DEFAULT_RING_SCALE_RATIO = 1.3f ;
41
+
38
42
/**
39
43
* CircleMenu event listener.
40
44
*/
@@ -91,6 +95,9 @@ public void onButtonClickAnimationEnd(@NonNull CircleMenuView view, int buttonIn
91
95
private int mDurationRing ;
92
96
private int mDurationOpen ;
93
97
private int mDurationClose ;
98
+ private int mDesiredSize ;
99
+ private int mRingRadius ;
100
+
94
101
private float mDistance ;
95
102
96
103
private final List <View > mButtons = new ArrayList <>();
@@ -123,6 +130,9 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs,
123
130
// TODO: add setter
124
131
final int menuButtonColor ;
125
132
133
+ final float density = context .getResources ().getDisplayMetrics ().density ;
134
+ final float defaultDistance = DEFAULT_DISTANCE * density ;
135
+
126
136
if (attrs != null ) {
127
137
final TypedArray a = context .getTheme ().obtainStyledAttributes (attrs , R .styleable .CircleMenuView , 0 , 0 );
128
138
try {
@@ -155,7 +165,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs,
155
165
mDurationOpen = a .getInteger (R .styleable .CircleMenuView_duration_open , getResources ().getInteger (android .R .integer .config_mediumAnimTime ));
156
166
mDurationClose = a .getInteger (R .styleable .CircleMenuView_duration_close , getResources ().getInteger (android .R .integer .config_mediumAnimTime ));
157
167
158
- mDistance = a .getDimension (R .styleable .CircleMenuView_distance , - 1 );
168
+ mDistance = a .getDimension (R .styleable .CircleMenuView_distance , defaultDistance );
159
169
160
170
menuButtonColor = a .getColor (R .styleable .CircleMenuView_icon_color , Color .WHITE );
161
171
} finally {
@@ -169,11 +179,15 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs,
169
179
mDurationOpen = getResources ().getInteger (android .R .integer .config_mediumAnimTime );
170
180
mDurationClose = getResources ().getInteger (android .R .integer .config_mediumAnimTime );
171
181
172
- mDistance = - 1 ;
182
+ mDistance = defaultDistance ;
173
183
174
184
menuButtonColor = Color .WHITE ;
175
185
}
176
186
187
+ final float buttonSize = DEFAULT_BUTTON_SIZE * density ;
188
+ mRingRadius = (int ) (buttonSize + (mDistance - buttonSize / 2 ));
189
+ mDesiredSize = (int ) (mRingRadius * 2 * DEFAULT_RING_SCALE_RATIO );
190
+
177
191
if (icons == null || colors == null ) {
178
192
throw new IllegalArgumentException ("No buttons icons or colors set" );
179
193
}
@@ -238,9 +252,17 @@ public void onAnimationEnd(Animator animation) {
238
252
mButtons .add (button );
239
253
}
240
254
241
- mRingView = new RingEffectView (context );
242
- mRingView .setStrokeColor (Color .RED );
243
- addView (mRingView );
255
+ mRingView = findViewById (R .id .ring_view );
256
+ }
257
+
258
+ @ Override
259
+ protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
260
+ super .onMeasure (widthMeasureSpec , heightMeasureSpec );
261
+
262
+ final int w = resolveSizeAndState (mDesiredSize , widthMeasureSpec , 0 );
263
+ final int h = resolveSizeAndState (mDesiredSize , heightMeasureSpec , 0 );
264
+
265
+ setMeasuredDimension (w , h );
244
266
}
245
267
246
268
@ Override
@@ -261,18 +283,8 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
261
283
262
284
mMenuButton .getContentRect (mButtonRect );
263
285
264
- mRingView .setX (getWidth () / 2f - mRingView .getWidth () / 2f );
265
- mRingView .setY (getHeight () / 2f - mRingView .getHeight () / 2f );
266
286
mRingView .setStrokeWidth (mButtonRect .width ());
267
-
268
- if (mDistance == -1 ) {
269
- mDistance = mButtonRect .width () * 1.5f ;
270
- }
271
-
272
- final LayoutParams lp = (LayoutParams ) mRingView .getLayoutParams ();
273
- lp .width = (int ) (mDistance * 2 );
274
- lp .height = (int ) (mDistance * 2 );
275
- mRingView .setLayoutParams (lp );
287
+ mRingView .setRadius (mRingRadius );
276
288
}
277
289
278
290
@ Override
@@ -330,8 +342,8 @@ public void onAnimationEnd(Animator animation) {
330
342
mRingView .setStrokeColor (button .getBackgroundTintList ().getDefaultColor ());
331
343
332
344
final ObjectAnimator ring = ObjectAnimator .ofFloat (mRingView , "angle" , 360 );
333
- final ObjectAnimator scaleX = ObjectAnimator .ofFloat (mRingView , "scaleX" , 1f , 1.3f );
334
- final ObjectAnimator scaleY = ObjectAnimator .ofFloat (mRingView , "scaleY" , 1f , 1.3f );
345
+ final ObjectAnimator scaleX = ObjectAnimator .ofFloat (mRingView , "scaleX" , 1f , DEFAULT_RING_SCALE_RATIO );
346
+ final ObjectAnimator scaleY = ObjectAnimator .ofFloat (mRingView , "scaleY" , 1f , DEFAULT_RING_SCALE_RATIO );
335
347
final ObjectAnimator visible = ObjectAnimator .ofFloat (mRingView , "alpha" , 1f , 0f );
336
348
337
349
final AnimatorSet lastSet = new AnimatorSet ();
0 commit comments