3
3
import android .content .Context ;
4
4
import android .graphics .Canvas ;
5
5
import android .graphics .Paint ;
6
- import android .graphics .RectF ;
6
+ import android .graphics .Path ;
7
7
import android .support .annotation .FloatRange ;
8
8
import android .support .annotation .Nullable ;
9
9
import android .util .AttributeSet ;
12
12
13
13
public class RingEffectView extends View {
14
14
15
- private final Paint mPaint ;
15
+ private static final int STEP_DEGREE = 5 ;
16
16
17
- private RectF mRingRect ;
17
+ private final Paint mPaint ;
18
+ private final Path mPath = new Path ();
18
19
19
20
private float mAngle ;
20
21
private float mStartAngle ;
@@ -35,8 +36,12 @@ public RingEffectView(Context context, @Nullable AttributeSet attrs) {
35
36
@ Override
36
37
protected void onDraw (Canvas canvas ) {
37
38
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 ();
40
45
}
41
46
}
42
47
@@ -56,7 +61,27 @@ public float getAngle() {
56
61
}
57
62
58
63
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
+
59
83
mAngle = angle ;
84
+
60
85
invalidate ();
61
86
}
62
87
@@ -66,6 +91,15 @@ public float getStartAngle() {
66
91
67
92
public void setStartAngle (@ FloatRange (from = 0.0 , to = 360.0 ) float startAngle ) {
68
93
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 );
69
103
}
70
104
71
105
public void setStrokeColor (int color ) {
@@ -78,15 +112,6 @@ public void setStrokeWidth(int width) {
78
112
79
113
public void setRadius (int radius ) {
80
114
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
115
}
91
116
92
117
public int getRadius () {
0 commit comments