Skip to content

Commit 143cf5b

Browse files
committed
patch 1
1 parent 4a0b05d commit 143cf5b

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

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

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import android.os.Parcel;
6161
import android.os.Parcelable;
6262

63-
import androidx.annotation.RestrictTo;
6463
import androidx.appcompat.content.res.AppCompatResources;
6564
import android.util.AttributeSet;
6665
import android.util.Log;
@@ -297,8 +296,8 @@ private interface TooltipDrawableFactory {
297296
// The index of the currently focused thumb.
298297
private int focusedThumbIdx = -1;
299298
private float stepSize = 0.0f;
299+
private boolean areTicksDrawn = false;
300300
private float[] ticksCoordinates;
301-
private boolean tickVisible = false;
302301
private int tickVisibilityMode;
303302
private int trackWidth;
304303
private boolean forceDrawCompatHalo;
@@ -479,9 +478,8 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
479478
? haloColor
480479
: AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color));
481480

482-
int tickVisibilityMode = a.getInt(R.styleable.Slider_tickVisibilityMode, -1);
483-
if (tickVisibilityMode != -1) {
484-
this.tickVisibilityMode = tickVisibilityMode;
481+
if (a.hasValue(R.styleable.Slider_tickVisibilityMode)) {
482+
this.tickVisibilityMode = a.getInt(R.styleable.Slider_tickVisibilityMode, -1);
485483
} else {
486484
this.tickVisibilityMode = a.getBoolean(R.styleable.Slider_tickVisible, true)
487485
? TICK_VISIBILITY_AUTO_LIMIT
@@ -1489,10 +1487,10 @@ public void setTickInactiveTintList(@NonNull ColorStateList tickColor) {
14891487
}
14901488

14911489
/**
1492-
* Returns whether the tick marks are visible.
1490+
* Returns whether the tick marks are visible (drawn).
14931491
*/
14941492
public boolean isTickVisible() {
1495-
return tickVisible;
1493+
return areTicksDrawn;
14961494
}
14971495

14981496
/**
@@ -1702,28 +1700,39 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
17021700
}
17031701

17041702
private void updateTicksCoordinates() {
1705-
if (stepSize <= 0.0f || tickVisibilityMode == TICK_VISIBILITY_HIDDEN) {
1706-
updateTicksCoordinates(0);
1707-
return;
1708-
}
1709-
1710-
final int tickCount = (int) ((valueTo - valueFrom) / stepSize + 1);
1711-
if (tickVisibilityMode == TICK_VISIBILITY_VISIBLE_ALL) {
1712-
updateTicksCoordinates(tickCount);
1703+
if (stepSize <= 0.0f) {
1704+
updateTicksCoordinates(/* tickCount= */ 0);
17131705
return;
17141706
}
17151707

1716-
final int maxTickCount = trackWidth / (trackHeight * 2) + 1;
1708+
final int tickCount;
17171709
switch (tickVisibilityMode) {
1710+
case TICK_VISIBILITY_VISIBLE_ALL:
1711+
tickCount = getDesiredTickCount();
1712+
break;
17181713
case TICK_VISIBILITY_AUTO_LIMIT:
1719-
updateTicksCoordinates(min(tickCount, maxTickCount));
1720-
return;
1714+
tickCount = min(getDesiredTickCount(), getMaxTickCount());
1715+
break;
17211716
case TICK_VISIBILITY_AUTO_HIDE:
1722-
updateTicksCoordinates(tickCount <= maxTickCount ? tickCount : 0);
1723-
return;
1717+
int desiredTickCount = getDesiredTickCount();
1718+
tickCount = desiredTickCount <= getMaxTickCount() ? desiredTickCount : 0;
1719+
break;
1720+
case TICK_VISIBILITY_HIDDEN:
1721+
tickCount = 0;
1722+
break;
17241723
default:
17251724
throw new IllegalArgumentException("Invalid tick visibility mode: " + tickVisibilityMode);
17261725
}
1726+
1727+
updateTicksCoordinates(tickCount);
1728+
}
1729+
1730+
private int getDesiredTickCount() {
1731+
return (int) ((valueTo - valueFrom) / stepSize + 1);
1732+
}
1733+
1734+
private int getMaxTickCount() {
1735+
return trackWidth / (trackHeight * 2) + 1;
17271736
}
17281737

17291738
private void updateTicksCoordinates(int tickCount) {
@@ -1791,7 +1800,7 @@ protected void onDraw(@NonNull Canvas canvas) {
17911800
drawActiveTrack(canvas, trackWidth, yCenter);
17921801
}
17931802

1794-
tickVisible = maybeDrawTicks(canvas);
1803+
areTicksDrawn = maybeDrawTicks(canvas);
17951804

17961805
if ((thumbIsPressed || isFocused() || shouldAlwaysShowLabel()) && isEnabled()) {
17971806
maybeDrawHalo(canvas, trackWidth, yCenter);

0 commit comments

Comments
 (0)