Skip to content

Commit

Permalink
[Slider] Added option to show/hide tick marks in discrete mode
Browse files Browse the repository at this point in the history
Resolves #1545

GIT_ORIGIN_REV_ID=6c5dc94df08ad7ec202722d5b546795f436de404
PiperOrigin-RevId: 324263710
  • Loading branch information
gabrielemariotti authored and dsn5ft committed Jul 31, 2020
1 parent f6700fb commit 5fb7964
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 11 deletions.
Expand Up @@ -48,6 +48,7 @@ public View onCreateDemoView(
setUpSlider(view, R.id.switch_button_3, R.id.slider_3, null);
setUpSlider(view, R.id.switch_button_4, R.id.slider_4, new BasicLabelFormatter());
setUpSlider(view, R.id.switch_button_5, R.id.slider_5, null);
setUpSlider(view, R.id.switch_button_6, R.id.slider_6, null);

return view;
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
-->
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand Down Expand Up @@ -175,5 +176,36 @@
android:valueTo="5.75"
android:stepSize="0.25" />
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/cat_slider_title_top_padding"
android:text="@string/cat_slider_title_6"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_button_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.SecondaryPalette.Green" />

<com.google.android.material.slider.Slider
android:id="@+id/slider_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="@dimen/cat_slider_left_margin"
android:layout_marginStart="@dimen/cat_slider_left_margin"
android:theme="@style/ThemeOverlay.PrimaryPalette.Green"
android:valueFrom="0"
android:valueTo="10"
android:stepSize="1"
app:tickVisible="false"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Expand Up @@ -34,6 +34,7 @@
<string name="cat_slider_title_3" description="The title for a demonstration slider">Negative numbers!</string>
<string name="cat_slider_title_4" description="The title for a demonstration slider">With a label formatter</string>
<string name="cat_slider_title_5" description="The title for a demonstration slider">I can have decimal numbers?</string>
<string name="cat_slider_title_6" description="The title for a demonstration slider">Without tick marks</string>
<string name="cat_slider_start_touch_description" description="Indicates the slider is now being touched">Slider started being touched</string>
<string name="cat_slider_stop_touch_description" description="Indicates the slider stopped being touched">Slider stopped being touched</string>

Expand Down
52 changes: 41 additions & 11 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Expand Up @@ -125,6 +125,8 @@
* discrete mode. This is a short hand for setting both the {@code tickColorActive} and {@code
* tickColorInactive} to the same thing. This takes precedence over {@code tickColorActive}
* and {@code tickColorInactive}.
* <li>{@code tickVisible}: Whether to show the tick marks. Only used when the slider is in
* discrete mode.
* <li>{@code trackColorActive}: The color of the active part of the track.
* <li>{@code trackColorInactive}: The color of the inactive part of the track.
* <li>{@code trackColor}: The color of the whole track. This is a short hand for setting both the
Expand Down Expand Up @@ -167,6 +169,7 @@
* @attr ref com.google.android.material.R.styleable#Slider_tickColor
* @attr ref com.google.android.material.R.styleable#Slider_tickColorActive
* @attr ref com.google.android.material.R.styleable#Slider_tickColorInactive
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
* @attr ref com.google.android.material.R.styleable#Slider_trackColor
* @attr ref com.google.android.material.R.styleable#Slider_trackColorActive
* @attr ref com.google.android.material.R.styleable#Slider_trackColorInactive
Expand Down Expand Up @@ -244,6 +247,7 @@ private interface TooltipDrawableFactory {
private int focusedThumbIdx = -1;
private float stepSize = 0.0f;
private float[] ticksCoordinates;
private boolean tickVisible = true;
private int trackWidth;
private boolean forceDrawCompatHalo;
private boolean isLongPress = false;
Expand Down Expand Up @@ -401,6 +405,7 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle
? haloColor
: AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color));

tickVisible = a.getBoolean(R.styleable.Slider_tickVisible, true);
boolean hasTickColor = a.hasValue(R.styleable.Slider_tickColor);
int tickColorInactiveRes =
hasTickColor ? R.styleable.Slider_tickColor : R.styleable.Slider_tickColorInactive;
Expand Down Expand Up @@ -1119,6 +1124,29 @@ public void setTickInactiveTintList(@NonNull ColorStateList tickColor) {
invalidate();
}

/**
* Returns whether the tick marks are visible. Only used when the slider is in discrete mode.
*
* @see #setTickVisible(boolean)
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
*/
public boolean isTickVisible() {
return tickVisible;
}

/**
* Sets whether the tick marks are visible. Only used when the slider is in discrete mode.
*
* @param tickVisible The visibility of tick marks.
* @attr ref com.google.android.material.R.styleable#Slider_tickVisible
*/
public void setTickVisible(boolean tickVisible) {
if (this.tickVisible != tickVisible){
this.tickVisible = tickVisible;
postInvalidate();
}
}

/**
* Returns the color of the track if the active and inactive parts aren't different.
*
Expand Down Expand Up @@ -1270,14 +1298,16 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
trackWidth = Math.max(w - trackSidePadding * 2, 0);

// Update the visible tick coordinates.
if (stepSize > 0.0f) {
calculateTicksCoordinates();
}
maybeCalculateTicksCoordinates();

updateHaloHotspot();
}

private void calculateTicksCoordinates() {
private void maybeCalculateTicksCoordinates() {
if (stepSize <= 0.0f) {
return;
}

validateConfigurationIfDirty();

int tickCount = (int) ((valueTo - valueFrom) / stepSize + 1);
Expand Down Expand Up @@ -1318,9 +1348,7 @@ protected void onDraw(@NonNull Canvas canvas) {
validateConfigurationIfDirty();

// Update the visible tick coordinates.
if (stepSize > 0.0f) {
calculateTicksCoordinates();
}
maybeCalculateTicksCoordinates();
}

super.onDraw(canvas);
Expand All @@ -1332,9 +1360,7 @@ protected void onDraw(@NonNull Canvas canvas) {
drawActiveTrack(canvas, trackWidth, top);
}

if (stepSize > 0.0f) {
drawTicks(canvas);
}
maybeDrawTicks(canvas);

if ((thumbIsPressed || isFocused()) && isEnabled()) {
maybeDrawHalo(canvas, trackWidth, top);
Expand Down Expand Up @@ -1395,7 +1421,11 @@ private void drawActiveTrack(@NonNull Canvas canvas, int width, int top) {
canvas.drawLine(left, top, right, top, activeTrackPaint);
}

private void drawTicks(@NonNull Canvas canvas) {
private void maybeDrawTicks(@NonNull Canvas canvas) {
if (!tickVisible || stepSize <= 0.0f) {
return;
}

float[] activeRange = getActiveRange();
int leftPivotIndex = pivotIndex(ticksCoordinates, activeRange[0]);
int rightPivotIndex = pivotIndex(ticksCoordinates, activeRange[1]);
Expand Down
Expand Up @@ -27,6 +27,7 @@
<public name="tickColor" type="attr" />
<public name="tickColorActive" type="attr" />
<public name="tickColorInactive" type="attr" />
<public name="tickVisible" type="attr" />
<public name="trackColorActive" type="attr" />
<public name="trackColorInactive" type="attr" />
<public name="trackHeight" type="attr" />
Expand Down
Expand Up @@ -51,6 +51,8 @@
<!-- The color of the slider's tick marks for the inactive portion of the track. Only used when
the slider is in discrete mode. -->
<attr name="tickColorInactive" format="color" />
<!-- Whether to show the tick marks. Only used when the slider is in discrete mode. -->
<attr name="tickVisible" format="boolean" />
<!-- The color of the track. -->
<attr name="trackColor" />
<!-- The color of active portion of the track. -->
Expand Down

0 comments on commit 5fb7964

Please sign in to comment.