Skip to content

Commit 69c2732

Browse files
committed
fix(android): border radius correctly working now
1 parent 9ea31cb commit 69c2732

File tree

1 file changed

+49
-64
lines changed
  • packages/image/platforms/android/java/com/nativescript/image

1 file changed

+49
-64
lines changed

packages/image/platforms/android/java/com/nativescript/image/DraweeView.java

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -36,74 +36,56 @@
3636
public class DraweeView extends SimpleDraweeView {
3737
public int imageWidth = 0;
3838
public int imageHeight = 0;
39+
public boolean isUsingOutlineProvider = false;
3940
private static Paint clipPaint;
4041

41-
private boolean clipEnabled = true;
42-
4342
public DraweeView(Context context, GenericDraweeHierarchy hierarchy) {
44-
super(context);
45-
setClipToBounds(clipEnabled);
46-
}
43+
super(context);
44+
}
4745

48-
public DraweeView(Context context) {
49-
super(context);
50-
setClipToBounds(clipEnabled);
51-
}
46+
public DraweeView(Context context) {
47+
super(context);
48+
}
5249

53-
public DraweeView(Context context, AttributeSet attrs) {
54-
super(context, attrs);
55-
setClipToBounds(clipEnabled);
56-
}
50+
public DraweeView(Context context, AttributeSet attrs) {
51+
super(context, attrs);
52+
}
5753

58-
public DraweeView(Context context, AttributeSet attrs, int defStyle) {
59-
super(context, attrs, defStyle);
60-
setClipToBounds(clipEnabled);
61-
}
54+
public DraweeView(Context context, AttributeSet attrs, int defStyle) {
55+
super(context, attrs, defStyle);
56+
}
6257

63-
public void setClipToBounds(boolean value) {
64-
clipEnabled = value;
65-
if (value) {
66-
if (android.os.Build.VERSION.SDK_INT >= 21) {
67-
setOutlineProvider(new ViewOutlineProvider() {
68-
@Override
69-
public void getOutline(View view, Outline outline) {
70-
Drawable drawable = getBackground();
71-
if (drawable instanceof BorderDrawable) {
72-
BorderDrawable borderDrawable = (BorderDrawable) drawable;
73-
float borderTopLeftRadius = borderDrawable.getBorderTopLeftRadius();
74-
float borderTopRightRadius = borderDrawable.getBorderTopRightRadius();
75-
float borderBottomRightRadius = borderDrawable.getBorderBottomRightRadius();
76-
float borderBottomLeftRadius = borderDrawable.getBorderBottomLeftRadius();
77-
float borderLeftWidth = borderDrawable.getBorderLeftWidth();
78-
float borderBottomWidth = borderDrawable.getBorderBottomWidth();
79-
float borderTopWidth = borderDrawable.getBorderTopWidth();
80-
float borderRightWidth = borderDrawable.getBorderRightWidth();
81-
if (android.os.Build.VERSION.SDK_INT >= 21 &&
82-
borderDrawable.getUniformBorderRadius() > 0 &&
83-
borderLeftWidth == 0 &&
84-
borderBottomWidth == 0 &&
85-
borderTopWidth == 0 &&
86-
borderRightWidth == 0) {
87-
drawable.getOutline(outline);
88-
return;
89-
}
58+
// private final Rect outlineRect = new RectF();
59+
public void updateOutlineProvider() {
60+
Drawable drawable = getBackground();
61+
isUsingOutlineProvider = false;
62+
if (android.os.Build.VERSION.SDK_INT >= 21 && drawable instanceof BorderDrawable && (android.os.Build.VERSION.SDK_INT >= 33 || ((BorderDrawable)drawable).hasUniformBorderRadius())) {
63+
setOutlineProvider(new ViewOutlineProvider() {
64+
@Override
65+
public void getOutline(View view, Outline outline) {
66+
Drawable drawable = getBackground();
67+
if (drawable instanceof BorderDrawable) {
68+
BorderDrawable borderDrawable = (BorderDrawable) drawable;
69+
// that if test is only needed until N BorderDrawable is updated to do it
70+
if (borderDrawable.hasUniformBorderRadius()) {
71+
// outlineRect.set(borderDrawable.getBounds());
72+
outline.setRoundRect(borderDrawable.getBounds(), borderDrawable.getBorderBottomLeftRadius());
73+
} else {
74+
drawable.getOutline(outline);
9075
}
91-
outline.setRect(0, 0, view.getWidth(), view.getHeight());
76+
} else {
77+
outline.setRect(100, 100, view.getWidth() - 200, view.getHeight() - 200);
9278
}
93-
});
94-
setClipToOutline(true);
95-
}
79+
}
80+
});
81+
setClipToOutline(true);
82+
isUsingOutlineProvider = true;
9683
} else if (android.os.Build.VERSION.SDK_INT >= 21) {
9784
setOutlineProvider(null);
9885
setClipToOutline(false);
9986
}
10087
}
10188

102-
public boolean getClipToBounds() {
103-
return clipEnabled;
104-
}
105-
106-
10789
@Override
10890
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
10991
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
@@ -141,13 +123,6 @@ private Path generateInnerBorderPath(BorderDrawable borderDrawable) {
141123
float borderBottomWidth = borderDrawable.getBorderBottomWidth();
142124
float borderTopWidth = borderDrawable.getBorderTopWidth();
143125
float borderRightWidth = borderDrawable.getBorderRightWidth();
144-
if (android.os.Build.VERSION.SDK_INT >= 21 && ((borderDrawable.hasUniformBorderRadius()
145-
&& (borderLeftWidth == 0 && borderBottomWidth == 0 && borderTopWidth == 0 && borderRightWidth == 0))
146-
|| (borderTopLeftRadius == 0 && borderTopRightRadius == 0 && borderBottomRightRadius == 0
147-
&& borderBottomLeftRadius == 0))) {
148-
// we can use outline
149-
return null;
150-
}
151126
if (innerBorderPath == null) {
152127
innerBorderPath = new Path();
153128
} else {
@@ -180,7 +155,7 @@ private Path generateInnerBorderPath(BorderDrawable borderDrawable) {
180155
@Override
181156
protected void onDraw(Canvas canvas) {
182157
Drawable drawable = getBackground();
183-
if (clipEnabled && drawable instanceof BorderDrawable) {
158+
if (!isUsingOutlineProvider && drawable instanceof BorderDrawable) {
184159
BorderDrawable borderDrawable = (BorderDrawable) drawable;
185160
Path clipPath = generateInnerBorderPath(borderDrawable);
186161
if (clipPath != null) {
@@ -201,12 +176,11 @@ protected void onDraw(Canvas canvas) {
201176
canvas.restoreToCount(saveCount);
202177
return;
203178
}
204-
}
179+
}
205180
super.onDraw(canvas);
206181
}
207182

208183
public void setUri(android.net.Uri uri, String jsonOptions, com.facebook.drawee.controller.ControllerListener listener) {
209-
Log.d("JS", "setUri " + uri.toString());
210184
ImageRequestBuilder requestBuilder = ImageRequestBuilder.newBuilderWithSource(uri).setRotationOptions( com.facebook.imagepipeline.common.RotationOptions.autoRotate());
211185
JSONObject object = null;
212186
if (jsonOptions.length() > 2) {
@@ -267,7 +241,18 @@ public void setUri(android.net.Uri uri, String jsonOptions, com.facebook.drawee.
267241
builder.setTapToRetryEnabled(true);
268242
}
269243
}
270-
Log.d("JS", "setUri " + uri.toString() + " " + object);
271244
setController(builder.build());
272245
}
246+
247+
@Override
248+
public void setBackground(Drawable background) {
249+
super.setBackground(background);
250+
updateOutlineProvider();
251+
}
252+
253+
@Override
254+
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
255+
super.onSizeChanged(w, h, oldw, oldh);
256+
updateOutlineProvider();
257+
}
273258
}

0 commit comments

Comments
 (0)