|
60 | 60 | import android.os.Parcel;
|
61 | 61 | import android.os.Parcelable;
|
62 | 62 |
|
63 |
| -import androidx.annotation.RestrictTo; |
64 | 63 | import androidx.appcompat.content.res.AppCompatResources;
|
65 | 64 | import android.util.AttributeSet;
|
66 | 65 | import android.util.Log;
|
@@ -297,8 +296,8 @@ private interface TooltipDrawableFactory {
|
297 | 296 | // The index of the currently focused thumb.
|
298 | 297 | private int focusedThumbIdx = -1;
|
299 | 298 | private float stepSize = 0.0f;
|
| 299 | + private boolean areTicksDrawn = false; |
300 | 300 | private float[] ticksCoordinates;
|
301 |
| - private boolean tickVisible = false; |
302 | 301 | private int tickVisibilityMode;
|
303 | 302 | private int trackWidth;
|
304 | 303 | private boolean forceDrawCompatHalo;
|
@@ -479,9 +478,8 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
|
479 | 478 | ? haloColor
|
480 | 479 | : AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color));
|
481 | 480 |
|
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); |
485 | 483 | } else {
|
486 | 484 | this.tickVisibilityMode = a.getBoolean(R.styleable.Slider_tickVisible, true)
|
487 | 485 | ? TICK_VISIBILITY_AUTO_LIMIT
|
@@ -1489,10 +1487,10 @@ public void setTickInactiveTintList(@NonNull ColorStateList tickColor) {
|
1489 | 1487 | }
|
1490 | 1488 |
|
1491 | 1489 | /**
|
1492 |
| - * Returns whether the tick marks are visible. |
| 1490 | + * Returns whether the tick marks are visible (drawn). |
1493 | 1491 | */
|
1494 | 1492 | public boolean isTickVisible() {
|
1495 |
| - return tickVisible; |
| 1493 | + return areTicksDrawn; |
1496 | 1494 | }
|
1497 | 1495 |
|
1498 | 1496 | /**
|
@@ -1702,28 +1700,39 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
1702 | 1700 | }
|
1703 | 1701 |
|
1704 | 1702 | 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); |
1713 | 1705 | return;
|
1714 | 1706 | }
|
1715 | 1707 |
|
1716 |
| - final int maxTickCount = trackWidth / (trackHeight * 2) + 1; |
| 1708 | + final int tickCount; |
1717 | 1709 | switch (tickVisibilityMode) {
|
| 1710 | + case TICK_VISIBILITY_VISIBLE_ALL: |
| 1711 | + tickCount = getDesiredTickCount(); |
| 1712 | + break; |
1718 | 1713 | case TICK_VISIBILITY_AUTO_LIMIT:
|
1719 |
| - updateTicksCoordinates(min(tickCount, maxTickCount)); |
1720 |
| - return; |
| 1714 | + tickCount = min(getDesiredTickCount(), getMaxTickCount()); |
| 1715 | + break; |
1721 | 1716 | 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; |
1724 | 1723 | default:
|
1725 | 1724 | throw new IllegalArgumentException("Invalid tick visibility mode: " + tickVisibilityMode);
|
1726 | 1725 | }
|
| 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; |
1727 | 1736 | }
|
1728 | 1737 |
|
1729 | 1738 | private void updateTicksCoordinates(int tickCount) {
|
@@ -1791,7 +1800,7 @@ protected void onDraw(@NonNull Canvas canvas) {
|
1791 | 1800 | drawActiveTrack(canvas, trackWidth, yCenter);
|
1792 | 1801 | }
|
1793 | 1802 |
|
1794 |
| - tickVisible = maybeDrawTicks(canvas); |
| 1803 | + areTicksDrawn = maybeDrawTicks(canvas); |
1795 | 1804 |
|
1796 | 1805 | if ((thumbIsPressed || isFocused() || shouldAlwaysShowLabel()) && isEnabled()) {
|
1797 | 1806 | maybeDrawHalo(canvas, trackWidth, yCenter);
|
|
0 commit comments