8
8
import android .graphics .Path ;
9
9
import android .graphics .Rect ;
10
10
import android .graphics .RectF ;
11
- import android .graphics .Region ;
12
11
import android .util .AttributeSet ;
13
12
import android .view .View ;
14
13
@@ -23,7 +22,7 @@ public class FilledView extends View {
23
22
public enum StartMode {
24
23
LEFT (0 ), TOP (1 ), RIGHT (2 ), BOTTOM (3 );
25
24
26
- private int mode ;
25
+ private final int mode ;
27
26
28
27
StartMode (int mode ) {
29
28
this .mode = mode ;
@@ -41,19 +40,19 @@ public int getMode() {
41
40
private int textSize ;
42
41
private boolean isShowBorder ;
43
42
private int borderSize = 1 ;
44
-
45
- private final Path textPath = new Path ();
46
- private final Path croppedProgressPath = new Path ();
47
- private final Path croppedTextPath = new Path ();
48
- private final Paint paint = new Paint (Paint .ANTI_ALIAS_FLAG );
49
43
private int width ;
50
44
private int height ;
51
45
private float percent = 0.1F ;
52
- private Path progressStrokePath = new Path ();
53
- private Rect textBounds = new Rect ();
54
46
55
- private final Region region = new Region ();
56
- private final Region textRegion = new Region ();
47
+ private final RectF rectF = new RectF ();
48
+ private final Path textPath = new Path ();
49
+ private final Path croppedTextPath = new Path ();
50
+ private final Path progressPath = new Path ();
51
+ private final Path croppedProgressPath = new Path ();
52
+ private final Path progressStrokePath = new Path ();
53
+
54
+ private final Paint paint = new Paint (Paint .ANTI_ALIAS_FLAG );
55
+ private final Rect textBounds = new Rect ();
57
56
58
57
public FilledView (Context context ) {
59
58
this (context , null );
@@ -133,52 +132,37 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
133
132
if (text != null ) {
134
133
paint .getTextPath (text , 0 , text .length (), cx , cy , textPath );
135
134
}
136
- progressStrokePath = getRoundRectPath (0 , 0 , width , height , radius );
137
-
138
- computePaths ();
135
+ initRoundRectPath (0 , 0 , width , height , radius );
136
+ createPaths ();
139
137
setMeasuredDimension (width , height );
140
138
}
141
139
142
- private Path getRoundRectPath (float left , float top , float right , float bottom , float radius ) {
143
- region .set ((int ) left , (int ) top , (int ) right , (int ) bottom );
144
- Path roundRectPath = new Path ();
145
- RectF rectF = new RectF ();
146
- rectF .set (left + borderSize , top + borderSize , right - borderSize , bottom - borderSize );
147
- roundRectPath .addRoundRect (rectF , radius , radius , Path .Direction .CCW );
148
- region .setPath (roundRectPath , region );
149
- return region .getBoundaryPath ();
150
- }
151
-
152
- public void computeCroppedProgressPath () {
140
+ private void createPaths () {
153
141
if (startPosition == StartMode .RIGHT .getMode ()) {
154
- region . set (( int ) ( width * (1F - percent ) ), 0 , width , height );
142
+ setRectPath ( progressPath , width * (1F - percent ), 0 , width , height );
155
143
} else if (startPosition == StartMode .LEFT .getMode ()) {
156
- region . set ( 0 , 0 , ( int ) ( width * percent ) , height );
144
+ setRectPath ( progressPath , 0 , 0 , width * percent , height );
157
145
} else if (startPosition == StartMode .TOP .getMode ()) {
158
- region . set ( 0 , 0 , width , ( int ) ( height * percent ) );
146
+ setRectPath ( progressPath , 0 , 0 , width , height * percent );
159
147
} else if (startPosition == StartMode .BOTTOM .getMode ()) {
160
- region . set ( 0 , ( int ) ( height * (1F - percent ) ), width , height );
148
+ setRectPath ( progressPath , 0 , height * (1F - percent ), width , height );
161
149
}
162
- region .setPath (progressStrokePath , region );
163
- textRegion .setPath (textPath , region );
164
- region .op (textRegion , Region .Op .DIFFERENCE );
165
- croppedProgressPath .rewind ();
166
- region .getBoundaryPath (croppedProgressPath );
150
+ croppedProgressPath .op (progressPath , textPath , Path .Op .DIFFERENCE );
151
+ croppedProgressPath .op (progressStrokePath , Path .Op .INTERSECT );
152
+
153
+ croppedTextPath .op (textPath , progressPath , Path .Op .DIFFERENCE );
167
154
}
168
155
169
- public void computeCroppedTextPath () {
170
- if (startPosition == StartMode .RIGHT .getMode ()) {
171
- region .set (0 , 0 , (int ) (width * (1F - percent )), height );
172
- } else if (startPosition == StartMode .LEFT .getMode ()) {
173
- region .set ((int ) (width * percent ), 0 , width , height );
174
- } else if (startPosition == StartMode .TOP .getMode ()) {
175
- region .set (0 , (int ) (height * percent ), width , height );
176
- } else if (startPosition == StartMode .BOTTOM .getMode ()) {
177
- region .set (0 , 0 , width , (int ) (height * (1F - percent )));
178
- }
179
- textRegion .setPath (textPath , region );
180
- croppedTextPath .rewind ();
181
- textRegion .getBoundaryPath (croppedTextPath );
156
+ private void initRoundRectPath (float left , float top , float right , float bottom , float radius ) {
157
+ progressStrokePath .reset ();
158
+ rectF .set (left + borderSize , top + borderSize , right - borderSize , bottom - borderSize );
159
+ progressStrokePath .addRoundRect (rectF , radius , radius , Path .Direction .CW );
160
+ }
161
+
162
+ public void setRectPath (Path path , float left , float top , float right , float bottom ) {
163
+ rectF .set (left , top , right , bottom );
164
+ path .rewind ();
165
+ path .addRect (rectF , Path .Direction .CW );
182
166
}
183
167
184
168
@ Override
@@ -200,7 +184,7 @@ protected void onDraw(Canvas canvas) {
200
184
public void setProgress (float percent ) {
201
185
if (percent >= 0f && percent <= 1F ) {
202
186
this .percent = percent ;
203
- computePaths ();
187
+ createPaths ();
204
188
invalidate ();
205
189
}
206
190
}
@@ -215,8 +199,8 @@ public void setText(String text) {
215
199
requestLayout ();
216
200
}
217
201
218
- public void showBorder (boolean flag ) {
219
- isShowBorder = flag ;
202
+ public void showBorder (boolean isShowBorder ) {
203
+ this . isShowBorder = isShowBorder ;
220
204
invalidate ();
221
205
}
222
206
@@ -230,13 +214,13 @@ public void setRadius(int radius) {
230
214
requestLayout ();
231
215
}
232
216
233
- public void setStartMode ( StartMode startPosition ) {
234
- this .startPosition = startPosition . getMode () ;
217
+ public void setBorderSize ( int borderSize ) {
218
+ this .borderSize = borderSize ;
235
219
requestLayout ();
236
220
}
237
221
238
- private void computePaths ( ) {
239
- computeCroppedProgressPath ();
240
- computeCroppedTextPath ();
222
+ public void setStartMode ( StartMode startPosition ) {
223
+ this . startPosition = startPosition . getMode ();
224
+ requestLayout ();
241
225
}
242
226
}
0 commit comments