Skip to content

Commit

Permalink
[Slider] Improve track icon bounds calculation
Browse files Browse the repository at this point in the history
Resolves #4535

GIT_ORIGIN_REV_ID=080a4db213a47dae1e65f6b4edb2235f183599bd
PiperOrigin-RevId: 716316682
  • Loading branch information
pubiqq authored and pekingme committed Jan 16, 2025
1 parent 867fd01 commit 54778a9
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,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;
Expand Down Expand Up @@ -516,6 +517,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) {
Expand Down Expand Up @@ -2692,7 +2695,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);
}
Expand All @@ -2712,28 +2715,24 @@ 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;
} else {
iconLeft =
isRtl() || isVertical()
@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 - iconSize - iconPadding;
}
float iconRight = iconLeft + iconSize;
int iconTop = calculateTrackCenter() - iconSize / 2;
if (trackBounds.left > iconLeft - iconPadding || trackBounds.right < iconRight + iconPadding) {
: trackBounds.right - iconPadding - iconSize;
float iconTop = calculateTrackCenter() - iconSize / 2f;
float iconRight = iconLeft + iconSize;
float iconBottom = iconTop + iconSize;
iconBounds.set(iconLeft, iconTop, iconRight, iconBottom);
} else {
// not enough space to draw icon
iconBounds.setEmpty();
return;
}
iconBounds.set(iconLeft, iconTop, iconRight, iconTop + iconSize);
}

private boolean hasGapBetweenThumbAndTrack() {
Expand Down

0 comments on commit 54778a9

Please sign in to comment.