Skip to content

Commit 811e81a

Browse files
committed
Improve RingEffectView
1 parent 876cda0 commit 811e81a

File tree

4 files changed

+66
-28
lines changed

4 files changed

+66
-28
lines changed

circle-menu-simple-example/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
<com.ramotion.circlemenu.CircleMenuView
1010
android:id="@+id/circle_menu"
11-
android:layout_width="300dp"
12-
android:layout_height="300dp"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
1313
app:button_colors="@array/colors"
1414
app:button_icons="@array/icons"
1515
app:layout_constraintBottom_toBottomOf="parent"

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

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
*/
3636
public class CircleMenuView extends FrameLayout implements View.OnClickListener {
3737

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+
3842
/**
3943
* CircleMenu event listener.
4044
*/
@@ -91,6 +95,9 @@ public void onButtonClickAnimationEnd(@NonNull CircleMenuView view, int buttonIn
9195
private int mDurationRing;
9296
private int mDurationOpen;
9397
private int mDurationClose;
98+
private int mDesiredSize;
99+
private int mRingRadius;
100+
94101
private float mDistance;
95102

96103
private final List<View> mButtons = new ArrayList<>();
@@ -123,6 +130,9 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs,
123130
// TODO: add setter
124131
final int menuButtonColor;
125132

133+
final float density = context.getResources().getDisplayMetrics().density;
134+
final float defaultDistance = DEFAULT_DISTANCE * density;
135+
126136
if (attrs != null) {
127137
final TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleMenuView, 0, 0);
128138
try {
@@ -155,7 +165,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs,
155165
mDurationOpen = a.getInteger(R.styleable.CircleMenuView_duration_open, getResources().getInteger(android.R.integer.config_mediumAnimTime));
156166
mDurationClose = a.getInteger(R.styleable.CircleMenuView_duration_close, getResources().getInteger(android.R.integer.config_mediumAnimTime));
157167

158-
mDistance = a.getDimension(R.styleable.CircleMenuView_distance, -1);
168+
mDistance = a.getDimension(R.styleable.CircleMenuView_distance, defaultDistance);
159169

160170
menuButtonColor = a.getColor(R.styleable.CircleMenuView_icon_color, Color.WHITE);
161171
} finally {
@@ -169,11 +179,15 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs,
169179
mDurationOpen = getResources().getInteger(android.R.integer.config_mediumAnimTime);
170180
mDurationClose = getResources().getInteger(android.R.integer.config_mediumAnimTime);
171181

172-
mDistance = -1;
182+
mDistance = defaultDistance;
173183

174184
menuButtonColor = Color.WHITE;
175185
}
176186

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+
177191
if (icons == null || colors == null) {
178192
throw new IllegalArgumentException("No buttons icons or colors set");
179193
}
@@ -238,9 +252,17 @@ public void onAnimationEnd(Animator animation) {
238252
mButtons.add(button);
239253
}
240254

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);
244266
}
245267

246268
@Override
@@ -261,18 +283,8 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
261283

262284
mMenuButton.getContentRect(mButtonRect);
263285

264-
mRingView.setX(getWidth() / 2f - mRingView.getWidth() / 2f);
265-
mRingView.setY(getHeight() / 2f - mRingView.getHeight() / 2f);
266286
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);
276288
}
277289

278290
@Override
@@ -330,8 +342,8 @@ public void onAnimationEnd(Animator animation) {
330342
mRingView.setStrokeColor(button.getBackgroundTintList().getDefaultColor());
331343

332344
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);
335347
final ObjectAnimator visible = ObjectAnimator.ofFloat(mRingView, "alpha", 1f, 0f);
336348

337349
final AnimatorSet lastSet = new AnimatorSet();

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import android.graphics.Paint;
66
import android.graphics.RectF;
77
import android.support.annotation.FloatRange;
8+
import android.support.annotation.Nullable;
9+
import android.util.AttributeSet;
810
import android.view.View;
911

1012

@@ -16,25 +18,26 @@ public class RingEffectView extends View {
1618

1719
private float mAngle;
1820
private float mStartAngle;
21+
private int mRadius;
1922

2023
public RingEffectView(Context context) {
21-
super(context);
24+
this(context, null);
25+
}
26+
27+
public RingEffectView(Context context, @Nullable AttributeSet attrs) {
28+
super(context, attrs);
2229

2330
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
2431
mPaint.setStyle(Paint.Style.STROKE);
2532
mPaint.setStrokeCap(Paint.Cap.ROUND);
2633
}
2734

28-
@Override
29-
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
30-
super.onSizeChanged(w, h, oldw, oldh);
31-
mRingRect = new RectF(0, 0, w, h);
32-
}
33-
3435
@Override
3536
protected void onDraw(Canvas canvas) {
3637
super.onDraw(canvas);
37-
canvas.drawArc(mRingRect, mStartAngle, mAngle, false, mPaint);
38+
if (mRingRect != null) {
39+
canvas.drawArc(mRingRect, mStartAngle, mAngle, false, mPaint);
40+
}
3841
}
3942

4043
@Override
@@ -73,4 +76,21 @@ public void setStrokeWidth(int width) {
7376
mPaint.setStrokeWidth(width);
7477
}
7578

79+
public void setRadius(int radius) {
80+
mRadius = radius;
81+
final int w = getMeasuredWidth();
82+
final int h = getMeasuredHeight();
83+
84+
final int wo = (w - radius * 2) / 2;
85+
final int ho = (h - radius * 2) / 2;
86+
87+
final float sw = mPaint.getStrokeWidth() * 0.5f;
88+
89+
mRingRect = new RectF(wo + sw, ho + sw, w - wo - sw, h - ho - sw);
90+
}
91+
92+
public int getRadius() {
93+
return mRadius;
94+
}
95+
7696
}

circle-menu/src/main/res/layout/circle_menu.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
android:layout_width="match_parent"
66
android:layout_height="match_parent">
77

8+
<com.ramotion.circlemenu.RingEffectView
9+
android:id="@+id/ring_view"
10+
android:layout_gravity="center"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent"/>
13+
814
<android.support.design.widget.FloatingActionButton
915
android:id="@+id/circle_menu_main_button"
1016
android:layout_gravity="center"

0 commit comments

Comments
 (0)