Skip to content

Commit

Permalink
[Slider] Fix incorrect style of stop indicators near handles
Browse files Browse the repository at this point in the history
Resolves #4097

GIT_ORIGIN_REV_ID=927159e78777a16472d96b2740295bdbce6361ab
PiperOrigin-RevId: 615993493

(cherry picked from commit bb646b6)
  • Loading branch information
pubiqq authored and hunterstich committed Mar 21, 2024
1 parent 884465b commit d61cffd
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2218,25 +2218,41 @@ private void maybeDrawTicks(@NonNull Canvas canvas) {
}

float[] activeRange = getActiveRange();
int leftPivotIndex = pivotIndex(ticksCoordinates, activeRange[0]);
int rightPivotIndex = pivotIndex(ticksCoordinates, activeRange[1]);

// Draw inactive ticks to the left of the smallest thumb.
canvas.drawPoints(ticksCoordinates, 0, leftPivotIndex * 2, inactiveTicksPaint);
// Calculate the index of the left tick of the active track.
final int leftActiveTickIndex =
(int) Math.ceil(activeRange[0] * (ticksCoordinates.length / 2f - 1));

// Draw active ticks between the thumbs.
canvas.drawPoints(
ticksCoordinates,
leftPivotIndex * 2,
rightPivotIndex * 2 - leftPivotIndex * 2,
activeTicksPaint);
// Calculate the index of the right tick of the active track.
final int rightActiveTickIndex =
(int) Math.floor(activeRange[1] * (ticksCoordinates.length / 2f - 1));

// Draw inactive ticks to the right of the largest thumb.
canvas.drawPoints(
ticksCoordinates,
rightPivotIndex * 2,
ticksCoordinates.length - rightPivotIndex * 2,
inactiveTicksPaint);
// Draw ticks on the left inactive track (if any).
if (leftActiveTickIndex > 0) {
canvas.drawPoints(
ticksCoordinates,
0,
leftActiveTickIndex * 2,
inactiveTicksPaint);
}

// Draw ticks on the active track (if any).
if (leftActiveTickIndex <= rightActiveTickIndex) {
canvas.drawPoints(
ticksCoordinates,
leftActiveTickIndex * 2,
(rightActiveTickIndex - leftActiveTickIndex + 1) * 2,
activeTicksPaint);
}

// Draw ticks on the right inactive track (if any).
if ((rightActiveTickIndex + 1) * 2 < ticksCoordinates.length) {
canvas.drawPoints(
ticksCoordinates,
(rightActiveTickIndex + 1) * 2,
ticksCoordinates.length - (rightActiveTickIndex + 1) * 2,
inactiveTicksPaint);
}
}

private void maybeDrawStopIndicator(@NonNull Canvas canvas, int yCenter) {
Expand Down Expand Up @@ -2413,17 +2429,6 @@ && abs(lastEvent.getY() - event.getY()) <= scaledTouchSlop) {
return true;
}

/**
* Calculates the index the closest tick coordinates that the thumb should snap to.
*
* @param coordinates Tick coordinates defined in {@code #setTicksCoordinates()}.
* @param position Actual thumb position.
* @return Index of the closest tick coordinate.
*/
private static int pivotIndex(float[] coordinates, float position) {
return Math.round(position * (coordinates.length / 2f - 1));
}

private double snapPosition(float position) {
if (stepSize > 0.0f) {
int stepCount = (int) ((valueTo - valueFrom) / stepSize);
Expand Down

0 comments on commit d61cffd

Please sign in to comment.