Skip to content

Commit

Permalink
[Carousel] Fix issue with next carousel item not being masked properly
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 551644125
  • Loading branch information
imhappi authored and pekingme committed Jul 28, 2023
1 parent 4e5166f commit a16f180
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
Expand Up @@ -18,6 +18,7 @@
import static com.google.android.material.carousel.CarouselLayoutManager.HORIZONTAL;
import static com.google.android.material.carousel.CarouselLayoutManager.VERTICAL;
import static java.lang.Math.max;
import static java.lang.Math.min;

import android.graphics.Rect;
import android.graphics.RectF;
Expand Down Expand Up @@ -222,9 +223,15 @@ public void moveMaskOnEdgeOutsideBounds(
RectF maskRect, RectF offsetMaskRect, RectF parentBoundsRect) {
if (offsetMaskRect.bottom <= parentBoundsRect.top) {
maskRect.bottom = (float) Math.floor(maskRect.bottom) - 1;
// Make sure that maskRect.top is equal to or smaller than maskRect.bottom.
// Otherwise the mask may not be re-drawn as the mask is invalid.
maskRect.top = min(maskRect.top, maskRect.bottom);
}
if (offsetMaskRect.top >= parentBoundsRect.bottom) {
maskRect.top = (float) Math.ceil(maskRect.top) + 1;
// Make sure that maskRect.bottom is equal to or bigger than maskRect.top.
// Otherwise the mask may not be re-drawn as the mask is invalid.
maskRect.bottom = max(maskRect.top, maskRect.bottom);
}
}

Expand Down Expand Up @@ -310,9 +317,15 @@ public void moveMaskOnEdgeOutsideBounds(
RectF maskRect, RectF offsetMaskRect, RectF parentBoundsRect) {
if (offsetMaskRect.right <= parentBoundsRect.left) {
maskRect.right = (float) Math.floor(maskRect.right) - 1;
// Make sure that maskRect.left is equal to or smaller than maskRect.right.
// Otherwise the mask may not be re-drawn as the mask is invalid.
maskRect.left = min(maskRect.left, maskRect.right);
}
if (offsetMaskRect.left >= parentBoundsRect.right) {
maskRect.left = (float) Math.ceil(maskRect.left) + 1;
// Make sure that maskRect.right is equal to or bigger than maskRect.left.
// Otherwise the mask may not be re-drawn as the mask is invalid.
maskRect.right = max(maskRect.left, maskRect.right);
}
}

Expand Down
Expand Up @@ -167,9 +167,6 @@ public void setOnMaskChangedListener(@Nullable OnMaskChangedListener onMaskChang
}

private void onMaskChanged() {
if (getWidth() == 0) {
return;
}
shapeableDelegate.onMaskChanged(this, maskRect);
if (onMaskChangedListener != null) {
onMaskChangedListener.onMaskChanged(maskRect);
Expand Down
Expand Up @@ -140,12 +140,16 @@ public void onMaskChanged(@NonNull View view, @NonNull RectF maskBounds) {
}

private void updateShapePath() {
if (!maskBounds.isEmpty() && shapeAppearanceModel != null) {
if (isMaskBoundsValid() && shapeAppearanceModel != null) {
ShapeAppearancePathProvider.getInstance()
.calculatePath(shapeAppearanceModel, 1F, maskBounds, shapePath);
}
}

private boolean isMaskBoundsValid() {
return maskBounds.left <= maskBounds.right && maskBounds.top <= maskBounds.bottom;
}

public void maybeClip(@NonNull Canvas canvas, @NonNull CanvasOperation op) {
if (shouldUseCompatClipping() && !shapePath.isEmpty()) {
canvas.save();
Expand Down

0 comments on commit a16f180

Please sign in to comment.