29
29
import static com .google .android .material .slider .LabelFormatter .LABEL_WITHIN_BOUNDS ;
30
30
import static com .google .android .material .slider .SliderOrientation .HORIZONTAL ;
31
31
import static com .google .android .material .slider .SliderOrientation .VERTICAL ;
32
+ import static com .google .android .material .slider .TickVisibilityMode .TICK_VISIBILITY_AUTO_HIDE ;
33
+ import static com .google .android .material .slider .TickVisibilityMode .TICK_VISIBILITY_AUTO_LIMIT ;
34
+ import static com .google .android .material .slider .TickVisibilityMode .TICK_VISIBILITY_HIDDEN ;
32
35
import static com .google .android .material .theme .overlay .MaterialThemeOverlay .wrap ;
33
36
import static java .lang .Float .compare ;
34
37
import static java .lang .Math .abs ;
157
160
* discrete mode. This is a short hand for setting both the {@code tickColorActive} and {@code
158
161
* tickColorInactive} to the same thing. This takes precedence over {@code tickColorActive}
159
162
* and {@code tickColorInactive}.
160
- * <li>{@code tickVisible}: Whether to show the tick marks. Only used when the slider is in
161
- * discrete mode.
163
+ * <li>{@code tickVisible} (<b>deprecated</b>, use {@code tickVisibilityMode} instead):
164
+ * Whether to show the tick marks. Only used when the slider is in discrete mode.
165
+ * <li>{@code tickVisibilityMode}: Mode to specify the visibility of tick marks. Only used when
166
+ * the slider is in discrete mode.
162
167
* <li>{@code trackColorActive}: The color of the active part of the track.
163
168
* <li>{@code trackColorInactive}: The color of the inactive part of the track.
164
169
* <li>{@code trackColor}: The color of the whole track. This is a short hand for setting both the
210
215
* @attr ref com.google.android.material.R.styleable#Slider_tickColorActive
211
216
* @attr ref com.google.android.material.R.styleable#Slider_tickColorInactive
212
217
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
218
+ * @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
213
219
* @attr ref com.google.android.material.R.styleable#Slider_trackColor
214
220
* @attr ref com.google.android.material.R.styleable#Slider_trackColorActive
215
221
* @attr ref com.google.android.material.R.styleable#Slider_trackColorInactive
@@ -352,7 +358,7 @@ abstract class BaseSlider<
352
358
private int focusedThumbIdx = -1 ;
353
359
private float stepSize = 0.0f ;
354
360
private float [] ticksCoordinates ;
355
- private boolean tickVisible = true ;
361
+ private int tickVisibilityMode ;
356
362
private int tickActiveRadius ;
357
363
private int tickInactiveRadius ;
358
364
private int trackWidth ;
@@ -566,7 +572,10 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
566
572
? haloColor
567
573
: AppCompatResources .getColorStateList (context , R .color .material_slider_halo_color ));
568
574
569
- tickVisible = a .getBoolean (R .styleable .Slider_tickVisible , true );
575
+ tickVisibilityMode = a .hasValue (R .styleable .Slider_tickVisibilityMode )
576
+ ? a .getInt (R .styleable .Slider_tickVisibilityMode , -1 )
577
+ : convertToTickVisibilityMode (a .getBoolean (R .styleable .Slider_tickVisible , true ));
578
+
570
579
boolean hasTickColor = a .hasValue (R .styleable .Slider_tickColor );
571
580
int tickColorInactiveRes =
572
581
hasTickColor ? R .styleable .Slider_tickColor : R .styleable .Slider_tickColorInactive ;
@@ -1768,22 +1777,60 @@ public void setTickInactiveTintList(@NonNull ColorStateList tickColor) {
1768
1777
/**
1769
1778
* Returns whether the tick marks are visible. Only used when the slider is in discrete mode.
1770
1779
*
1771
- * @see #setTickVisible(boolean)
1772
1780
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
1773
1781
*/
1774
1782
public boolean isTickVisible () {
1775
- return tickVisible ;
1783
+ switch (tickVisibilityMode ) {
1784
+ case TICK_VISIBILITY_AUTO_LIMIT :
1785
+ return true ;
1786
+ case TICK_VISIBILITY_AUTO_HIDE :
1787
+ return getDesiredTickCount () <= getMaxTickCount ();
1788
+ case TICK_VISIBILITY_HIDDEN :
1789
+ return false ;
1790
+ default :
1791
+ throw new RuntimeException ("Unexpected tickVisibilityMode: " + tickVisibilityMode );
1792
+ }
1776
1793
}
1777
1794
1778
1795
/**
1779
1796
* Sets whether the tick marks are visible. Only used when the slider is in discrete mode.
1780
1797
*
1781
1798
* @param tickVisible The visibility of tick marks.
1782
1799
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
1800
+ * @deprecated Use {@link #setTickVisibilityMode(int)} instead.
1783
1801
*/
1802
+ @ Deprecated
1784
1803
public void setTickVisible (boolean tickVisible ) {
1785
- if (this .tickVisible != tickVisible ) {
1786
- this .tickVisible = tickVisible ;
1804
+ setTickVisibilityMode (convertToTickVisibilityMode (tickVisible ));
1805
+ }
1806
+
1807
+ @ TickVisibilityMode
1808
+ private int convertToTickVisibilityMode (boolean tickVisible ) {
1809
+ return tickVisible
1810
+ ? TICK_VISIBILITY_AUTO_LIMIT
1811
+ : TICK_VISIBILITY_HIDDEN ;
1812
+ }
1813
+
1814
+ /**
1815
+ * Returns the current tick visibility mode.
1816
+ *
1817
+ * @see #setTickVisibilityMode(int)
1818
+ * @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
1819
+ */
1820
+ @ TickVisibilityMode
1821
+ public int getTickVisibilityMode () {
1822
+ return tickVisibilityMode ;
1823
+ }
1824
+
1825
+ /**
1826
+ * Sets the tick visibility mode. Only used when the slider is in discrete mode.
1827
+ *
1828
+ * @see #getTickVisibilityMode()
1829
+ * @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
1830
+ */
1831
+ public void setTickVisibilityMode (@ TickVisibilityMode int tickVisibilityMode ) {
1832
+ if (this .tickVisibilityMode != tickVisibilityMode ) {
1833
+ this .tickVisibilityMode = tickVisibilityMode ;
1787
1834
postInvalidate ();
1788
1835
}
1789
1836
}
@@ -2337,24 +2384,57 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
2337
2384
updateHaloHotspot ();
2338
2385
}
2339
2386
2340
- private void maybeCalculateTicksCoordinates () {
2387
+ private void updateTicksCoordinates () {
2388
+ validateConfigurationIfDirty ();
2389
+
2341
2390
if (stepSize <= 0.0f ) {
2391
+ updateTicksCoordinates (/* tickCount= */ 0 );
2342
2392
return ;
2343
2393
}
2344
2394
2345
- validateConfigurationIfDirty ();
2395
+ final int tickCount ;
2396
+ switch (tickVisibilityMode ) {
2397
+ case TICK_VISIBILITY_AUTO_LIMIT :
2398
+ tickCount = min (getDesiredTickCount (), getMaxTickCount ());
2399
+ break ;
2400
+ case TICK_VISIBILITY_AUTO_HIDE :
2401
+ int desiredTickCount = getDesiredTickCount ();
2402
+ tickCount = desiredTickCount <= getMaxTickCount () ? desiredTickCount : 0 ;
2403
+ break ;
2404
+ case TICK_VISIBILITY_HIDDEN :
2405
+ tickCount = 0 ;
2406
+ break ;
2407
+ default :
2408
+ throw new RuntimeException ("Unexpected tickVisibilityMode: " + tickVisibilityMode );
2409
+ }
2410
+
2411
+ updateTicksCoordinates (tickCount );
2412
+ }
2413
+
2414
+ private int getDesiredTickCount () {
2415
+ return (int ) ((valueTo - valueFrom ) / stepSize + 1 );
2416
+ }
2417
+
2418
+ private int getMaxTickCount () {
2419
+ return trackWidth / minTickSpacing + 1 ;
2420
+ }
2421
+
2422
+ private void updateTicksCoordinates (int tickCount ) {
2423
+ if (tickCount == 0 ) {
2424
+ ticksCoordinates = null ;
2425
+ return ;
2426
+ }
2346
2427
2347
- int tickCount = (int ) ((valueTo - valueFrom ) / stepSize + 1 );
2348
- // Limit the tickCount if they will be too dense.
2349
- tickCount = min (tickCount , trackWidth / minTickSpacing + 1 );
2350
2428
if (ticksCoordinates == null || ticksCoordinates .length != tickCount * 2 ) {
2351
2429
ticksCoordinates = new float [tickCount * 2 ];
2352
2430
}
2353
2431
2354
2432
float interval = trackWidth / (float ) (tickCount - 1 );
2433
+ float trackCenterY = calculateTrackCenter ();
2434
+
2355
2435
for (int i = 0 ; i < tickCount * 2 ; i += 2 ) {
2356
2436
ticksCoordinates [i ] = trackSidePadding + i / 2f * interval ;
2357
- ticksCoordinates [i + 1 ] = calculateTrackCenter () ;
2437
+ ticksCoordinates [i + 1 ] = trackCenterY ;
2358
2438
}
2359
2439
2360
2440
if (isVertical ()) {
@@ -2367,7 +2447,7 @@ private void updateTrackWidth(int width) {
2367
2447
trackWidth = max (width - trackSidePadding * 2 , 0 );
2368
2448
2369
2449
// Update the visible tick coordinates.
2370
- maybeCalculateTicksCoordinates ();
2450
+ updateTicksCoordinates ();
2371
2451
}
2372
2452
2373
2453
private void updateHaloHotspot () {
@@ -2404,7 +2484,7 @@ protected void onDraw(@NonNull Canvas canvas) {
2404
2484
validateConfigurationIfDirty ();
2405
2485
2406
2486
// Update the visible tick coordinates.
2407
- maybeCalculateTicksCoordinates ();
2487
+ updateTicksCoordinates ();
2408
2488
}
2409
2489
2410
2490
super .onDraw (canvas );
@@ -2703,7 +2783,7 @@ private float[] getCornerRadii(float leftSide, float rightSide) {
2703
2783
}
2704
2784
2705
2785
private void maybeDrawTicks (@ NonNull Canvas canvas ) {
2706
- if (! tickVisible || stepSize <= 0.0f ) {
2786
+ if (ticksCoordinates == null || ticksCoordinates . length == 0 ) {
2707
2787
return ;
2708
2788
}
2709
2789
0 commit comments