Skip to content

Commit

Permalink
[Carousel] Update mask size on size change if mask x percentage has b…
Browse files Browse the repository at this point in the history
…een set

Resolves #3450

GIT_ORIGIN_REV_ID=df8a325da6e4a796ab33d93e2ba165e40da8733e
Co-authored-by: imhappi
PiperOrigin-RevId: 558207460
  • Loading branch information
pubiqq authored and imhappi committed Aug 18, 2023
1 parent 44bfe2d commit dc91b39
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Expand Up @@ -40,7 +40,9 @@
/** A {@link FrameLayout} than is able to mask itself and all children. */
public class MaskableFrameLayout extends FrameLayout implements Maskable, Shapeable {

private float maskXPercentage = 0F;
private static final int NOT_SET = -1;

private float maskXPercentage = NOT_SET;
private final RectF maskRect = new RectF();
@Nullable private OnMaskChangedListener onMaskChangedListener;
@NonNull private ShapeAppearanceModel shapeAppearanceModel;
Expand All @@ -65,7 +67,9 @@ public MaskableFrameLayout(
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
onMaskChanged();
if (maskXPercentage != NOT_SET) {
updateMaskRectForMaskXPercentage();
}
}

@Override
Expand Down Expand Up @@ -123,10 +127,16 @@ public void setMaskXPercentage(float percentage) {
percentage = MathUtils.clamp(percentage, 0F, 1F);
if (maskXPercentage != percentage) {
this.maskXPercentage = percentage;
// Translate the percentage into an actual pixel value of how much of this view should be
// masked away.
float maskWidth = AnimationUtils.lerp(0f, getWidth() / 2F, 0f, 1f, maskXPercentage);
setMaskRectF(new RectF(maskWidth, 0F, (getWidth() - maskWidth), getHeight()));
updateMaskRectForMaskXPercentage();
}
}

private void updateMaskRectForMaskXPercentage() {
if (maskXPercentage != NOT_SET) {
// Translate the percentage into an actual pixel value of how much of this view should be
// masked away.
float maskWidth = AnimationUtils.lerp(0f, getWidth() / 2F, 0f, 1f, maskXPercentage);
setMaskRectF(new RectF(maskWidth, 0F, (getWidth() - maskWidth), getHeight()));
}
}

Expand Down
Expand Up @@ -28,6 +28,7 @@
import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.graphics.RectF;
import android.os.Build.VERSION_CODES;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
Expand Down Expand Up @@ -107,8 +108,9 @@ public void testScrollBeyondMaxHorizontalScroll_shouldLimitToMaxScrollOffset() t
@Test
public void testSingleItem_shouldBeInFocalRange() throws Throwable {
setAdapterItems(recyclerView, layoutManager, adapter, CarouselHelper.createDataSetWithSize(1));
RectF maskRect = ((Maskable) recyclerView.getChildAt(0)).getMaskRectF();

assertThat(((Maskable) recyclerView.getChildAt(0)).getMaskXPercentage()).isEqualTo(0F);
assertThat((int) (maskRect.right - maskRect.left)).isEqualTo(DEFAULT_ITEM_WIDTH);
}

@Test
Expand Down
Expand Up @@ -254,8 +254,9 @@ public void testEmptyAdapter_shouldClearAllViewsFromRecyclerView() throws Throwa
@Test
public void testSingleItem_shouldBeInFocalRange() throws Throwable {
setAdapterItems(recyclerView, layoutManager, adapter, createDataSetWithSize(1));
RectF maskRect = ((Maskable) recyclerView.getChildAt(0)).getMaskRectF();

assertThat(((Maskable) recyclerView.getChildAt(0)).getMaskXPercentage()).isEqualTo(0F);
assertThat((int) (maskRect.right - maskRect.left)).isEqualTo(DEFAULT_ITEM_WIDTH);
}

@Test
Expand Down

0 comments on commit dc91b39

Please sign in to comment.