From 7d6a977d50b798e991d31d5223a4f3efcdfd6a24 Mon Sep 17 00:00:00 2001 From: rightnao Date: Thu, 1 Jun 2023 18:27:22 +0000 Subject: [PATCH] [Carousel] Fix contained mask logic to only update masks when it is still in view, and remove restrictions on mask size with childWidth/2F. The only restriction is that the right of the mask must be greater than the left of the mask. PiperOrigin-RevId: 537080963 --- .../material/carousel/CarouselLayoutManager.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/java/com/google/android/material/carousel/CarouselLayoutManager.java b/lib/java/com/google/android/material/carousel/CarouselLayoutManager.java index b84fb8df356..542643a56ec 100644 --- a/lib/java/com/google/android/material/carousel/CarouselLayoutManager.java +++ b/lib/java/com/google/android/material/carousel/CarouselLayoutManager.java @@ -21,7 +21,6 @@ import static com.google.android.material.animation.AnimationUtils.lerp; import static java.lang.Math.abs; import static java.lang.Math.max; -import static java.lang.Math.min; import android.graphics.Canvas; import android.graphics.Color; @@ -773,16 +772,17 @@ private void updateChildMaskForLocation( // If the carousel is a CONTAINED carousel, ensure the mask collapses against the side of the // container instead of bleeding and being clipped by the RecyclerView's bounds. + // Only do this if there is only one side of the mask that is out of bounds; if + // both sides are out of bounds on the same side, then the whole mask is out of view. if (carouselStrategy.isContained()) { float offsetCx = calculateChildOffsetCenterForLocation(child, childCenterLocation, range); float maskedLeft = offsetCx - (maskRect.width() / 2F); float maskedRight = offsetCx + (maskRect.width() / 2F); - - if (maskedLeft < getParentLeft()) { - maskRect.left = min(maskRect.left + (getParentLeft() - maskedLeft), childWidth / 2F); + if (maskedLeft < getParentLeft() && maskedRight >= getParentLeft()) { + maskRect.left = maskRect.left + (getParentLeft() - maskedLeft); } - if (maskedRight > getParentRight()) { - maskRect.right = max(maskRect.right - (maskedRight - getParentRight()), childWidth / 2F); + if (maskedRight > getParentRight() && maskedLeft <= getParentRight()) { + maskRect.right = max(maskRect.right - (maskedRight - getParentRight()), maskRect.left); } } ((Maskable) child).setMaskRectF(maskRect);