Skip to content

Commit 4ce7e4c

Browse files
pubiqqleticiarossi
authored andcommitted
[Carousel] Reduce the number of truncations in intermediate calculations
Resolves #3581 Resolves #3579 GIT_ORIGIN_REV_ID=1bcb42de0535edac134a6fc27dcb14b22f7dc1a5 PiperOrigin-RevId: 565653783
1 parent 09382b8 commit 4ce7e4c

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,13 @@ public void onLayoutCompleted(State state) {
365365
* @param startPosition the adapter position from which to start adding views
366366
*/
367367
private void addViewsStart(Recycler recycler, int startPosition) {
368-
int start = calculateChildStartForFill(startPosition);
368+
float start = calculateChildStartForFill(startPosition);
369369
for (int i = startPosition; i >= 0; i--) {
370370
ChildCalculations calculations = makeChildCalculations(recycler, start, i);
371371
if (isLocOffsetOutOfFillBoundsStart(calculations.offsetCenter, calculations.range)) {
372372
break;
373373
}
374-
start = addStart(start, (int) currentKeylineState.getItemSize());
374+
start = addStart(start, currentKeylineState.getItemSize());
375375

376376
// If this child's start is beyond the end of the container, don't add the child but continue
377377
// to loop so we can eventually get to children that are within bounds.
@@ -392,13 +392,13 @@ private void addViewsStart(Recycler recycler, int startPosition) {
392392
* @param startPosition the adapter position from which to start adding views
393393
*/
394394
private void addViewsEnd(Recycler recycler, State state, int startPosition) {
395-
int start = calculateChildStartForFill(startPosition);
395+
float start = calculateChildStartForFill(startPosition);
396396
for (int i = startPosition; i < state.getItemCount(); i++) {
397397
ChildCalculations calculations = makeChildCalculations(recycler, start, i);
398398
if (isLocOffsetOutOfFillBoundsEnd(calculations.offsetCenter, calculations.range)) {
399399
break;
400400
}
401-
start = addEnd(start, (int) currentKeylineState.getItemSize());
401+
start = addEnd(start, currentKeylineState.getItemSize());
402402

403403
// If this child's end is beyond the start of the container, don't add the child but continue
404404
// to loop so we can eventually get to children that are within bounds.
@@ -473,11 +473,10 @@ private void validateChildOrderIfDebugging() {
473473
* @return a {@link ChildCalculations} object
474474
*/
475475
private ChildCalculations makeChildCalculations(Recycler recycler, float start, int position) {
476-
float halfItemSize = currentKeylineState.getItemSize() / 2F;
477476
View child = recycler.getViewForPosition(position);
478477
measureChildWithMargins(child, 0, 0);
479478

480-
int center = addEnd((int) start, (int) halfItemSize);
479+
float center = addEnd(start, currentKeylineState.getItemSize() / 2F);
481480
KeylineRange range =
482481
getSurroundingKeylineRange(currentKeylineState.getKeylines(), center, false);
483482

@@ -517,7 +516,7 @@ private void addAndLayoutView(View child, int index, ChildCalculations calculati
517516
*/
518517
private boolean isLocOffsetOutOfFillBoundsStart(float locOffset, KeylineRange range) {
519518
float maskedSize = getMaskedItemSizeForLocOffset(locOffset, range);
520-
int maskedEnd = addEnd((int) locOffset, (int) (maskedSize / 2F));
519+
float maskedEnd = addEnd(locOffset, maskedSize / 2F);
521520
return isLayoutRtl() ? maskedEnd > getContainerSize() : maskedEnd < 0;
522521
}
523522

@@ -540,7 +539,7 @@ public boolean isHorizontal() {
540539
*/
541540
private boolean isLocOffsetOutOfFillBoundsEnd(float locOffset, KeylineRange range) {
542541
float maskedSize = getMaskedItemSizeForLocOffset(locOffset, range);
543-
int maskedStart = addStart((int) locOffset, (int) (maskedSize / 2F));
542+
float maskedStart = addStart(locOffset, maskedSize / 2F);
544543
return isLayoutRtl() ? maskedStart < 0 : maskedStart > getContainerSize();
545544
}
546545

@@ -755,8 +754,7 @@ private int calculateStartScroll(@NonNull KeylineStateList stateList) {
755754
Keyline startFocalKeyline =
756755
isRtl ? startState.getLastFocalKeyline() : startState.getFirstFocalKeyline();
757756
float firstItemDistanceFromStart = getPaddingStart() * (isRtl ? 1 : -1);
758-
int firstItemStart =
759-
addStart((int) startFocalKeyline.loc, (int) (startState.getItemSize() / 2F));
757+
float firstItemStart = addStart(startFocalKeyline.loc, startState.getItemSize() / 2F);
760758
return (int) (firstItemDistanceFromStart + getParentStart() - firstItemStart);
761759
}
762760

@@ -798,11 +796,11 @@ private int calculateEndScroll(State state, KeylineStateList stateList) {
798796
* @param startPosition the adapter position of the item whose start position will be calculated
799797
* @return the start location of the view at {@code startPosition} along the scroll axis
800798
*/
801-
private int calculateChildStartForFill(int startPosition) {
799+
private float calculateChildStartForFill(int startPosition) {
802800
float childScrollOffset = getParentStart() - scrollOffset;
803801
float positionOffset = currentKeylineState.getItemSize() * startPosition;
804802

805-
return addEnd((int) childScrollOffset, (int) positionOffset);
803+
return addEnd(childScrollOffset, positionOffset);
806804
}
807805

808806
/**
@@ -1013,12 +1011,12 @@ boolean isLayoutRtl() {
10131011
}
10141012

10151013
/** Moves {@code value} towards the start of the container by {@code amount}. */
1016-
private int addStart(int value, int amount) {
1014+
private float addStart(float value, float amount) {
10171015
return isLayoutRtl() ? value + amount : value - amount;
10181016
}
10191017

10201018
/** Moves {@code value} towards the end of the container by {@code amount}. */
1021-
private int addEnd(int value, int amount) {
1019+
private float addEnd(float value, float amount) {
10221020
return isLayoutRtl() ? value - amount : value + amount;
10231021
}
10241022

@@ -1251,12 +1249,12 @@ private int scrollBy(int distance, Recycler recycler, State state) {
12511249

12521250
float halfItemSize = currentKeylineState.getItemSize() / 2F;
12531251
int startPosition = getPosition(getChildAt(0));
1254-
int start = calculateChildStartForFill(startPosition);
1252+
float start = calculateChildStartForFill(startPosition);
12551253
Rect boundsRect = new Rect();
12561254
for (int i = 0; i < getChildCount(); i++) {
12571255
View child = getChildAt(i);
12581256
offsetChild(child, start, halfItemSize, boundsRect);
1259-
start = addEnd(start, (int) currentKeylineState.getItemSize());
1257+
start = addEnd(start, currentKeylineState.getItemSize());
12601258
}
12611259

12621260
// Fill any additional space caused by scrolling with more items.
@@ -1276,7 +1274,7 @@ private int scrollBy(int distance, Recycler recycler, State state) {
12761274
* @param boundsRect a Rect to use to find the current bounds of {@code child}
12771275
*/
12781276
private void offsetChild(View child, float startOffset, float halfItemSize, Rect boundsRect) {
1279-
int center = addEnd((int) startOffset, (int) halfItemSize);
1277+
float center = addEnd(startOffset, halfItemSize);
12801278
KeylineRange range =
12811279
getSurroundingKeylineRange(currentKeylineState.getKeylines(), center, false);
12821280
float offsetCenter = calculateChildOffsetCenterForLocation(child, center, range);

0 commit comments

Comments
 (0)