Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Slider] Always update track bounds before drawing icons #4488

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2410,14 +2410,9 @@ protected void onDraw(@NonNull Canvas canvas) {

int yCenter = calculateTrackCenter();

float first = values.get(0);
float last = values.get(values.size() - 1);
if (last < valueTo || (values.size() > 1 && first > valueFrom)) {
drawInactiveTrack(canvas, trackWidth, yCenter);
}
if (last > valueFrom) {
drawActiveTrack(canvas, trackWidth, yCenter);
}
drawInactiveTracks(canvas, trackWidth, yCenter);
drawActiveTracks(canvas, trackWidth, yCenter);

drawTrackIcons(canvas, activeTrackRect, inactiveTrackRect);

maybeDrawTicks(canvas);
Expand Down Expand Up @@ -2446,34 +2441,52 @@ private float[] getActiveRange() {
return isRtl() || isVertical() ? new float[] {right, left} : new float[] {left, right};
}

private void drawInactiveTrack(@NonNull Canvas canvas, int width, int yCenter) {
private void drawInactiveTracks(@NonNull Canvas canvas, int width, int yCenter) {
// The order of method calls does not affect the result of drawing, but for
// the `drawTrackIcons` method we need `inactiveTrackRect` to be set to the bounds
// of the end inactive track.
if (isRtl() || isVertical()) {
drawInactiveRightTrack(canvas, inactiveTrackRect, width, yCenter);
drawInactiveLeftTrack(canvas, inactiveTrackRect, width, yCenter);
} else {
drawInactiveLeftTrack(canvas, inactiveTrackRect, width, yCenter);
drawInactiveRightTrack(canvas, inactiveTrackRect, width, yCenter);
}
}

private void drawInactiveLeftTrack(
@NonNull Canvas canvas, @NonNull RectF trackBounds, int width, int yCenter) {
int trackCornerSize = getTrackCornerSize();
float[] activeRange = getActiveRange();
float right = trackSidePadding + activeRange[1] * width;
if (right < trackSidePadding + width) {
inactiveTrackRect.set(
right + thumbTrackGapSize,
float left = trackSidePadding + activeRange[0] * width;
if (left > trackSidePadding) {
trackBounds.set(
trackSidePadding - trackCornerSize,
yCenter - trackThickness / 2f,
trackSidePadding + width + trackCornerSize,
left - thumbTrackGapSize,
yCenter + trackThickness / 2f);
updateTrack(
canvas,
inactiveTrackPaint,
inactiveTrackRect,
trackCornerSize,
FullCornerDirection.RIGHT);
canvas, inactiveTrackPaint, trackBounds, trackCornerSize, FullCornerDirection.LEFT);
} else {
trackBounds.setEmpty();
}
}

// Also draw inactive track to the left if there is any
float left = trackSidePadding + activeRange[0] * width;
if (left > trackSidePadding) {
inactiveTrackRect.set(
trackSidePadding - trackCornerSize,
private void drawInactiveRightTrack(
@NonNull Canvas canvas, @NonNull RectF trackBounds, int width, int yCenter) {
int trackCornerSize = getTrackCornerSize();
float[] activeRange = getActiveRange();
float right = trackSidePadding + activeRange[1] * width;
if (right < trackSidePadding + width) {
trackBounds.set(
right + thumbTrackGapSize,
yCenter - trackThickness / 2f,
left - thumbTrackGapSize,
trackSidePadding + width + trackCornerSize,
yCenter + trackThickness / 2f);
updateTrack(
canvas, inactiveTrackPaint, inactiveTrackRect, trackCornerSize, FullCornerDirection.LEFT);
canvas, inactiveTrackPaint, trackBounds, trackCornerSize, FullCornerDirection.RIGHT);
} else {
trackBounds.setEmpty();
}
}

Expand All @@ -2489,10 +2502,14 @@ private float normalizeValue(float value) {
return normalized;
}

private void drawActiveTrack(@NonNull Canvas canvas, int width, int yCenter) {
private void drawActiveTracks(@NonNull Canvas canvas, int width, int yCenter) {
float[] activeRange = getActiveRange();
float right = trackSidePadding + activeRange[1] * width;
float left = trackSidePadding + activeRange[0] * width;
if (left >= right) {
activeTrackRect.setEmpty();
return;
}

FullCornerDirection direction = FullCornerDirection.NONE;
if (values.size() == 1) { // Only 1 thumb
Expand Down Expand Up @@ -2532,6 +2549,7 @@ private void drawActiveTrack(@NonNull Canvas canvas, int width, int yCenter) {

// Nothing to draw if left is bigger than right.
if (left >= right) {
activeTrackRect.setEmpty();
continue;
}

Expand Down