25
25
import static com .google .android .material .slider .LabelFormatter .LABEL_GONE ;
26
26
import static com .google .android .material .slider .LabelFormatter .LABEL_VISIBLE ;
27
27
import static com .google .android .material .slider .LabelFormatter .LABEL_WITHIN_BOUNDS ;
28
+ import static com .google .android .material .slider .TickVisibilityMode .TICK_VISIBILITY_AUTO_HIDE ;
29
+ import static com .google .android .material .slider .TickVisibilityMode .TICK_VISIBILITY_AUTO_LIMIT ;
30
+ import static com .google .android .material .slider .TickVisibilityMode .TICK_VISIBILITY_HIDDEN ;
31
+ import static com .google .android .material .slider .TickVisibilityMode .TICK_VISIBILITY_VISIBLE_ALL ;
28
32
import static com .google .android .material .theme .overlay .MaterialThemeOverlay .wrap ;
29
33
import static java .lang .Float .compare ;
30
34
import static java .lang .Math .abs ;
151
155
* discrete mode. This is a short hand for setting both the {@code tickColorActive} and {@code
152
156
* tickColorInactive} to the same thing. This takes precedence over {@code tickColorActive}
153
157
* and {@code tickColorInactive}.
154
- * <li>{@code tickVisible}: Whether to show the tick marks. Only used when the slider is in
155
- * discrete mode.
158
+ * <li>{@code tickVisible} (deprecated, use {@code tickVisibilityMode} instead): Whether to show
159
+ * the tick marks. Only used when the slider is in discrete mode.
160
+ * <li>{@code tickVisibilityMode}: Mode to specify the visibility of tick marks. Only used when
161
+ * the slider is in discrete mode.
156
162
* <li>{@code trackColorActive}: The color of the active part of the track.
157
163
* <li>{@code trackColorInactive}: The color of the inactive part of the track.
158
164
* <li>{@code trackColor}: The color of the whole track. This is a short hand for setting both the
202
208
* @attr ref com.google.android.material.R.styleable#Slider_tickColorActive
203
209
* @attr ref com.google.android.material.R.styleable#Slider_tickColorInactive
204
210
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
211
+ * @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
205
212
* @attr ref com.google.android.material.R.styleable#Slider_trackColor
206
213
* @attr ref com.google.android.material.R.styleable#Slider_trackColorActive
207
214
* @attr ref com.google.android.material.R.styleable#Slider_trackColorInactive
@@ -322,7 +329,7 @@ abstract class BaseSlider<
322
329
private int focusedThumbIdx = -1 ;
323
330
private float stepSize = 0.0f ;
324
331
private float [] ticksCoordinates ;
325
- private boolean tickVisible = true ;
332
+ private int tickVisibilityMode ;
326
333
private int tickActiveRadius ;
327
334
private int tickInactiveRadius ;
328
335
private int trackWidth ;
@@ -501,7 +508,10 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
501
508
? haloColor
502
509
: AppCompatResources .getColorStateList (context , R .color .material_slider_halo_color ));
503
510
504
- tickVisible = a .getBoolean (R .styleable .Slider_tickVisible , true );
511
+ tickVisibilityMode = a .hasValue (R .styleable .Slider_tickVisibilityMode )
512
+ ? a .getInt (R .styleable .Slider_tickVisibilityMode , -1 )
513
+ : convertToTickVisibilityMode (a .getBoolean (R .styleable .Slider_tickVisible , true ));
514
+
505
515
boolean hasTickColor = a .hasValue (R .styleable .Slider_tickColor );
506
516
int tickColorInactiveRes =
507
517
hasTickColor ? R .styleable .Slider_tickColor : R .styleable .Slider_tickColorInactive ;
@@ -1668,22 +1678,61 @@ public void setTickInactiveTintList(@NonNull ColorStateList tickColor) {
1668
1678
/**
1669
1679
* Returns whether the tick marks are visible. Only used when the slider is in discrete mode.
1670
1680
*
1671
- * @see #setTickVisible(boolean)
1672
1681
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
1673
1682
*/
1674
1683
public boolean isTickVisible () {
1675
- return tickVisible ;
1684
+ switch (tickVisibilityMode ) {
1685
+ case TICK_VISIBILITY_VISIBLE_ALL :
1686
+ case TICK_VISIBILITY_AUTO_LIMIT :
1687
+ return true ;
1688
+ case TICK_VISIBILITY_AUTO_HIDE :
1689
+ return getDesiredTickCount () <= getMaxTickCount ();
1690
+ case TICK_VISIBILITY_HIDDEN :
1691
+ return false ;
1692
+ default :
1693
+ throw new RuntimeException ("Unexpected tickVisibilityMode: " + tickVisibilityMode );
1694
+ }
1676
1695
}
1677
1696
1678
1697
/**
1679
1698
* Sets whether the tick marks are visible. Only used when the slider is in discrete mode.
1680
1699
*
1681
1700
* @param tickVisible The visibility of tick marks.
1682
1701
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
1702
+ * @deprecated Use {@link #setTickVisibilityMode(int)} instead.
1683
1703
*/
1704
+ @ Deprecated
1684
1705
public void setTickVisible (boolean tickVisible ) {
1685
- if (this .tickVisible != tickVisible ) {
1686
- this .tickVisible = tickVisible ;
1706
+ setTickVisibilityMode (convertToTickVisibilityMode (tickVisible ));
1707
+ }
1708
+
1709
+ @ TickVisibilityMode
1710
+ private int convertToTickVisibilityMode (boolean tickVisible ) {
1711
+ return tickVisible
1712
+ ? TICK_VISIBILITY_AUTO_LIMIT
1713
+ : TICK_VISIBILITY_HIDDEN ;
1714
+ }
1715
+
1716
+ /**
1717
+ * Returns the current tick visibility mode.
1718
+ *
1719
+ * @see #setTickVisibilityMode(int)
1720
+ * @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
1721
+ */
1722
+ @ TickVisibilityMode
1723
+ public int getTickVisibilityMode () {
1724
+ return tickVisibilityMode ;
1725
+ }
1726
+
1727
+ /**
1728
+ * Sets the tick visibility mode. Only used when the slider is in discrete mode.
1729
+ *
1730
+ * @see #getTickVisibilityMode()
1731
+ * @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
1732
+ */
1733
+ public void setTickVisibilityMode (@ TickVisibilityMode int tickVisibilityMode ) {
1734
+ if (this .tickVisibilityMode != tickVisibilityMode ) {
1735
+ this .tickVisibilityMode = tickVisibilityMode ;
1687
1736
postInvalidate ();
1688
1737
}
1689
1738
}
@@ -1933,24 +1982,60 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1933
1982
updateHaloHotspot ();
1934
1983
}
1935
1984
1936
- private void maybeCalculateTicksCoordinates () {
1985
+ private void updateTicksCoordinates () {
1986
+ validateConfigurationIfDirty ();
1987
+
1937
1988
if (stepSize <= 0.0f ) {
1989
+ updateTicksCoordinates (/* tickCount= */ 0 );
1938
1990
return ;
1939
1991
}
1940
1992
1941
- validateConfigurationIfDirty ();
1993
+ final int tickCount ;
1994
+ switch (tickVisibilityMode ) {
1995
+ case TICK_VISIBILITY_VISIBLE_ALL :
1996
+ tickCount = getDesiredTickCount ();
1997
+ break ;
1998
+ case TICK_VISIBILITY_AUTO_LIMIT :
1999
+ tickCount = min (getDesiredTickCount (), getMaxTickCount ());
2000
+ break ;
2001
+ case TICK_VISIBILITY_AUTO_HIDE :
2002
+ int desiredTickCount = getDesiredTickCount ();
2003
+ tickCount = desiredTickCount <= getMaxTickCount () ? desiredTickCount : 0 ;
2004
+ break ;
2005
+ case TICK_VISIBILITY_HIDDEN :
2006
+ tickCount = 0 ;
2007
+ break ;
2008
+ default :
2009
+ throw new RuntimeException ("Unexpected tickVisibilityMode: " + tickVisibilityMode );
2010
+ }
2011
+
2012
+ updateTicksCoordinates (tickCount );
2013
+ }
2014
+
2015
+ private int getDesiredTickCount () {
2016
+ return (int ) ((valueTo - valueFrom ) / stepSize + 1 );
2017
+ }
2018
+
2019
+ private int getMaxTickCount () {
2020
+ return trackWidth / minTickSpacing + 1 ;
2021
+ }
2022
+
2023
+ private void updateTicksCoordinates (int tickCount ) {
2024
+ if (tickCount == 0 ) {
2025
+ ticksCoordinates = null ;
2026
+ return ;
2027
+ }
1942
2028
1943
- int tickCount = (int ) ((valueTo - valueFrom ) / stepSize + 1 );
1944
- // Limit the tickCount if they will be too dense.
1945
- tickCount = min (tickCount , trackWidth / minTickSpacing + 1 );
1946
2029
if (ticksCoordinates == null || ticksCoordinates .length != tickCount * 2 ) {
1947
2030
ticksCoordinates = new float [tickCount * 2 ];
1948
2031
}
1949
2032
1950
2033
float interval = trackWidth / (float ) (tickCount - 1 );
2034
+ float trackCenterY = calculateTrackCenter ();
2035
+
1951
2036
for (int i = 0 ; i < tickCount * 2 ; i += 2 ) {
1952
2037
ticksCoordinates [i ] = trackSidePadding + i / 2f * interval ;
1953
- ticksCoordinates [i + 1 ] = calculateTrackCenter () ;
2038
+ ticksCoordinates [i + 1 ] = trackCenterY ;
1954
2039
}
1955
2040
}
1956
2041
@@ -1959,7 +2044,7 @@ private void updateTrackWidth(int width) {
1959
2044
trackWidth = max (width - trackSidePadding * 2 , 0 );
1960
2045
1961
2046
// Update the visible tick coordinates.
1962
- maybeCalculateTicksCoordinates ();
2047
+ updateTicksCoordinates ();
1963
2048
}
1964
2049
1965
2050
private void updateHaloHotspot () {
@@ -1988,7 +2073,7 @@ protected void onDraw(@NonNull Canvas canvas) {
1988
2073
validateConfigurationIfDirty ();
1989
2074
1990
2075
// Update the visible tick coordinates.
1991
- maybeCalculateTicksCoordinates ();
2076
+ updateTicksCoordinates ();
1992
2077
}
1993
2078
1994
2079
super .onDraw (canvas );
@@ -2213,7 +2298,7 @@ private float[] getCornerRadii(float leftSide, float rightSide) {
2213
2298
}
2214
2299
2215
2300
private void maybeDrawTicks (@ NonNull Canvas canvas ) {
2216
- if (! tickVisible || stepSize <= 0.0f ) {
2301
+ if (ticksCoordinates == null || ticksCoordinates . length == 0 ) {
2217
2302
return ;
2218
2303
}
2219
2304
0 commit comments