Skip to content

Commit 95e1c44

Browse files
paulfthomasraajkumars
authored andcommitted
[NTC][Slider] Internal changes
PiperOrigin-RevId: 587826282
1 parent bbd12e6 commit 95e1c44

File tree

5 files changed

+93
-55
lines changed

5 files changed

+93
-55
lines changed

catalog/java/io/material/catalog/slider/SliderMainDemoFragment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public View onCreateDemoView(
7272
RangeSlider rangeSlider = view.findViewById(R.id.range_slider);
7373
rangeSlider.addOnSliderTouchListener(rangeSliderTouchListener);
7474

75+
RangeSlider rangeSliderMulti = view.findViewById(R.id.range_slider_multi);
76+
rangeSliderMulti.addOnSliderTouchListener(rangeSliderTouchListener);
77+
7578
Button button = view.findViewById(R.id.button);
7679
button.setOnClickListener(v -> slider.setValue(slider.getValueTo()));
7780

catalog/java/io/material/catalog/slider/res/layout/cat_slider_fragment.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@
4343
android:valueFrom="0.0"
4444
android:valueTo="11.0" />
4545

46+
<com.google.android.material.slider.RangeSlider
47+
android:id="@+id/range_slider_multi"
48+
android:layout_width="match_parent"
49+
android:layout_height="wrap_content"
50+
android:layout_marginTop="32dp"
51+
android:layout_gravity="center"
52+
app:values="@array/initial_slider_multi_values"
53+
android:valueFrom="0.0"
54+
android:valueTo="11.0" />
55+
4656
<Button
4757
android:id="@+id/button"
4858
android:layout_width="wrap_content"

catalog/java/io/material/catalog/slider/res/values/arrays.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@
1919
<item>2.0</item>
2020
<item>7.0</item>
2121
</array>
22+
<array name="initial_slider_multi_values">
23+
<item>2.0</item>
24+
<item>7.0</item>
25+
<item>9.0</item>
26+
</array>
2227
</resources>

lib/java/com/google/android/material/slider/BaseSlider.java

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ abstract class BaseSlider<
286286
private int defaultTrackHeight;
287287
private int defaultTickActiveRadius;
288288
private int defaultTickInactiveRadius;
289+
private int minTickSpacing;
289290

290291
@Px private int minTouchTargetSize;
291292

@@ -339,25 +340,27 @@ abstract class BaseSlider<
339340
private float touchPosition;
340341
@SeparationUnit private int separationUnit = UNIT_PX;
341342

342-
@NonNull private final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener = () -> {
343-
if (shouldAlwaysShowLabel() && isEnabled()) {
344-
Rect contentViewBounds = new Rect();
345-
ViewUtils.getContentView(this).getHitRect(contentViewBounds);
346-
boolean isSliderVisibleOnScreen = getLocalVisibleRect(contentViewBounds);
347-
for (int i = 0; i < labels.size(); i++) {
348-
TooltipDrawable label = labels.get(i);
349-
// Get associated value for label
350-
if (i < values.size()) {
351-
positionLabel(label, values.get(i));
352-
}
353-
if (isSliderVisibleOnScreen) {
354-
ViewUtils.getContentViewOverlay(this).add(label);
355-
} else {
356-
ViewUtils.getContentViewOverlay(this).remove(label);
343+
@NonNull
344+
private final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener =
345+
() -> {
346+
if (shouldAlwaysShowLabel() && isEnabled()) {
347+
Rect contentViewBounds = new Rect();
348+
ViewUtils.getContentView(this).getHitRect(contentViewBounds);
349+
boolean isSliderVisibleOnScreen = getLocalVisibleRect(contentViewBounds);
350+
for (int i = 0; i < labels.size(); i++) {
351+
TooltipDrawable label = labels.get(i);
352+
// Get associated value for label
353+
if (i < values.size()) {
354+
positionLabel(label, values.get(i));
355+
}
356+
if (isSliderVisibleOnScreen) {
357+
ViewUtils.getContentViewOverlay(this).add(label);
358+
} else {
359+
ViewUtils.getContentViewOverlay(this).remove(label);
360+
}
361+
}
357362
}
358-
}
359-
}
360-
};
363+
};
361364

362365
/**
363366
* Determines the behavior of the label which can be any of the following.
@@ -447,6 +450,7 @@ private void loadResources(@NonNull Resources resources) {
447450

448451
defaultTickActiveRadius = resources.getDimensionPixelSize(R.dimen.mtrl_slider_tick_radius);
449452
defaultTickInactiveRadius = resources.getDimensionPixelSize(R.dimen.mtrl_slider_tick_radius);
453+
minTickSpacing = resources.getDimensionPixelSize(R.dimen.mtrl_slider_tick_min_spacing);
450454

451455
labelPadding = resources.getDimensionPixelSize(R.dimen.mtrl_slider_label_padding);
452456
}
@@ -1946,7 +1950,7 @@ private void maybeCalculateTicksCoordinates() {
19461950

19471951
int tickCount = (int) ((valueTo - valueFrom) / stepSize + 1);
19481952
// Limit the tickCount if they will be too dense.
1949-
tickCount = min(tickCount, trackWidth / (trackHeight * 2) + 1);
1953+
tickCount = min(tickCount, trackWidth / minTickSpacing + 1);
19501954
if (ticksCoordinates == null || ticksCoordinates.length != tickCount * 2) {
19511955
ticksCoordinates = new float[tickCount * 2];
19521956
}
@@ -2046,9 +2050,9 @@ private void drawInactiveTrack(@NonNull Canvas canvas, int width, int yCenter) {
20462050
if (hasGapBetweenThumbAndTrack()) {
20472051
trackRect.set(
20482052
right + thumbTrackGapSize,
2049-
yCenter - trackHeight / 2,
2050-
trackSidePadding + width + trackHeight / 2,
2051-
yCenter + trackHeight / 2);
2053+
yCenter - trackHeight / 2f,
2054+
trackSidePadding + width + trackHeight / 2f,
2055+
yCenter + trackHeight / 2f);
20522056
updateTrack(canvas, inactiveTrackPaint, trackRect, FullCornerDirection.RIGHT);
20532057
} else {
20542058
inactiveTrackPaint.setStyle(Style.STROKE);
@@ -2062,10 +2066,10 @@ private void drawInactiveTrack(@NonNull Canvas canvas, int width, int yCenter) {
20622066
if (left > trackSidePadding + thumbTrackGapSize) {
20632067
if (hasGapBetweenThumbAndTrack()) {
20642068
trackRect.set(
2065-
trackSidePadding - trackHeight / 2,
2066-
yCenter - trackHeight / 2,
2069+
trackSidePadding - trackHeight / 2f,
2070+
yCenter - trackHeight / 2f,
20672071
left - thumbTrackGapSize,
2068-
yCenter + trackHeight / 2);
2072+
yCenter + trackHeight / 2f);
20692073
updateTrack(canvas, inactiveTrackPaint, trackRect, FullCornerDirection.LEFT);
20702074
} else {
20712075
inactiveTrackPaint.setStyle(Style.STROKE);
@@ -2093,38 +2097,53 @@ private void drawActiveTrack(@NonNull Canvas canvas, int width, int yCenter) {
20932097
float left = trackSidePadding + activeRange[0] * width;
20942098

20952099
if (hasGapBetweenThumbAndTrack()) {
2096-
FullCornerDirection direction =
2097-
isRtl() ? FullCornerDirection.RIGHT : FullCornerDirection.LEFT;
2098-
if (values.size() > 1) { // Active track is in-between thumbs
2099-
direction = FullCornerDirection.NONE;
2100-
}
2101-
float fullCornerSize = trackHeight / 2f;
2102-
switch (direction) {
2103-
case BOTH:
2104-
left -= fullCornerSize;
2105-
right += fullCornerSize;
2106-
break;
2107-
case LEFT:
2108-
left -= fullCornerSize;
2109-
right -= thumbTrackGapSize;
2110-
break;
2111-
case RIGHT:
2112-
left += thumbTrackGapSize;
2113-
right += fullCornerSize;
2114-
break;
2115-
case NONE:
2116-
left += thumbTrackGapSize;
2117-
right -= thumbTrackGapSize;
2118-
break;
2100+
FullCornerDirection direction = FullCornerDirection.NONE;
2101+
if (values.size() == 1) { // Only 1 thumb
2102+
direction = isRtl() ? FullCornerDirection.RIGHT : FullCornerDirection.LEFT;
21192103
}
21202104

2121-
// Active track is too small to be drawn
2122-
if (left + thumbTrackGapSize >= right) {
2123-
return;
2124-
}
2105+
for (int i = 0; i < values.size(); i++) {
2106+
if (values.size() > 1) {
2107+
if (i > 0) {
2108+
left = valueToX(values.get(i - 1));
2109+
}
2110+
right = valueToX(values.get(i));
2111+
if (isRtl()) { // Swap left right
2112+
float temp = left;
2113+
left = right;
2114+
right = temp;
2115+
}
2116+
}
21252117

2126-
trackRect.set(left, yCenter - trackHeight / 2, right, yCenter + trackHeight / 2);
2127-
updateTrack(canvas, activeTrackPaint, trackRect, direction);
2118+
float threshold = 0;
2119+
switch (direction) {
2120+
case NONE:
2121+
left += thumbTrackGapSize;
2122+
right -= thumbTrackGapSize;
2123+
threshold = trackInsideCornerSize * 2;
2124+
break;
2125+
case LEFT:
2126+
left -= trackHeight / 2f;
2127+
right -= thumbTrackGapSize;
2128+
threshold = trackInsideCornerSize + trackHeight / 2f + thumbTrackGapSize;
2129+
break;
2130+
case RIGHT:
2131+
left += thumbTrackGapSize;
2132+
right += trackHeight / 2f;
2133+
threshold = trackInsideCornerSize + trackHeight / 2f + thumbTrackGapSize;
2134+
break;
2135+
default:
2136+
// fall through
2137+
}
2138+
2139+
// Active track is too small to be drawn
2140+
if (right - left <= threshold) {
2141+
continue;
2142+
}
2143+
2144+
trackRect.set(left, yCenter - trackHeight / 2f, right, yCenter + trackHeight / 2f);
2145+
updateTrack(canvas, activeTrackPaint, trackRect, direction);
2146+
}
21282147
} else {
21292148
activeTrackPaint.setStyle(Style.STROKE);
21302149
activeTrackPaint.setStrokeCap(Cap.ROUND);
@@ -2383,7 +2402,7 @@ && abs(lastEvent.getY() - event.getY()) <= scaledTouchSlop) {
23832402
* @return Index of the closest tick coordinate.
23842403
*/
23852404
private static int pivotIndex(float[] coordinates, float position) {
2386-
return Math.round(position * (coordinates.length / 2 - 1));
2405+
return Math.round(position * (coordinates.length / 2f - 1));
23872406
}
23882407

23892408
private double snapPosition(float position) {

lib/java/com/google/android/material/slider/res/values/dimens.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<dimen name="mtrl_slider_track_height">4dp</dimen>
2323

2424
<dimen name="mtrl_slider_tick_radius">1dp</dimen>
25+
<dimen name="mtrl_slider_tick_min_spacing">8dp</dimen>
2526

2627
<dimen name="mtrl_slider_thumb_radius">10dp</dimen>
2728
<dimen name="mtrl_slider_thumb_elevation">1dp</dimen>

0 commit comments

Comments
 (0)