Skip to content

Commit aee834c

Browse files
author
vakrilov
committed
Exposed properties to allow image to be cropped when there is border radius
1 parent 62efc37 commit aee834c

1 file changed

Lines changed: 133 additions & 90 deletions

File tree

‎src/org/nativescript/widgets/ImageView.java‎

Lines changed: 133 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,122 +4,165 @@
44
package org.nativescript.widgets;
55

66
import android.content.Context;
7+
import android.graphics.*;
78
import android.graphics.drawable.Drawable;
89

910
/**
1011
* @author hhristov
11-
*
12+
*
1213
*/
1314
public class ImageView extends android.widget.ImageView {
15+
private float cornerRadius = 0;
16+
private float borderWidth = 0;
17+
18+
private Path path = new Path();
19+
private RectF rect = new RectF();
20+
21+
private double scaleW = 1;
22+
private double scaleH = 1;
23+
24+
public ImageView(Context context) {
25+
super(context);
26+
this.setScaleType(ScaleType.FIT_CENTER);
27+
}
28+
29+
public float getCornerRadius() {
30+
return this.cornerRadius;
31+
}
32+
33+
public void setCornerRadius(float radius) {
34+
if (radius != this.cornerRadius) {
35+
this.cornerRadius = radius;
36+
this.invalidate();
37+
}
38+
}
1439

15-
private double scaleW = 1;
16-
private double scaleH = 1;
17-
18-
public ImageView(Context context) {
19-
super(context);
20-
this.setScaleType(ScaleType.FIT_CENTER);
21-
}
22-
23-
@Override
24-
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
25-
26-
int width = MeasureSpec.getSize(widthMeasureSpec);
27-
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
28-
29-
int height = MeasureSpec.getSize(heightMeasureSpec);
30-
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
40+
public float getBorderWidth() {
41+
return this.borderWidth;
42+
}
43+
44+
public void setBorderWidth(float radius) {
45+
if (radius != this.borderWidth) {
46+
this.borderWidth = radius;
47+
this.invalidate();
48+
}
49+
}
50+
51+
@Override
52+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
53+
54+
int width = MeasureSpec.getSize(widthMeasureSpec);
55+
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
56+
57+
int height = MeasureSpec.getSize(heightMeasureSpec);
58+
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
3159

3260
Drawable drawable = this.getDrawable();
3361
int measureWidth;
3462
int measureHeight;
3563
if (drawable != null) {
36-
measureWidth = drawable.getIntrinsicWidth();
37-
measureHeight = drawable.getIntrinsicHeight();
64+
measureWidth = drawable.getIntrinsicWidth();
65+
measureHeight = drawable.getIntrinsicHeight();
66+
} else {
67+
measureWidth = 0;
68+
measureHeight = 0;
3869
}
39-
else {
40-
measureWidth = 0;
41-
measureHeight = 0;
70+
71+
boolean finiteWidth = widthMode != MeasureSpec.UNSPECIFIED;
72+
boolean finiteHeight = heightMode != MeasureSpec.UNSPECIFIED;
73+
74+
if (measureWidth != 0 && measureHeight != 0 && (finiteWidth || finiteHeight)) {
75+
this.computeScaleFactor(width, height, finiteWidth, finiteHeight, measureWidth, measureHeight);
76+
int resultW = (int) Math.floor(measureWidth * this.scaleW);
77+
int resultH = (int) Math.floor(measureHeight * this.scaleH);
78+
79+
measureWidth = finiteWidth ? Math.min(resultW, width) : resultW;
80+
measureHeight = finiteHeight ? Math.min(resultH, height) : resultH;
4281
}
43-
44-
boolean finiteWidth = widthMode != MeasureSpec.UNSPECIFIED;
45-
boolean finiteHeight = heightMode != MeasureSpec.UNSPECIFIED;
46-
47-
if (measureWidth != 0 && measureHeight != 0 && (finiteWidth || finiteHeight)) {
48-
this.computeScaleFactor(width, height, finiteWidth, finiteHeight, measureWidth, measureHeight);
49-
int resultW = (int) Math.floor(measureWidth * this.scaleW);
50-
int resultH = (int) Math.floor(measureHeight * this.scaleH);
51-
52-
measureWidth = finiteWidth ? Math.min(resultW, width) : resultW;
53-
measureHeight = finiteHeight ? Math.min(resultH, height) : resultH;
54-
}
55-
56-
measureWidth += this.getPaddingLeft() + this.getPaddingRight();
57-
measureHeight += this.getPaddingTop() + this.getPaddingBottom();
58-
59-
measureWidth = Math.max(measureWidth, getSuggestedMinimumWidth());
60-
measureHeight = Math.max(measureHeight, getSuggestedMinimumHeight());
61-
62-
if (CommonLayoutParams.debuggable > 0) {
63-
StringBuilder sb = CommonLayoutParams.getStringBuilder();
64-
sb.append("ImageView onMeasure: ");
65-
sb.append(MeasureSpec.toString(widthMeasureSpec));
66-
sb.append(", ");
67-
sb.append(MeasureSpec.toString(heightMeasureSpec));
68-
sb.append(", stretch: ");
69-
sb.append(this.getScaleType());
70-
sb.append(", measureWidth: ");
71-
sb.append(measureWidth);
72-
sb.append(", measureHeight: ");
73-
sb.append(measureHeight);
74-
75-
CommonLayoutParams.log(CommonLayoutParams.tag, sb.toString());
76-
}
77-
82+
83+
measureWidth += this.getPaddingLeft() + this.getPaddingRight();
84+
measureHeight += this.getPaddingTop() + this.getPaddingBottom();
85+
86+
measureWidth = Math.max(measureWidth, getSuggestedMinimumWidth());
87+
measureHeight = Math.max(measureHeight, getSuggestedMinimumHeight());
88+
89+
if (CommonLayoutParams.debuggable > 0) {
90+
StringBuilder sb = CommonLayoutParams.getStringBuilder();
91+
sb.append("ImageView onMeasure: ");
92+
sb.append(MeasureSpec.toString(widthMeasureSpec));
93+
sb.append(", ");
94+
sb.append(MeasureSpec.toString(heightMeasureSpec));
95+
sb.append(", stretch: ");
96+
sb.append(this.getScaleType());
97+
sb.append(", measureWidth: ");
98+
sb.append(measureWidth);
99+
sb.append(", measureHeight: ");
100+
sb.append(measureHeight);
101+
102+
CommonLayoutParams.log(CommonLayoutParams.tag, sb.toString());
103+
}
104+
78105
int widthSizeAndState = resolveSizeAndState(measureWidth, widthMeasureSpec, 0);
79106
int heightSizeAndState = resolveSizeAndState(measureHeight, heightMeasureSpec, 0);
80107

81108
this.setMeasuredDimension(widthSizeAndState, heightSizeAndState);
82-
}
83-
84-
private void computeScaleFactor(
85-
int measureWidth,
86-
int measureHeight,
87-
boolean widthIsFinite,
88-
boolean heightIsFinite,
89-
double nativeWidth,
90-
double nativeHeight) {
91-
92-
this.scaleW = 1;
93-
this.scaleH = 1;
94-
95-
ScaleType scale = this.getScaleType();
109+
}
110+
111+
private void computeScaleFactor(int measureWidth, int measureHeight, boolean widthIsFinite, boolean heightIsFinite, double nativeWidth, double nativeHeight) {
112+
113+
this.scaleW = 1;
114+
this.scaleH = 1;
115+
116+
ScaleType scale = this.getScaleType();
96117
if ((scale == ScaleType.CENTER_CROP || scale == ScaleType.FIT_CENTER || scale == ScaleType.FIT_XY) &&
97-
(widthIsFinite || heightIsFinite)) {
118+
(widthIsFinite || heightIsFinite)) {
98119

99-
this.scaleW = (nativeWidth > 0) ? measureWidth / nativeWidth : 0d;
100-
this.scaleH = (nativeHeight > 0) ? measureHeight / nativeHeight : 0d;
120+
this.scaleW = (nativeWidth > 0) ? measureWidth / nativeWidth : 0d;
121+
this.scaleH = (nativeHeight > 0) ? measureHeight / nativeHeight : 0d;
101122

102123
if (!widthIsFinite) {
103-
this.scaleW = scaleH;
104-
}
105-
else if (!heightIsFinite) {
106-
this.scaleH = scaleW;
107-
}
108-
else {
124+
this.scaleW = scaleH;
125+
} else if (!heightIsFinite) {
126+
this.scaleH = scaleW;
127+
} else {
109128
// No infinite dimensions.
110129
switch (scale) {
111-
case FIT_CENTER:
112-
this.scaleH = this.scaleW < this.scaleH ? this.scaleW : this.scaleH;
113-
this.scaleW = this.scaleH;
114-
break;
115-
case CENTER_CROP:
116-
this.scaleH = this.scaleW > this.scaleH ? this.scaleW : this.scaleH;
117-
this.scaleW = this.scaleH;
118-
break;
119-
default:
120-
break;
130+
case FIT_CENTER:
131+
this.scaleH = this.scaleW < this.scaleH ? this.scaleW : this.scaleH;
132+
this.scaleW = this.scaleH;
133+
break;
134+
case CENTER_CROP:
135+
this.scaleH = this.scaleW > this.scaleH ? this.scaleW : this.scaleH;
136+
this.scaleW = this.scaleH;
137+
break;
138+
default:
139+
break;
121140
}
122141
}
123142
}
124143
}
144+
145+
@Override
146+
protected void onDraw(Canvas canvas) {
147+
// floor the border width to avoid gaps between the border and the image
148+
float roundedBorderWidth = (float) Math.floor(this.borderWidth);
149+
float innerRadius = Math.max(0, this.cornerRadius - roundedBorderWidth);
150+
151+
// The border width is included in the padding so there is no need for
152+
// clip if there is no inner border radius.
153+
if (innerRadius != 0) {
154+
this.rect.set(
155+
roundedBorderWidth,
156+
roundedBorderWidth,
157+
this.getWidth() - roundedBorderWidth,
158+
this.getHeight() - roundedBorderWidth);
159+
160+
this.path.reset();
161+
this.path.addRoundRect(rect, innerRadius, innerRadius, android.graphics.Path.Direction.CW);
162+
163+
canvas.clipPath(this.path);
164+
}
165+
166+
super.onDraw(canvas);
167+
}
125168
}

0 commit comments

Comments
 (0)