Skip to content

Commit bc152b1

Browse files
committed
Draw ring effect with Path.lineTo
1 parent d810f38 commit bc152b1

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,11 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
284284

285285
mRingView.setStrokeWidth(mButtonRect.width());
286286
mRingView.setRadius(mRingRadius);
287+
288+
final LayoutParams lp = (LayoutParams) mRingView.getLayoutParams();//new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
289+
lp.width = right - left;
290+
lp.height = bottom - top;
291+
mRingView.setLayoutParams(lp);
287292
}
288293

289294
@Override
@@ -314,10 +319,11 @@ public void onAnimationEnd(Animator animation) {
314319
private Animator getButtonClickAnimation(final @NonNull FloatingActionButton button) {
315320
final int buttonNumber = mButtons.indexOf(button) + 1;
316321
final float stepAngle = 360f / mButtons.size();
317-
final float startAngle = -90 - stepAngle + stepAngle * buttonNumber;
322+
final float rOStartAngle = (270 - stepAngle + stepAngle * buttonNumber);
323+
final float rStartAngle = rOStartAngle > 360 ? rOStartAngle % 360 : rOStartAngle;
318324

319-
final float x = (float) Math.cos(Math.toRadians(startAngle)) * mDistance;
320-
final float y = (float) Math.sin(Math.toRadians(startAngle)) * mDistance;
325+
final float x = (float) Math.cos(Math.toRadians(rStartAngle)) * mDistance;
326+
final float y = (float) Math.sin(Math.toRadians(rStartAngle)) * mDistance;
321327

322328
final float pivotX = button.getPivotX();
323329
final float pivotY = button.getPivotY();
@@ -336,8 +342,7 @@ public void onAnimationEnd(Animator animation) {
336342
final float elevation = mMenuButton.getCompatElevation();
337343

338344
mRingView.setVisibility(View.INVISIBLE);
339-
mRingView.setStartAngle(startAngle);
340-
mRingView.setAngle(0);
345+
mRingView.setStartAngle(rStartAngle);
341346

342347
final ColorStateList csl = button.getBackgroundTintList();
343348
if (csl != null) {

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import android.content.Context;
44
import android.graphics.Canvas;
55
import android.graphics.Paint;
6-
import android.graphics.RectF;
6+
import android.graphics.Path;
77
import android.support.annotation.FloatRange;
88
import android.support.annotation.Nullable;
99
import android.util.AttributeSet;
@@ -12,9 +12,10 @@
1212

1313
public class RingEffectView extends View {
1414

15-
private final Paint mPaint;
15+
private static final int STEP_DEGREE = 5;
1616

17-
private RectF mRingRect;
17+
private final Paint mPaint;
18+
private final Path mPath = new Path();
1819

1920
private float mAngle;
2021
private float mStartAngle;
@@ -35,8 +36,12 @@ public RingEffectView(Context context, @Nullable AttributeSet attrs) {
3536
@Override
3637
protected void onDraw(Canvas canvas) {
3738
super.onDraw(canvas);
38-
if (mRingRect != null) {
39-
canvas.drawArc(mRingRect, mStartAngle, mAngle, false, mPaint);
39+
40+
if (!mPath.isEmpty()) {
41+
canvas.save();
42+
canvas.translate(getWidth() / 2, getHeight() / 2);
43+
canvas.drawPath(mPath, mPaint);
44+
canvas.restore();
4045
}
4146
}
4247

@@ -56,7 +61,27 @@ public float getAngle() {
5661
}
5762

5863
public void setAngle(@FloatRange(from = 0.0, to = 360.0) float angle) {
64+
final float diff = angle - mAngle;
65+
final int stepCount = (int) (diff / STEP_DEGREE);
66+
final float stepMod = diff % STEP_DEGREE;
67+
68+
final float sw = mPaint.getStrokeWidth() * 0.5f;
69+
final float radius = mRadius - sw;
70+
71+
for (int i = 1; i <= stepCount; i++ ) {
72+
final float stepAngel = mStartAngle + mAngle + STEP_DEGREE * i;
73+
final float x = (float) Math.cos(Math.toRadians(stepAngel)) * radius;
74+
final float y = (float) Math.sin(Math.toRadians(stepAngel)) * radius;
75+
mPath.lineTo(x, y);
76+
}
77+
78+
final float stepAngel = mStartAngle + mAngle + STEP_DEGREE * stepCount + stepMod;
79+
final float x = (float) Math.cos(Math.toRadians(stepAngel)) * radius;
80+
final float y = (float) Math.sin(Math.toRadians(stepAngel)) * radius;
81+
mPath.lineTo(x, y);
82+
5983
mAngle = angle;
84+
6085
invalidate();
6186
}
6287

@@ -66,6 +91,15 @@ public float getStartAngle() {
6691

6792
public void setStartAngle(@FloatRange(from = 0.0, to = 360.0) float startAngle) {
6893
mStartAngle = startAngle;
94+
mAngle = 0;
95+
96+
final float sw = mPaint.getStrokeWidth() * 0.5f;
97+
final float radius = mRadius - sw;
98+
99+
mPath.reset();
100+
final float x = (float) Math.cos(Math.toRadians(startAngle)) * radius;
101+
final float y = (float) Math.sin(Math.toRadians(startAngle)) * radius;
102+
mPath.moveTo(x, y);
69103
}
70104

71105
public void setStrokeColor(int color) {
@@ -78,15 +112,6 @@ public void setStrokeWidth(int width) {
78112

79113
public void setRadius(int radius) {
80114
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);
90115
}
91116

92117
public int getRadius() {

0 commit comments

Comments
 (0)