Skip to content

Commit 52eb143

Browse files
committed
Optimize stroke inset bounds method.
Use RectF#inset() instead of manually insetting the stroke by half of the stroke width. PiperOrigin-RevId: 218887035
1 parent 560c982 commit 52eb143

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

lib/java/com/google/android/material/shape/MaterialShapeDrawable.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public class MaterialShapeDrawable extends Drawable implements TintAwareDrawable
107107
private final Path pathInsetByStroke = new Path();
108108
private final PointF pointF = new PointF();
109109
private final RectF rectF = new RectF();
110-
private final RectF insetRectF = new RectF();
111110
private final ShapePath shapePath = new ShapePath();
112111
private final Region transparentRegion = new Region();
113112
private final Region scratchRegion = new Region();
@@ -755,9 +754,7 @@ private void drawFillShape(Canvas canvas) {
755754
drawShape(canvas, fillPaint);
756755
}
757756

758-
/**
759-
* Draw the path or try to draw a round rect if possible.
760-
*/
757+
/** Draw the path or try to draw a round rect if possible. */
761758
private void drawShape(Canvas canvas, Paint paint) {
762759
if (shapeAppearanceModel.isRoundRect()) {
763760
float cornerSize = shapeAppearanceModel.getTopRightCorner().getCornerSize();
@@ -776,7 +773,7 @@ private void prepareCanvasForShadow(Canvas canvas) {
776773
// be clipped and not visible.
777774
Rect canvasClipBounds = canvas.getClipBounds();
778775
canvasClipBounds.inset(-shadowCompatRadius, -shadowCompatRadius);
779-
//TODO: double check that offset doesn't work for sure
776+
// TODO: double check that offset doesn't work for sure
780777
canvasClipBounds.inset(-Math.abs(shadowOffsetX), -Math.abs(shadowOffsetY));
781778
canvas.clipRect(canvasClipBounds, Region.Op.REPLACE);
782779

@@ -805,10 +802,8 @@ private void drawCompatShadow(Canvas canvas) {
805802
edgeTransforms[index], shadowRenderer, shadowCompatRadius, canvas);
806803
}
807804

808-
int shadowOffsetX =
809-
(int) (shadowCompatOffset * Math.sin(Math.toRadians(shadowCompatRotation)));
810-
int shadowOffsetY =
811-
(int) (shadowCompatOffset * Math.cos(Math.toRadians(shadowCompatRotation)));
805+
int shadowOffsetX = (int) (shadowCompatOffset * Math.sin(Math.toRadians(shadowCompatRotation)));
806+
int shadowOffsetY = (int) (shadowCompatOffset * Math.cos(Math.toRadians(shadowCompatRotation)));
812807

813808
canvas.translate(-shadowOffsetX, -shadowOffsetY);
814809
canvas.drawPath(pathInsetByStroke, clearPaint);
@@ -1049,21 +1044,20 @@ private boolean updateColorsForState(int[] state, boolean invalidateSelf) {
10491044
return invalidateSelf;
10501045
}
10511046

1052-
private float getStrokeInsetLength() {
1047+
private RectF getBoundsInsetByStroke() {
1048+
RectF bounds = getBoundsAsRectF();
1049+
float strokeInsetWidth = getStrokeInsetWidth();
1050+
bounds.inset(strokeInsetWidth, strokeInsetWidth);
1051+
return bounds;
1052+
}
1053+
1054+
private float getStrokeInsetWidth() {
10531055
if (hasStroke()) {
10541056
return strokePaint.getStrokeWidth() / 2.0f;
10551057
}
10561058
return 0f;
10571059
}
10581060

1059-
private RectF getBoundsInsetByStroke() {
1060-
RectF rectF = getBoundsAsRectF();
1061-
float inset = getStrokeInsetLength();
1062-
insetRectF.set(
1063-
rectF.left + inset, rectF.top + inset, rectF.right - inset, rectF.bottom - inset);
1064-
return insetRectF;
1065-
}
1066-
10671061
/**
10681062
* Dummy implementation of constant state. This drawable doesn't have shared state. Implementing
10691063
* so that calls to getConstantState().newDrawable() don't crash on L and M.

0 commit comments

Comments
 (0)