Skip to content

Commit 158d9f0

Browse files
pubiqqimhappi
authored andcommitted
[ShapeAppearance] Change relative corner size behavior for "vertical" shapes
Resolves #2929 GIT_ORIGIN_REV_ID=38438252c1d43d1ccbeb333bdfc7a76b469033f4 PiperOrigin-RevId: 471564817
1 parent 14415a1 commit 158d9f0

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,44 @@
1616

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

19+
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20+
import static java.lang.Math.min;
21+
1922
import android.graphics.RectF;
2023
import androidx.annotation.FloatRange;
2124
import androidx.annotation.NonNull;
25+
import androidx.annotation.RestrictTo;
2226
import java.util.Arrays;
2327

2428
/**
25-
* A {@link CornerSize} that takes a percent and computes the size used based on the height of the
26-
* shape.
29+
* A {@link CornerSize} that takes a percent and computes the size used based on the length of the
30+
* shortest edge adjacent to the corner.
2731
*/
2832
public final class RelativeCornerSize implements CornerSize {
2933

3034
private final float percent;
3135

36+
/**
37+
* Creates a relative corner size from a given corner size.
38+
*
39+
* @hide
40+
*/
41+
@RestrictTo(LIBRARY_GROUP)
42+
@NonNull
43+
public static RelativeCornerSize createFromCornerSize(
44+
@NonNull final RectF bounds, @NonNull final CornerSize cornerSize) {
45+
return cornerSize instanceof RelativeCornerSize
46+
? (RelativeCornerSize) cornerSize
47+
: new RelativeCornerSize(cornerSize.getCornerSize(bounds) / getMaxCornerSize(bounds));
48+
}
49+
50+
private static float getMaxCornerSize(@NonNull RectF bounds) {
51+
return min(bounds.width(), bounds.height());
52+
}
53+
3254
/**
3355
* @param percent The relative size of the corner in range [0,1] where 0 is no size and 1 is the
34-
* full bounds height.
56+
* largest possible corner size.
3557
*/
3658
public RelativeCornerSize(@FloatRange(from = 0.0f, to = 1.0f) float percent) {
3759
this.percent = percent;
@@ -45,7 +67,7 @@ public float getRelativePercent() {
4567

4668
@Override
4769
public float getCornerSize(@NonNull RectF bounds) {
48-
return percent * bounds.height();
70+
return percent * getMaxCornerSize(bounds);
4971
}
5072

5173
@Override

lib/java/com/google/android/material/transition/TransitionUtils.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import com.google.android.material.shape.CornerSize;
4545
import com.google.android.material.shape.RelativeCornerSize;
4646
import com.google.android.material.shape.ShapeAppearanceModel;
47-
import com.google.android.material.shape.ShapeAppearanceModel.CornerSizeUnaryOperator;
4847

4948
class TransitionUtils {
5049

@@ -123,15 +122,7 @@ static PathMotion resolveThemePath(Context context, @AttrRes int attrResId) {
123122
static ShapeAppearanceModel convertToRelativeCornerSizes(
124123
ShapeAppearanceModel shapeAppearanceModel, final RectF bounds) {
125124
return shapeAppearanceModel.withTransformedCornerSizes(
126-
new CornerSizeUnaryOperator() {
127-
@NonNull
128-
@Override
129-
public CornerSize apply(@NonNull CornerSize cornerSize) {
130-
return cornerSize instanceof RelativeCornerSize
131-
? cornerSize
132-
: new RelativeCornerSize(cornerSize.getCornerSize(bounds) / bounds.height());
133-
}
134-
});
125+
cornerSize -> RelativeCornerSize.createFromCornerSize(bounds, cornerSize));
135126
}
136127

137128
// TODO: rethink how to interpolate more than just corner size

lib/java/com/google/android/material/transition/platform/TransitionUtils.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import com.google.android.material.shape.CornerSize;
4949
import com.google.android.material.shape.RelativeCornerSize;
5050
import com.google.android.material.shape.ShapeAppearanceModel;
51-
import com.google.android.material.shape.ShapeAppearanceModel.CornerSizeUnaryOperator;
5251

5352
@androidx.annotation.RequiresApi(android.os.Build.VERSION_CODES.LOLLIPOP)
5453
class TransitionUtils {
@@ -128,15 +127,7 @@ static PathMotion resolveThemePath(Context context, @AttrRes int attrResId) {
128127
static ShapeAppearanceModel convertToRelativeCornerSizes(
129128
ShapeAppearanceModel shapeAppearanceModel, final RectF bounds) {
130129
return shapeAppearanceModel.withTransformedCornerSizes(
131-
new CornerSizeUnaryOperator() {
132-
@NonNull
133-
@Override
134-
public CornerSize apply(@NonNull CornerSize cornerSize) {
135-
return cornerSize instanceof RelativeCornerSize
136-
? cornerSize
137-
: new RelativeCornerSize(cornerSize.getCornerSize(bounds) / bounds.height());
138-
}
139-
});
130+
cornerSize -> RelativeCornerSize.createFromCornerSize(bounds, cornerSize));
140131
}
141132

142133
// TODO: rethink how to interpolate more than just corner size

0 commit comments

Comments
 (0)