12
12
import android .view .View ;
13
13
14
14
import androidx .annotation .Nullable ;
15
+ import androidx .annotation .StringDef ;
15
16
16
17
import com .facebook .react .bridge .Arguments ;
17
18
import com .facebook .react .bridge .ReactContext ;
18
19
import com .facebook .react .bridge .WritableArray ;
19
20
import com .facebook .react .bridge .WritableMap ;
20
21
import com .facebook .react .uimanager .PixelUtil ;
21
22
23
+ import java .lang .annotation .Retention ;
24
+ import java .lang .annotation .RetentionPolicy ;
22
25
import java .util .ArrayList ;
23
26
import java .util .HashMap ;
24
27
import java .util .Locale ;
@@ -29,13 +32,13 @@ public class RPath extends View {
29
32
protected String mPathId ;
30
33
private RectF mHitSlop ;
31
34
private boolean mOverriddenHitSlop = false ;
35
+ private @ RPath .ResizeMode String mResizeMode = RPath .ResizeMode .NONE ;
32
36
33
37
private Paint mPaint ;
34
38
protected Path mPath ;
35
39
36
40
protected ArrayList <PointF > mTempPoints ;
37
41
38
-
39
42
public RPath (ReactContext context ) {
40
43
super (context );
41
44
mPath = new Path ();
@@ -129,12 +132,14 @@ void setHitSlop(RectF hitSlop, boolean override) {
129
132
}
130
133
}
131
134
135
+ public void setResizeMode (@ ResizeMode String resizeMode ) {
136
+ mResizeMode = resizeMode ;
137
+ }
138
+
132
139
private static boolean isTranslucent (int strokeColor ) {
133
140
return ((strokeColor >> 24 ) & 0xff ) != 255 && strokeColor != Color .TRANSPARENT ;
134
141
}
135
142
136
-
137
-
138
143
public void addPoint (PointF p ) {
139
144
RPathState currentState = mPathStateStack .peek ();
140
145
ArrayList <PointF > points = currentState .points ;
@@ -209,6 +214,47 @@ public WritableMap toWritableMap(Boolean includePoints){
209
214
return path ;
210
215
}
211
216
217
+ @ Retention (RetentionPolicy .SOURCE )
218
+ @ StringDef ({
219
+ ResizeMode .COVER ,
220
+ ResizeMode .STRETCH ,
221
+ ResizeMode .NONE
222
+ })
223
+ @interface ResizeMode {
224
+ String COVER = "cover" ;
225
+ String STRETCH = "stretch" ;
226
+ String NONE = "none" ;
227
+ }
228
+
229
+ @ Override
230
+ protected void onSizeChanged (int w , int h , int oldw , int oldh ) {
231
+ super .onSizeChanged (w , h , oldw , oldh );
232
+ PointF scaler ;
233
+ float sx = w * 1.f / oldw * 1.f ;
234
+ float sy = h * 1.f / oldh * 1.f ;
235
+ float scale = sx ;
236
+ PointF next ;
237
+ switch (mResizeMode ) {
238
+ case ResizeMode .COVER :
239
+ next = new PointF (Math .max (oldw * scale , w ), Math .max (oldh * scale , h ));
240
+ scale = Math .min (next .x / oldw , next .y / oldh );
241
+ scaler = new PointF (scale , scale );
242
+ break ;
243
+ case ResizeMode .STRETCH :
244
+ scaler = new PointF (sx , sy );
245
+ break ;
246
+ default :
247
+ case ResizeMode .NONE :
248
+ return ;
249
+ }
250
+ ArrayList <PointF > points = new ArrayList <>(mPathStateStack .peek ().points );
251
+ for (PointF point : points ) {
252
+ point .x *= scaler .x ;
253
+ point .y *= scaler .y ;
254
+ }
255
+ setPoints (points );
256
+ }
257
+
212
258
@ Override
213
259
public String toString () {
214
260
HashMap <String , Object > props = new HashMap <>();
@@ -220,4 +266,5 @@ public String toString() {
220
266
props .put ("points" , currentState .points );
221
267
return String .format (Locale .ENGLISH , "RPath(%s)" , props );
222
268
}
269
+
223
270
}
0 commit comments