Skip to content

Commit 781d344

Browse files
paulfthomasleticiarossi
authored andcommitted
[Slider] Centered Slider Expressive updates
- update the Centered Slider to show the track starting from the center PiperOrigin-RevId: 750301810
1 parent 5161e71 commit 781d344

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,9 +2589,7 @@ protected void onDraw(@NonNull Canvas canvas) {
25892589
int yCenter = calculateTrackCenter();
25902590

25912591
drawInactiveTracks(canvas, trackWidth, yCenter);
2592-
if (!isCentered()) {
2593-
drawActiveTracks(canvas, trackWidth, yCenter);
2594-
}
2592+
drawActiveTracks(canvas, trackWidth, yCenter);
25952593

25962594
if (isRtl() || isVertical()) {
25972595
drawTrackIcons(canvas, activeTrackRect, inactiveTrackLeftRect);
@@ -2621,14 +2619,16 @@ private float[] getActiveRange() {
26212619
float left = normalizeValue(values.size() == 1 ? valueFrom : min);
26222620
float right = normalizeValue(max);
26232621

2624-
// When centered, there is no active range, left == right in order to draw the inactive track on
2625-
// both sides of the thumb leaving space for it, covering the entirety of the track.
2622+
// When centered, the active range is bound by the center.
26262623
if (isCentered()) {
2627-
left = right;
2624+
left = min(.5f, right);
2625+
right = max(.5f, right);
26282626
}
26292627

2630-
// In RTL we draw things in reverse, so swap the left and right range values
2631-
return isRtl() || isVertical() ? new float[] {right, left} : new float[] {left, right};
2628+
// In RTL we draw things in reverse, so swap the left and right range values.
2629+
return !isCentered() && (isRtl() || isVertical())
2630+
? new float[] {right, left}
2631+
: new float[] {left, right};
26322632
}
26332633

26342634
private void drawInactiveTracks(@NonNull Canvas canvas, int width, int yCenter) {
@@ -2692,7 +2692,7 @@ private void drawActiveTracks(@NonNull Canvas canvas, int width, int yCenter) {
26922692
}
26932693

26942694
FullCornerDirection direction = FullCornerDirection.NONE;
2695-
if (values.size() == 1) { // Only 1 thumb
2695+
if (values.size() == 1 && !isCentered()) { // Only 1 thumb
26962696
direction = isRtl() || isVertical() ? FullCornerDirection.RIGHT : FullCornerDirection.LEFT;
26972697
}
26982698

@@ -2712,8 +2712,14 @@ private void drawActiveTracks(@NonNull Canvas canvas, int width, int yCenter) {
27122712
int trackCornerSize = getTrackCornerSize();
27132713
switch (direction) {
27142714
case NONE:
2715-
left += thumbTrackGapSize;
2716-
right -= thumbTrackGapSize;
2715+
if (!isCentered()) {
2716+
left += thumbTrackGapSize;
2717+
right -= thumbTrackGapSize;
2718+
} else if (activeRange[1] == .5f) { // centered, active track ends at the center
2719+
left += thumbTrackGapSize;
2720+
} else if (activeRange[0] == .5f) { // centered, active track starts at the center
2721+
right -= thumbTrackGapSize;
2722+
}
27172723
break;
27182724
case LEFT:
27192725
left -= trackCornerSize;
@@ -2955,11 +2961,7 @@ private void maybeDrawTicks(@NonNull Canvas canvas) {
29552961

29562962
// Draw ticks on the active track (if any).
29572963
if (leftActiveTickIndex <= rightActiveTickIndex) {
2958-
drawTicks(
2959-
leftActiveTickIndex * 2,
2960-
(rightActiveTickIndex + 1) * 2,
2961-
canvas,
2962-
isCentered() ? inactiveTicksPaint : activeTicksPaint); // centered uses inactive color.
2964+
drawTicks(leftActiveTickIndex * 2, (rightActiveTickIndex + 1) * 2, canvas, activeTicksPaint);
29632965
}
29642966

29652967
// Draw ticks on the right inactive track (if any).
@@ -2971,7 +2973,9 @@ private void maybeDrawTicks(@NonNull Canvas canvas) {
29712973

29722974
private void drawTicks(int from, int to, Canvas canvas, Paint paint) {
29732975
for (int i = from; i < to; i += 2) {
2974-
if (isOverlappingThumb(ticksCoordinates[i])) {
2976+
float coordinateToCheck = isVertical() ? ticksCoordinates[i + 1] : ticksCoordinates[i];
2977+
if (isOverlappingThumb(coordinateToCheck)
2978+
|| (isCentered() && isOverlappingCenterGap(coordinateToCheck))) {
29752979
continue;
29762980
}
29772981
canvas.drawPoint(ticksCoordinates[i], ticksCoordinates[i + 1], paint);
@@ -2987,6 +2991,12 @@ private boolean isOverlappingThumb(float tickCoordinate) {
29872991
return false;
29882992
}
29892993

2994+
private boolean isOverlappingCenterGap(float tickCoordinate) {
2995+
float threshold = thumbTrackGapSize + thumbWidth / 2f;
2996+
float trackCenter = (trackWidth + trackSidePadding * 2) / 2f;
2997+
return tickCoordinate >= trackCenter - threshold && tickCoordinate <= trackCenter + threshold;
2998+
}
2999+
29903000
private void maybeDrawStopIndicator(@NonNull Canvas canvas, int yCenter) {
29913001
if (trackStopIndicatorSize <= 0 || values.isEmpty()) {
29923002
return;
@@ -3000,10 +3010,6 @@ private void maybeDrawStopIndicator(@NonNull Canvas canvas, int yCenter) {
30003010
if (isCentered() || (values.size() > 1 && values.get(0) > valueFrom)) {
30013011
drawStopIndicator(canvas, valueToX(valueFrom), yCenter);
30023012
}
3003-
// Centered, draw indicator in the middle of the track.
3004-
if (isCentered()) {
3005-
drawStopIndicator(canvas, (valueToX(valueTo) + valueToX(valueFrom)) / 2f, yCenter);
3006-
}
30073013
}
30083014

30093015
private void drawStopIndicator(@NonNull Canvas canvas, float x, float y) {

0 commit comments

Comments
 (0)