Skip to content

Commit 6a7b506

Browse files
committed
[Carousel] Update mask size when MaskableFrameLayout size is changed
1 parent c04eaba commit 6a7b506

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

lib/java/com/google/android/material/carousel/MaskableFrameLayout.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
/** A {@link FrameLayout} than is able to mask itself and all children. */
4141
public class MaskableFrameLayout extends FrameLayout implements Maskable, Shapeable {
4242

43-
private float maskXPercentage = 0F;
43+
private static final float MASK_PERCENTAGE_UNSET = -1F;
44+
private float maskXPercentage = 0f;
45+
4446
private final RectF maskRect = new RectF();
4547
@Nullable private OnMaskChangedListener onMaskChangedListener;
4648
@NonNull private ShapeAppearanceModel shapeAppearanceModel;
@@ -65,7 +67,14 @@ public MaskableFrameLayout(
6567
@Override
6668
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6769
super.onSizeChanged(w, h, oldw, oldh);
68-
onMaskChanged();
70+
71+
if (maskXPercentage != MASK_PERCENTAGE_UNSET) {
72+
// The mask percentage is set, so it is necessary to recalculate the mask size after each
73+
// resizing of the view.
74+
updateMaskXPercentage(maskXPercentage);
75+
} else {
76+
// The mask is set directly, so no recalculation is required.
77+
}
6978
}
7079

7180
@Override
@@ -121,13 +130,7 @@ public ShapeAppearanceModel getShapeAppearanceModel() {
121130
@Deprecated
122131
public void setMaskXPercentage(float percentage) {
123132
percentage = MathUtils.clamp(percentage, 0F, 1F);
124-
if (maskXPercentage != percentage) {
125-
this.maskXPercentage = percentage;
126-
// Translate the percentage into an actual pixel value of how much of this view should be
127-
// masked away.
128-
float maskWidth = AnimationUtils.lerp(0f, getWidth() / 2F, 0f, 1f, maskXPercentage);
129-
setMaskRectF(new RectF(maskWidth, 0F, (getWidth() - maskWidth), getHeight()));
130-
}
133+
updateMaskXPercentage(percentage);
131134
}
132135

133136
/**
@@ -137,14 +140,33 @@ public void setMaskXPercentage(float percentage) {
137140
*/
138141
@Override
139142
public void setMaskRectF(@NonNull RectF maskRect) {
140-
this.maskRect.set(maskRect);
141-
onMaskChanged();
143+
maskXPercentage = MASK_PERCENTAGE_UNSET;
144+
updateMaskRectF(maskRect);
145+
}
146+
147+
private void updateMaskXPercentage(float percentage) {
148+
if (maskXPercentage != percentage) {
149+
maskXPercentage = percentage;
150+
151+
// Translate the percentage into an actual pixel value of how much of this view should be
152+
// masked away.
153+
float maskWidth = AnimationUtils.lerp(0f, getWidth() / 2F, 0f, 1f, maskXPercentage);
154+
updateMaskRectF(new RectF(maskWidth, 0F, (getWidth() - maskWidth), getHeight()));
155+
}
156+
}
157+
158+
private void updateMaskRectF(@NonNull RectF rect) {
159+
if (!maskRect.equals(rect)) {
160+
maskRect.set(rect);
161+
onMaskChanged();
162+
}
142163
}
143164

144165
/**
145166
* Gets the percentage by which this {@link View} is masked by along the x axis.
146167
*
147-
* @return a float between 0 and 1 where 0 is fully unmasked and 1 is fully masked.
168+
* @return a float between 0 and 1, where 0 is fully unmasked and 1 is fully masked, or
169+
* {@value MASK_PERCENTAGE_UNSET}, if the mask is set manually.
148170
* @deprecated This is no longer used as {@link CarouselLayoutManager} calculates its own mask
149171
* percentages.
150172
*/

0 commit comments

Comments
 (0)