Skip to content

Commit d4290db

Browse files
committed
patch 1
1 parent 4a0b05d commit d4290db

File tree

1 file changed

+50
-22
lines changed

1 file changed

+50
-22
lines changed

lib/java/com/google/android/material/slider/BaseSlider.java

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
import android.os.Bundle;
6060
import android.os.Parcel;
6161
import android.os.Parcelable;
62-
63-
import androidx.annotation.RestrictTo;
6462
import androidx.appcompat.content.res.AppCompatResources;
6563
import android.util.AttributeSet;
6664
import android.util.Log;
@@ -297,8 +295,8 @@ private interface TooltipDrawableFactory {
297295
// The index of the currently focused thumb.
298296
private int focusedThumbIdx = -1;
299297
private float stepSize = 0.0f;
298+
private boolean areTicksDrawn = false;
300299
private float[] ticksCoordinates;
301-
private boolean tickVisible = false;
302300
private int tickVisibilityMode;
303301
private int trackWidth;
304302
private boolean forceDrawCompatHalo;
@@ -479,9 +477,8 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
479477
? haloColor
480478
: AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color));
481479

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);
485482
} else {
486483
this.tickVisibilityMode = a.getBoolean(R.styleable.Slider_tickVisible, true)
487484
? TICK_VISIBILITY_AUTO_LIMIT
@@ -1489,10 +1486,30 @@ public void setTickInactiveTintList(@NonNull ColorStateList tickColor) {
14891486
}
14901487

14911488
/**
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.
14931495
*/
1496+
@Deprecated
14941497
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;
14961513
}
14971514

14981515
/**
@@ -1702,28 +1719,39 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
17021719
}
17031720

17041721
private void updateTicksCoordinates() {
1705-
if (stepSize <= 0.0f || tickVisibilityMode == TICK_VISIBILITY_HIDDEN) {
1706-
updateTicksCoordinates(0);
1722+
if (stepSize <= 0.0f) {
1723+
updateTicksCoordinates(/* tickCount= */ 0);
17071724
return;
17081725
}
17091726

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;
17171728
switch (tickVisibilityMode) {
1729+
case TICK_VISIBILITY_VISIBLE_ALL:
1730+
tickCount = getDesiredTickCount();
1731+
break;
17181732
case TICK_VISIBILITY_AUTO_LIMIT:
1719-
updateTicksCoordinates(min(tickCount, maxTickCount));
1720-
return;
1733+
tickCount = min(getDesiredTickCount(), getMaxTickCount());
1734+
break;
17211735
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;
17241742
default:
17251743
throw new IllegalArgumentException("Invalid tick visibility mode: " + tickVisibilityMode);
17261744
}
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;
17271755
}
17281756

17291757
private void updateTicksCoordinates(int tickCount) {
@@ -1791,7 +1819,7 @@ protected void onDraw(@NonNull Canvas canvas) {
17911819
drawActiveTrack(canvas, trackWidth, yCenter);
17921820
}
17931821

1794-
tickVisible = maybeDrawTicks(canvas);
1822+
areTicksDrawn = maybeDrawTicks(canvas);
17951823

17961824
if ((thumbIsPressed || isFocused() || shouldAlwaysShowLabel()) && isEnabled()) {
17971825
maybeDrawHalo(canvas, trackWidth, yCenter);

0 commit comments

Comments
 (0)