24
24
import static com .google .android .material .slider .LabelFormatter .LABEL_GONE ;
25
25
import static com .google .android .material .slider .LabelFormatter .LABEL_VISIBLE ;
26
26
import static com .google .android .material .slider .LabelFormatter .LABEL_WITHIN_BOUNDS ;
27
+ import static com .google .android .material .slider .TickVisibilityMode .TICK_VISIBILITY_AUTO_HIDE ;
28
+ import static com .google .android .material .slider .TickVisibilityMode .TICK_VISIBILITY_AUTO_LIMIT ;
29
+ import static com .google .android .material .slider .TickVisibilityMode .TICK_VISIBILITY_HIDDEN ;
30
+ import static com .google .android .material .slider .TickVisibilityMode .TICK_VISIBILITY_VISIBLE_ALL ;
27
31
import static com .google .android .material .theme .overlay .MaterialThemeOverlay .wrap ;
28
32
import static java .lang .Float .compare ;
29
33
import static java .lang .Math .abs ;
55
59
import android .os .Bundle ;
56
60
import android .os .Parcel ;
57
61
import android .os .Parcelable ;
62
+
63
+ import androidx .annotation .RestrictTo ;
58
64
import androidx .appcompat .content .res .AppCompatResources ;
59
65
import android .util .AttributeSet ;
60
66
import android .util .Log ;
@@ -292,7 +298,8 @@ private interface TooltipDrawableFactory {
292
298
private int focusedThumbIdx = -1 ;
293
299
private float stepSize = 0.0f ;
294
300
private float [] ticksCoordinates ;
295
- private boolean tickVisible = true ;
301
+ private boolean tickVisible = false ;
302
+ private int tickVisibilityMode ;
296
303
private int trackWidth ;
297
304
private boolean forceDrawCompatHalo ;
298
305
private boolean isLongPress = false ;
@@ -472,7 +479,15 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
472
479
? haloColor
473
480
: AppCompatResources .getColorStateList (context , R .color .material_slider_halo_color ));
474
481
475
- tickVisible = a .getBoolean (R .styleable .Slider_tickVisible , true );
482
+ int tickVisibilityMode = a .getInt (R .styleable .Slider_tickVisibilityMode , -1 );
483
+ if (tickVisibilityMode != -1 ) {
484
+ this .tickVisibilityMode = tickVisibilityMode ;
485
+ } else {
486
+ this .tickVisibilityMode = a .getBoolean (R .styleable .Slider_tickVisible , true )
487
+ ? TICK_VISIBILITY_AUTO_LIMIT
488
+ : TICK_VISIBILITY_HIDDEN ;
489
+ }
490
+
476
491
boolean hasTickColor = a .hasValue (R .styleable .Slider_tickColor );
477
492
int tickColorInactiveRes =
478
493
hasTickColor ? R .styleable .Slider_tickColor : R .styleable .Slider_tickColorInactive ;
@@ -1474,10 +1489,7 @@ public void setTickInactiveTintList(@NonNull ColorStateList tickColor) {
1474
1489
}
1475
1490
1476
1491
/**
1477
- * Returns whether the tick marks are visible. Only used when the slider is in discrete mode.
1478
- *
1479
- * @see #setTickVisible(boolean)
1480
- * @attr ref com.google.android.material.R.styleable#Slider_tickVisible
1492
+ * Returns whether the tick marks are visible.
1481
1493
*/
1482
1494
public boolean isTickVisible () {
1483
1495
return tickVisible ;
@@ -1488,10 +1500,33 @@ public boolean isTickVisible() {
1488
1500
*
1489
1501
* @param tickVisible The visibility of tick marks.
1490
1502
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
1503
+ * @deprecated Use {@link BaseSlider#setTickVisibilityMode(int)} instead.
1491
1504
*/
1505
+ @ Deprecated
1492
1506
public void setTickVisible (boolean tickVisible ) {
1493
- if (this .tickVisible != tickVisible ) {
1494
- this .tickVisible = tickVisible ;
1507
+ setTickVisibilityMode (tickVisible ? TICK_VISIBILITY_AUTO_LIMIT : TICK_VISIBILITY_HIDDEN );
1508
+ }
1509
+
1510
+ /**
1511
+ * Returns the current tick visibility mode.
1512
+ *
1513
+ * @see #setTickVisibilityMode(int)
1514
+ * @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
1515
+ */
1516
+ @ TickVisibilityMode
1517
+ public int getTickVisibilityMode () {
1518
+ return tickVisibilityMode ;
1519
+ }
1520
+
1521
+ /**
1522
+ * Sets the tick visibility mode. Only used when the slider is in discrete mode.
1523
+ *
1524
+ * @see #getTickVisibilityMode()
1525
+ * @attr ref com.google.android.material.R.styleable#Slider_tickVisibilityMode
1526
+ */
1527
+ public void setTickVisibilityMode (@ TickVisibilityMode int tickVisibilityMode ) {
1528
+ if (this .tickVisibilityMode != tickVisibilityMode ) {
1529
+ this .tickVisibilityMode = tickVisibilityMode ;
1495
1530
postInvalidate ();
1496
1531
}
1497
1532
}
@@ -1666,24 +1701,47 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1666
1701
updateHaloHotspot ();
1667
1702
}
1668
1703
1669
- private void maybeCalculateTicksCoordinates () {
1670
- if (stepSize <= 0.0f ) {
1704
+ private void updateTicksCoordinates () {
1705
+ if (stepSize <= 0.0f || tickVisibilityMode == TICK_VISIBILITY_HIDDEN ) {
1706
+ updateTicksCoordinates (0 );
1671
1707
return ;
1672
1708
}
1673
1709
1674
- validateConfigurationIfDirty ();
1710
+ final int tickCount = (int ) ((valueTo - valueFrom ) / stepSize + 1 );
1711
+ if (tickVisibilityMode == TICK_VISIBILITY_VISIBLE_ALL ) {
1712
+ updateTicksCoordinates (tickCount );
1713
+ return ;
1714
+ }
1715
+
1716
+ final int maxTickCount = trackWidth / (trackHeight * 2 ) + 1 ;
1717
+ switch (tickVisibilityMode ) {
1718
+ case TICK_VISIBILITY_AUTO_LIMIT :
1719
+ updateTicksCoordinates (min (tickCount , maxTickCount ));
1720
+ return ;
1721
+ case TICK_VISIBILITY_AUTO_HIDE :
1722
+ updateTicksCoordinates (tickCount <= maxTickCount ? tickCount : 0 );
1723
+ return ;
1724
+ default :
1725
+ throw new IllegalArgumentException ("Invalid tick visibility mode: " + tickVisibilityMode );
1726
+ }
1727
+ }
1728
+
1729
+ private void updateTicksCoordinates (int tickCount ) {
1730
+ if (tickCount == 0 ) {
1731
+ ticksCoordinates = null ;
1732
+ return ;
1733
+ }
1675
1734
1676
- int tickCount = (int ) ((valueTo - valueFrom ) / stepSize + 1 );
1677
- // Limit the tickCount if they will be too dense.
1678
- tickCount = min (tickCount , trackWidth / (trackHeight * 2 ) + 1 );
1679
1735
if (ticksCoordinates == null || ticksCoordinates .length != tickCount * 2 ) {
1680
1736
ticksCoordinates = new float [tickCount * 2 ];
1681
1737
}
1682
1738
1683
1739
float interval = trackWidth / (float ) (tickCount - 1 );
1740
+ float trackCenterY = calculateTrackCenter ();
1741
+
1684
1742
for (int i = 0 ; i < tickCount * 2 ; i += 2 ) {
1685
1743
ticksCoordinates [i ] = trackSidePadding + i / 2 * interval ;
1686
- ticksCoordinates [i + 1 ] = calculateTrackCenter () ;
1744
+ ticksCoordinates [i + 1 ] = trackCenterY ;
1687
1745
}
1688
1746
}
1689
1747
@@ -1692,7 +1750,7 @@ private void updateTrackWidth(int width) {
1692
1750
trackWidth = max (width - trackSidePadding * 2 , 0 );
1693
1751
1694
1752
// Update the visible tick coordinates.
1695
- maybeCalculateTicksCoordinates ();
1753
+ updateTicksCoordinates ();
1696
1754
}
1697
1755
1698
1756
private void updateHaloHotspot () {
@@ -1721,7 +1779,7 @@ protected void onDraw(@NonNull Canvas canvas) {
1721
1779
validateConfigurationIfDirty ();
1722
1780
1723
1781
// Update the visible tick coordinates.
1724
- maybeCalculateTicksCoordinates ();
1782
+ updateTicksCoordinates ();
1725
1783
}
1726
1784
1727
1785
super .onDraw (canvas );
@@ -1733,7 +1791,7 @@ protected void onDraw(@NonNull Canvas canvas) {
1733
1791
drawActiveTrack (canvas , trackWidth , yCenter );
1734
1792
}
1735
1793
1736
- maybeDrawTicks (canvas );
1794
+ tickVisible = maybeDrawTicks (canvas );
1737
1795
1738
1796
if ((thumbIsPressed || isFocused () || shouldAlwaysShowLabel ()) && isEnabled ()) {
1739
1797
maybeDrawHalo (canvas , trackWidth , yCenter );
@@ -1797,9 +1855,9 @@ private void drawActiveTrack(@NonNull Canvas canvas, int width, int yCenter) {
1797
1855
canvas .drawLine (left , yCenter , right , yCenter , activeTrackPaint );
1798
1856
}
1799
1857
1800
- private void maybeDrawTicks (@ NonNull Canvas canvas ) {
1801
- if (! tickVisible || stepSize <= 0.0f ) {
1802
- return ;
1858
+ private boolean maybeDrawTicks (@ NonNull Canvas canvas ) {
1859
+ if (ticksCoordinates == null || ticksCoordinates . length == 0 ) {
1860
+ return false ;
1803
1861
}
1804
1862
1805
1863
float [] activeRange = getActiveRange ();
@@ -1822,6 +1880,8 @@ private void maybeDrawTicks(@NonNull Canvas canvas) {
1822
1880
rightPivotIndex * 2 ,
1823
1881
ticksCoordinates .length - rightPivotIndex * 2 ,
1824
1882
inactiveTicksPaint );
1883
+
1884
+ return true ;
1825
1885
}
1826
1886
1827
1887
private void drawThumbs (@ NonNull Canvas canvas , int width , int yCenter ) {
0 commit comments