From 080a4db213a47dae1e65f6b4edb2235f183599bd Mon Sep 17 00:00:00 2001 From: pubiqq Date: Mon, 13 Jan 2025 21:54:26 +0300 Subject: [PATCH] [Slider] Improve track icon bounds calculation --- .../android/material/slider/BaseSlider.java | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/java/com/google/android/material/slider/BaseSlider.java b/lib/java/com/google/android/material/slider/BaseSlider.java index 0e19663f40a..f7b5c589bd1 100644 --- a/lib/java/com/google/android/material/slider/BaseSlider.java +++ b/lib/java/com/google/android/material/slider/BaseSlider.java @@ -349,6 +349,7 @@ abstract class BaseSlider< private boolean trackIconInactiveEndMutated = false; @Nullable private ColorStateList trackIconInactiveColor; @Px private int trackIconSize; + @Px private int trackIconPadding; private int labelPadding; private float touchDownX; private MotionEvent lastEvent; @@ -518,6 +519,8 @@ private void loadResources(@NonNull Resources resources) { minTickSpacing = resources.getDimensionPixelSize(R.dimen.mtrl_slider_tick_min_spacing); labelPadding = resources.getDimensionPixelSize(R.dimen.mtrl_slider_label_padding); + + trackIconPadding = resources.getDimensionPixelOffset(R.dimen.m3_slider_track_icon_padding); } private void processAttributes(Context context, AttributeSet attrs, int defStyleAttr) { @@ -2706,7 +2709,7 @@ private void calculateBoundsAndDrawTrackIcon( @Nullable Drawable icon, boolean isStart) { if (icon != null) { - calculateTrackIconBounds(trackBounds, iconRectF, trackIconSize, isStart); + calculateTrackIconBounds(trackBounds, iconRectF, trackIconSize, trackIconPadding, isStart); if (!iconRectF.isEmpty()) { drawTrackIcon(canvas, iconRectF, icon); } @@ -2726,28 +2729,25 @@ private void drawTrackIcon( } private void calculateTrackIconBounds( - @NonNull RectF trackBounds, @NonNull RectF iconBounds, @Px int iconSize, boolean isStart) { - float iconPadding = getResources().getDimension(R.dimen.m3_slider_track_icon_padding); - float iconLeft; - if (isStart) { - iconLeft = - isRtl() || isVertical() - ? trackBounds.right - iconSize - iconPadding - : trackBounds.left + iconPadding; + @NonNull RectF trackBounds, + @NonNull RectF iconBounds, + @Px int iconSize, + @Px int iconPadding, + boolean isStart) { + if (trackBounds.right - trackBounds.left >= iconSize + 2 * iconPadding) { + float iconLeft = (isStart ^ (isRtl() || isVertical())) + ? trackBounds.left + iconPadding + : trackBounds.right - iconPadding - iconSize; + float iconRight = iconLeft + iconSize; + + float iconTop = calculateTrackCenter() - (float) iconSize / 2; + float iconBottom = iconTop + iconSize; + + iconBounds.set(iconLeft, iconTop, iconRight, iconBottom); } else { - iconLeft = - isRtl() || isVertical() - ? trackBounds.left + iconPadding - : trackBounds.right - iconSize - iconPadding; - } - float iconRight = iconLeft + iconSize; - int iconTop = calculateTrackCenter() - iconSize / 2; - if (trackBounds.left > iconLeft - iconPadding || trackBounds.right < iconRight + iconPadding) { // not enough space to draw icon iconBounds.setEmpty(); - return; } - iconBounds.set(iconLeft, iconTop, iconRight, iconTop + iconSize); } private boolean hasGapBetweenThumbAndTrack() {