Skip to content

Commit

Permalink
[Shape] Added state list support to shape appearance and corner size.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 653356021
  • Loading branch information
pekingme authored and paulfthomas committed Jul 22, 2024
1 parent c13fff7 commit 8e63e7f
Show file tree
Hide file tree
Showing 13 changed files with 906 additions and 236 deletions.
20 changes: 20 additions & 0 deletions lib/java/com/google/android/material/math/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.google.android.material.math;

import androidx.annotation.NonNull;

/** A class that contains utility methods related to numbers. */
public final class MathUtils {

Expand Down Expand Up @@ -99,4 +101,22 @@ public static int floorMod(int x, int y) {
}
return x - r * y;
}

/**
* Returns whether the array contains all same elements.
*
* @param array Input array of floats.
*/
public static boolean areAllElementsEqual(@NonNull float[] array) {
if (array.length <= 1) {
return true;
}
float sample = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] != sample) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public int hashCode() {
Object[] hashedFields = {size};
return Arrays.hashCode(hashedFields);
}

@Override
public String toString() {
return getCornerSize() + "px";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.material.shape;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import static androidx.core.math.MathUtils.clamp;
import static java.lang.Math.min;

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

@Override
public float getCornerSize(@NonNull RectF bounds) {
return min(target, getMaxCornerSize(bounds));
return clamp(target, 0, getMaxCornerSize(bounds));
}

@Override
Expand Down
62 changes: 0 additions & 62 deletions lib/java/com/google/android/material/shape/CornerTreatment.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,6 @@ public void getCornerPath(
getCornerPath(angle, interpolation, shapePath);
}

/**
* Generates a {@link ShapePath} using start and end radius values for this corner treatment.
*
* <p>CornerTreatments are assumed to have an origin of (0, 0) (i.e. they represent the top-left
* corner), and are automatically rotated and scaled as necessary when applied to other corners.
*
* @param shapePath the {@link ShapePath} that this treatment should write to.
* @param angle the angle of the corner, typically 90 degrees.
* @param interpolation the interpolation of the corner treatment. Ranges between 0 (none) and 1
* (fully) interpolated. Custom corner treatments can implement interpolation to support shape
* transition between two arbitrary states. Typically, a value of 0 indicates that the custom
* corner treatment is not rendered (i.e. that it is a 90 degree angle), and a value of 1
* indicates that the treatment is fully rendered. Animation between these two values can
* "heal" or "reveal" a corner treatment.
* @param startRadius the starting radius or size of this corner before interpolation.
* @param endRadius the ending radius or size of this corner after interpolation.
*/
public void getCornerPath(
@NonNull ShapePath shapePath,
float angle,
float interpolation,
float startRadius,
float endRadius) {
getCornerPath(shapePath, angle, interpolation, endRadius);
}

/**
* Generates a {@link ShapePath} for this corner treatment.
*
Expand Down Expand Up @@ -123,40 +97,4 @@ public void getCornerPath(
@NonNull CornerSize size) {
getCornerPath(shapePath, angle, interpolation, size.getCornerSize(bounds));
}

/**
* Generates a {@link ShapePath} using start and end {@link CornerSize} values for this corner
* treatment.
*
* <p>CornerTreatments are assumed to have an origin of (0, 0) (i.e. they represent the top-left
* corner), and are automatically rotated and scaled as necessary when applied to other corners.
*
* @param shapePath the {@link ShapePath} that this treatment should write to.
* @param angle the angle of the corner, typically 90 degrees.
* @param interpolation the interpolation of the corner treatment. Ranges between 0 (none) and 1
* (fully) interpolated. Custom corner treatments can implement interpolation to support shape
* transition between two arbitrary states. Typically, a value of 0 indicates that the custom
* corner treatment is not rendered (i.e. that it is a 90 degree angle), and a value of 1
* indicates that the treatment is fully rendered. Animation between these two values can
* "heal" or "reveal" a corner treatment.
* @param bounds the bounds of the full shape that will be drawn. This could be used change the
* behavior of the CornerTreatment depending on how much space is available for the full
* shape.
* @param startSize the starting {@link CornerSize} used for this corner before interpolation
* @param endSize the ending {@link CornerSize} used for this corner after interpolation
*/
public void getCornerPath(
@NonNull ShapePath shapePath,
float angle,
float interpolation,
@NonNull RectF bounds,
@NonNull CornerSize startSize,
@NonNull CornerSize endSize) {
getCornerPath(
shapePath,
angle,
interpolation,
startSize.getCornerSize(bounds),
endSize.getCornerSize(bounds));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.android.material.shape;

import static com.google.android.material.math.MathUtils.lerp;

import androidx.annotation.NonNull;

Expand Down Expand Up @@ -46,17 +45,7 @@ public CutCornerTreatment(float size) {
@Override
public void getCornerPath(
@NonNull ShapePath shapePath, float angle, float interpolation, float radius) {
getCornerPath(shapePath, angle, interpolation, 0, radius);
}

@Override
public void getCornerPath(
@NonNull ShapePath shapePath,
float angle,
float interpolation,
float startRadius,
float endRadius) {
float radius = lerp(startRadius, endRadius, interpolation);
radius *= interpolation;
shapePath.reset(0, radius, ShapePath.ANGLE_LEFT, 180 - angle);
shapePath.lineTo(
(float) (Math.sin(Math.toRadians(angle)) * radius),
Expand Down
Loading

0 comments on commit 8e63e7f

Please sign in to comment.