Skip to content

Commit

Permalink
[Carousel] Fix item masking for API 21
Browse files Browse the repository at this point in the history
Resolves #3330

GIT_ORIGIN_REV_ID=1c46e2882b074e35ab1f19af0af00f88dee84f4c
PiperOrigin-RevId: 523119333

(cherry picked from commit 7bc26e5)
  • Loading branch information
pubiqq authored and drchen committed Apr 13, 2023
1 parent 93913a4 commit 3514524
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public MaskableFrameLayout(
private MaskableDelegate createMaskableDelegate() {
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
return new MaskableDelegateV33(this);
} else if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
return new MaskableDelegateV21(this);
} else if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP_MR1) {
return new MaskableDelegateV22(this);
} else {
return new MaskableDelegateV14();
}
Expand Down Expand Up @@ -302,7 +302,7 @@ void maybeClip(Canvas canvas, CanvasOperation op) {
}

/**
* A {@link MaskableDelegate} implementation for API 14-20 that always clips using canvas
* A {@link MaskableDelegate} implementation for API 14-21 that always clips using canvas
* clipping.
*/
private static class MaskableDelegateV14 extends MaskableDelegate {
Expand All @@ -325,19 +325,20 @@ void invalidateClippingMethod(View view) {
}

/**
* A {@link MaskableDelegate} for API 21-32 that uses {@link ViewOutlineProvider} to clip when the
* A {@link MaskableDelegate} for API 22-32 that uses {@link ViewOutlineProvider} to clip when the
* shape being clipped is a round rect with symmetrical corners and canvas clipping for all other
* shapes.
* shapes. This way is not used for API 21 because outline invalidation is incorrectly implemented
* in this version.
*
* <p>{@link Outline#setRoundRect(Rect, float)} is only able to clip to a rectangle with a single
* corner radius for all four corners.
*/
@RequiresApi(VERSION_CODES.LOLLIPOP)
private static class MaskableDelegateV21 extends MaskableDelegate {
@RequiresApi(VERSION_CODES.LOLLIPOP_MR1)
private static class MaskableDelegateV22 extends MaskableDelegate {

private boolean isShapeRoundRect = false;

MaskableDelegateV21(View view) {
MaskableDelegateV22(View view) {
initMaskOutlineProvider(view);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public void testShapeAppearanceWithAbsoluteCornerSizes_shouldBeClamped() {
assertThat(topRightCornerSize.getCornerSize(maskableFrameLayout.getMaskRectF())).isEqualTo(25F);
}

@RequiresApi(api = VERSION_CODES.LOLLIPOP)
@Config(sdk = VERSION_CODES.LOLLIPOP)
@RequiresApi(api = VERSION_CODES.LOLLIPOP_MR1)
@Config(sdk = VERSION_CODES.LOLLIPOP_MR1)
@Test
public void testForceCompatClipping_shouldNotUseViewOutlineProvider() {
MaskableFrameLayout maskableFrameLayout = createMaskableFrameLayoutWithSize(50, 50);
Expand All @@ -87,21 +87,21 @@ public void testForceCompatClipping_shouldNotUseViewOutlineProvider() {
assertThat(maskableFrameLayout.getClipToOutline()).isFalse();
}

@RequiresApi(api = VERSION_CODES.LOLLIPOP)
@Config(sdk = VERSION_CODES.LOLLIPOP)
@RequiresApi(api = VERSION_CODES.LOLLIPOP_MR1)
@Config(sdk = VERSION_CODES.LOLLIPOP_MR1)
@Test
public void testRoundedCornersApi21_usesViewOutlineProvider() {
public void testRoundedCornersApi22_usesViewOutlineProvider() {
MaskableFrameLayout maskableFrameLayout = createMaskableFrameLayoutWithSize(50, 50);
ShapeAppearanceModel model = new ShapeAppearanceModel.Builder().setAllCornerSizes(10F).build();
maskableFrameLayout.setShapeAppearanceModel(model);

assertThat(maskableFrameLayout.getClipToOutline()).isTrue();
}

@RequiresApi(api = VERSION_CODES.LOLLIPOP)
@Config(sdk = VERSION_CODES.LOLLIPOP)
@RequiresApi(api = VERSION_CODES.LOLLIPOP_MR1)
@Config(sdk = VERSION_CODES.LOLLIPOP_MR1)
@Test
public void testCutCornersApi21_doesNotUseViewOutlineProvider() {
public void testCutCornersApi22_doesNotUseViewOutlineProvider() {
MaskableFrameLayout maskableFrameLayout = createMaskableFrameLayoutWithSize(50, 50);
ShapeAppearanceModel model =
new ShapeAppearanceModel.Builder()
Expand Down

0 comments on commit 3514524

Please sign in to comment.