|
59 | 59 | import android.os.Bundle;
|
60 | 60 | import android.os.Parcel;
|
61 | 61 | import android.os.Parcelable;
|
62 |
| - |
63 |
| -import androidx.annotation.RestrictTo; |
64 | 62 | import androidx.appcompat.content.res.AppCompatResources;
|
65 | 63 | import android.util.AttributeSet;
|
66 | 64 | import android.util.Log;
|
@@ -297,8 +295,8 @@ private interface TooltipDrawableFactory {
|
297 | 295 | // The index of the currently focused thumb.
|
298 | 296 | private int focusedThumbIdx = -1;
|
299 | 297 | private float stepSize = 0.0f;
|
| 298 | + private boolean areTicksDrawn = false; |
300 | 299 | private float[] ticksCoordinates;
|
301 |
| - private boolean tickVisible = false; |
302 | 300 | private int tickVisibilityMode;
|
303 | 301 | private int trackWidth;
|
304 | 302 | private boolean forceDrawCompatHalo;
|
@@ -479,9 +477,8 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
|
479 | 477 | ? haloColor
|
480 | 478 | : AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color));
|
481 | 479 |
|
482 |
| - int tickVisibilityMode = a.getInt(R.styleable.Slider_tickVisibilityMode, -1); |
483 |
| - if (tickVisibilityMode != -1) { |
484 |
| - this.tickVisibilityMode = tickVisibilityMode; |
| 480 | + if (a.hasValue(R.styleable.Slider_tickVisibilityMode)) { |
| 481 | + this.tickVisibilityMode = a.getInt(R.styleable.Slider_tickVisibilityMode, -1); |
485 | 482 | } else {
|
486 | 483 | this.tickVisibilityMode = a.getBoolean(R.styleable.Slider_tickVisible, true)
|
487 | 484 | ? TICK_VISIBILITY_AUTO_LIMIT
|
@@ -1489,10 +1486,30 @@ public void setTickInactiveTintList(@NonNull ColorStateList tickColor) {
|
1489 | 1486 | }
|
1490 | 1487 |
|
1491 | 1488 | /**
|
1492 |
| - * Returns whether the tick marks are visible. |
| 1489 | + * Returns whether the tick marks are visible. Only used when the slider is in discrete mode. |
| 1490 | + * |
| 1491 | + * @attr ref com.google.android.material.R.styleable#Slider_tickVisible |
| 1492 | + * @deprecated Use {@link BaseSlider#getTickVisibilityMode()} to get the current |
| 1493 | + * tick visibility mode or {@link BaseSlider#areTicksDrawn()} to get whether the ticks |
| 1494 | + * are currently drawn. |
1493 | 1495 | */
|
| 1496 | + @Deprecated |
1494 | 1497 | public boolean isTickVisible() {
|
1495 |
| - return tickVisible; |
| 1498 | + switch (tickVisibilityMode) { |
| 1499 | + case TICK_VISIBILITY_AUTO_LIMIT: |
| 1500 | + return true; |
| 1501 | + case TICK_VISIBILITY_HIDDEN: |
| 1502 | + return false; |
| 1503 | + default: |
| 1504 | + return areTicksDrawn(); |
| 1505 | + } |
| 1506 | + } |
| 1507 | + |
| 1508 | + /** |
| 1509 | + * Returns whether the tick marks are drawn. |
| 1510 | + */ |
| 1511 | + public boolean areTicksDrawn() { |
| 1512 | + return areTicksDrawn; |
1496 | 1513 | }
|
1497 | 1514 |
|
1498 | 1515 | /**
|
@@ -1702,28 +1719,39 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
1702 | 1719 | }
|
1703 | 1720 |
|
1704 | 1721 | private void updateTicksCoordinates() {
|
1705 |
| - if (stepSize <= 0.0f || tickVisibilityMode == TICK_VISIBILITY_HIDDEN) { |
1706 |
| - updateTicksCoordinates(0); |
| 1722 | + if (stepSize <= 0.0f) { |
| 1723 | + updateTicksCoordinates(/* tickCount= */ 0); |
1707 | 1724 | return;
|
1708 | 1725 | }
|
1709 | 1726 |
|
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; |
| 1727 | + final int tickCount; |
1717 | 1728 | switch (tickVisibilityMode) {
|
| 1729 | + case TICK_VISIBILITY_VISIBLE_ALL: |
| 1730 | + tickCount = getDesiredTickCount(); |
| 1731 | + break; |
1718 | 1732 | case TICK_VISIBILITY_AUTO_LIMIT:
|
1719 |
| - updateTicksCoordinates(min(tickCount, maxTickCount)); |
1720 |
| - return; |
| 1733 | + tickCount = min(getDesiredTickCount(), getMaxTickCount()); |
| 1734 | + break; |
1721 | 1735 | case TICK_VISIBILITY_AUTO_HIDE:
|
1722 |
| - updateTicksCoordinates(tickCount <= maxTickCount ? tickCount : 0); |
1723 |
| - return; |
| 1736 | + int desiredTickCount = getDesiredTickCount(); |
| 1737 | + tickCount = desiredTickCount <= getMaxTickCount() ? desiredTickCount : 0; |
| 1738 | + break; |
| 1739 | + case TICK_VISIBILITY_HIDDEN: |
| 1740 | + tickCount = 0; |
| 1741 | + break; |
1724 | 1742 | default:
|
1725 | 1743 | throw new IllegalArgumentException("Invalid tick visibility mode: " + tickVisibilityMode);
|
1726 | 1744 | }
|
| 1745 | + |
| 1746 | + updateTicksCoordinates(tickCount); |
| 1747 | + } |
| 1748 | + |
| 1749 | + private int getDesiredTickCount() { |
| 1750 | + return (int) ((valueTo - valueFrom) / stepSize + 1); |
| 1751 | + } |
| 1752 | + |
| 1753 | + private int getMaxTickCount() { |
| 1754 | + return trackWidth / (trackHeight * 2) + 1; |
1727 | 1755 | }
|
1728 | 1756 |
|
1729 | 1757 | private void updateTicksCoordinates(int tickCount) {
|
@@ -1791,7 +1819,7 @@ protected void onDraw(@NonNull Canvas canvas) {
|
1791 | 1819 | drawActiveTrack(canvas, trackWidth, yCenter);
|
1792 | 1820 | }
|
1793 | 1821 |
|
1794 |
| - tickVisible = maybeDrawTicks(canvas); |
| 1822 | + areTicksDrawn = maybeDrawTicks(canvas); |
1795 | 1823 |
|
1796 | 1824 | if ((thumbIsPressed || isFocused() || shouldAlwaysShowLabel()) && isEnabled()) {
|
1797 | 1825 | maybeDrawHalo(canvas, trackWidth, yCenter);
|
|
0 commit comments