From 0437dc5fd010715429822287ec12a8f77bffbe92 Mon Sep 17 00:00:00 2001 From: pubiqq Date: Thu, 19 Dec 2024 21:35:14 +0300 Subject: [PATCH] [Slider] Always update track bounds before drawing icons --- .../android/material/slider/BaseSlider.java | 72 ++++++++++++------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/lib/java/com/google/android/material/slider/BaseSlider.java b/lib/java/com/google/android/material/slider/BaseSlider.java index 6212302bca6..0849f6a37a0 100644 --- a/lib/java/com/google/android/material/slider/BaseSlider.java +++ b/lib/java/com/google/android/material/slider/BaseSlider.java @@ -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); @@ -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(); } } @@ -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 @@ -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; }