Skip to content

Commit 2799892

Browse files
pekingmepaulfthomas
authored andcommitted
[Shape] Added state list support to shape appearance and corner size.
PiperOrigin-RevId: 650718631
1 parent d9a3c6b commit 2799892

13 files changed

+866
-227
lines changed

lib/java/com/google/android/material/math/MathUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.google.android.material.math;
1717

18+
import androidx.annotation.NonNull;
19+
1820
/** A class that contains utility methods related to numbers. */
1921
public final class MathUtils {
2022

@@ -99,4 +101,17 @@ public static int floorMod(int x, int y) {
99101
}
100102
return x - r * y;
101103
}
104+
105+
public static boolean areAllElementsEqual(@NonNull float[] array) {
106+
if (array.length <= 1) {
107+
return true;
108+
}
109+
float sample = array[0];
110+
for (int i = 1; i < array.length; i++) {
111+
if (array[i] != sample) {
112+
return false;
113+
}
114+
}
115+
return true;
116+
}
102117
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ public int hashCode() {
5858
Object[] hashedFields = {size};
5959
return Arrays.hashCode(hashedFields);
6060
}
61+
62+
@Override
63+
public String toString() {
64+
return getCornerSize() + "px";
65+
}
6166
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.android.material.shape;
1818

1919
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20+
import static androidx.core.math.MathUtils.clamp;
2021
import static java.lang.Math.min;
2122

2223
import android.graphics.RectF;
@@ -57,7 +58,7 @@ public ClampedCornerSize(float target) {
5758

5859
@Override
5960
public float getCornerSize(@NonNull RectF bounds) {
60-
return min(target, getMaxCornerSize(bounds));
61+
return clamp(target, 0, getMaxCornerSize(bounds));
6162
}
6263

6364
@Override

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

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,6 @@ public void getCornerPath(
7070
getCornerPath(angle, interpolation, shapePath);
7171
}
7272

73-
/**
74-
* Generates a {@link ShapePath} using start and end radius values for this corner treatment.
75-
*
76-
* <p>CornerTreatments are assumed to have an origin of (0, 0) (i.e. they represent the top-left
77-
* corner), and are automatically rotated and scaled as necessary when applied to other corners.
78-
*
79-
* @param shapePath the {@link ShapePath} that this treatment should write to.
80-
* @param angle the angle of the corner, typically 90 degrees.
81-
* @param interpolation the interpolation of the corner treatment. Ranges between 0 (none) and 1
82-
* (fully) interpolated. Custom corner treatments can implement interpolation to support shape
83-
* transition between two arbitrary states. Typically, a value of 0 indicates that the custom
84-
* corner treatment is not rendered (i.e. that it is a 90 degree angle), and a value of 1
85-
* indicates that the treatment is fully rendered. Animation between these two values can
86-
* "heal" or "reveal" a corner treatment.
87-
* @param startRadius the starting radius or size of this corner before interpolation.
88-
* @param endRadius the ending radius or size of this corner after interpolation.
89-
*/
90-
public void getCornerPath(
91-
@NonNull ShapePath shapePath,
92-
float angle,
93-
float interpolation,
94-
float startRadius,
95-
float endRadius) {
96-
getCornerPath(shapePath, angle, interpolation, endRadius);
97-
}
98-
9973
/**
10074
* Generates a {@link ShapePath} for this corner treatment.
10175
*
@@ -123,40 +97,4 @@ public void getCornerPath(
12397
@NonNull CornerSize size) {
12498
getCornerPath(shapePath, angle, interpolation, size.getCornerSize(bounds));
12599
}
126-
127-
/**
128-
* Generates a {@link ShapePath} using start and end {@link CornerSize} values for this corner
129-
* treatment.
130-
*
131-
* <p>CornerTreatments are assumed to have an origin of (0, 0) (i.e. they represent the top-left
132-
* corner), and are automatically rotated and scaled as necessary when applied to other corners.
133-
*
134-
* @param shapePath the {@link ShapePath} that this treatment should write to.
135-
* @param angle the angle of the corner, typically 90 degrees.
136-
* @param interpolation the interpolation of the corner treatment. Ranges between 0 (none) and 1
137-
* (fully) interpolated. Custom corner treatments can implement interpolation to support shape
138-
* transition between two arbitrary states. Typically, a value of 0 indicates that the custom
139-
* corner treatment is not rendered (i.e. that it is a 90 degree angle), and a value of 1
140-
* indicates that the treatment is fully rendered. Animation between these two values can
141-
* "heal" or "reveal" a corner treatment.
142-
* @param bounds the bounds of the full shape that will be drawn. This could be used change the
143-
* behavior of the CornerTreatment depending on how much space is available for the full
144-
* shape.
145-
* @param startSize the starting {@link CornerSize} used for this corner before interpolation
146-
* @param endSize the ending {@link CornerSize} used for this corner after interpolation
147-
*/
148-
public void getCornerPath(
149-
@NonNull ShapePath shapePath,
150-
float angle,
151-
float interpolation,
152-
@NonNull RectF bounds,
153-
@NonNull CornerSize startSize,
154-
@NonNull CornerSize endSize) {
155-
getCornerPath(
156-
shapePath,
157-
angle,
158-
interpolation,
159-
startSize.getCornerSize(bounds),
160-
endSize.getCornerSize(bounds));
161-
}
162100
}

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.android.material.shape;
1818

19-
import static com.google.android.material.math.MathUtils.lerp;
2019

2120
import androidx.annotation.NonNull;
2221

@@ -46,17 +45,7 @@ public CutCornerTreatment(float size) {
4645
@Override
4746
public void getCornerPath(
4847
@NonNull ShapePath shapePath, float angle, float interpolation, float radius) {
49-
getCornerPath(shapePath, angle, interpolation, 0, radius);
50-
}
51-
52-
@Override
53-
public void getCornerPath(
54-
@NonNull ShapePath shapePath,
55-
float angle,
56-
float interpolation,
57-
float startRadius,
58-
float endRadius) {
59-
float radius = lerp(startRadius, endRadius, interpolation);
48+
radius *= interpolation;
6049
shapePath.reset(0, radius, ShapePath.ANGLE_LEFT, 180 - angle);
6150
shapePath.lineTo(
6251
(float) (Math.sin(Math.toRadians(angle)) * radius),

0 commit comments

Comments
 (0)