From 87f859c90630c7b806de692eaaf0f3a2280c9d4b Mon Sep 17 00:00:00 2001 From: pubiqq Date: Tue, 24 Dec 2024 22:53:55 +0300 Subject: [PATCH] [Slider] Add API to set thumb ripple effect color --- docs/components/Slider.md | 27 ++++++----- .../android/material/slider/BaseSlider.java | 45 +++++++++++++++++++ .../slider/res-public/values/public.xml | 1 + .../material/slider/res/values/attrs.xml | 3 ++ .../material/slider/res/values/styles.xml | 1 + 5 files changed, 65 insertions(+), 12 deletions(-) diff --git a/docs/components/Slider.md b/docs/components/Slider.md index 8298f0efa85..a6e743d2e8f 100644 --- a/docs/components/Slider.md +++ b/docs/components/Slider.md @@ -115,22 +115,25 @@ thickness. ### Thumb attributes -Element | Attribute | Related method(s) | Default value ----------------- | ----------------------- | --------------------------------------------------------------------------------- | ------------- -**Color** | `app:thumbColor` | `setThumbTintList`
`getThumbTintList` | `?attr/colorPrimary` -**Width** | `app:thumbWidth` | `setThumbWidth`
`setThumbWidthResource`
`getThumbWidth` | `4dp` -**Height** | `app:thumbHeight` | `setThumbHeight`
`setThumbHeightResource`
`getThumbHeight` | `44dp` -**Radius** | `app:thumbRadius` | `setThumbRadiusResource`
`setThumbRadius`
`getThumbRadius` | N/A -**Elevation** | `app:thumbElevation` | `setThumbElevationResource`
`setThumbElevation`
`getThumbElevation` | `2dp` -**Halo color** | `app:haloColor` | `setHaloTintList`
`getHaloTintList` | `@android:color/transparent` -**Halo radius** | `app:haloRadius` | `setHaloRadiusResource`
`setHaloRadius`
`getHaloRadius` | N/A -**Stroke color** | `app:thumbStrokeColor` | `setThumbStrokeColor`
`setThumbStrokeColorResource`
`getThumbStrokeColor` | `null` -**Stroke width** | `app:thumbStrokeWidth` | `setThumbStrokeWidth`
`setThumbStrokeWidthResource`
`getThumbStrokeWidth` | `0dp` -**Gap size** | `app:thumbTrackGapSize` | `setThumbTrackGapSize`
`getThumbTrackGapSize` | `6dp` +| Element | Attribute | Related method(s) | Default value | +|-----------------------|-------------------------|-----------------------------------------------------------------------------------|------------------------------| +| **Color** | `app:thumbColor` | `setThumbTintList`
`getThumbTintList` | `?attr/colorPrimary` | +| **Width** | `app:thumbWidth` | `setThumbWidth`
`setThumbWidthResource`
`getThumbWidth` | `4dp` | +| **Height** | `app:thumbHeight` | `setThumbHeight`
`setThumbHeightResource`
`getThumbHeight` | `44dp` | +| **Radius** | `app:thumbRadius` | `setThumbRadiusResource`
`setThumbRadius`
`getThumbRadius` | N/A | +| **Elevation** | `app:thumbElevation` | `setThumbElevationResource`
`setThumbElevation`
`getThumbElevation` | `2dp` | +| **Halo color** | `app:haloColor` | `setHaloTintList`
`getHaloTintList` | `@android:color/transparent` | +| **Halo effect color** | `app:haloEffectColor` | `setHaloEffectTintList`
`getHaloEffectTintList` | `@android:color/transparent` | +| **Halo radius** | `app:haloRadius` | `setHaloRadiusResource`
`setHaloRadius`
`getHaloRadius` | N/A | +| **Stroke color** | `app:thumbStrokeColor` | `setThumbStrokeColor`
`setThumbStrokeColorResource`
`getThumbStrokeColor` | `null` | +| **Stroke width** | `app:thumbStrokeWidth` | `setThumbStrokeWidth`
`setThumbStrokeWidthResource`
`getThumbStrokeWidth` | `0dp` | +| **Gap size** | `app:thumbTrackGapSize` | `setThumbTrackGapSize`
`getThumbTrackGapSize` | `6dp` | **Note:** `app:thumbWidth` and `app:thumbHeight` take precedence over `app:thumbRadius`. +**Note:** `app:haloEffectColor` only has an effect on API 31 and above. + ### Value label attributes Element | Attribute | Related method(s) | Default value diff --git a/lib/java/com/google/android/material/slider/BaseSlider.java b/lib/java/com/google/android/material/slider/BaseSlider.java index bf455830157..497c4e922a2 100644 --- a/lib/java/com/google/android/material/slider/BaseSlider.java +++ b/lib/java/com/google/android/material/slider/BaseSlider.java @@ -66,6 +66,7 @@ import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; +import androidx.annotation.RequiresApi; import androidx.appcompat.content.res.AppCompatResources; import android.text.TextUtils; import android.util.AttributeSet; @@ -274,6 +275,7 @@ abstract class BaseSlider< private static final float THUMB_WIDTH_PRESSED_RATIO = .5f; private static final int TRACK_CORNER_SIZE_UNSET = -1; private static final float TOUCH_SLOP_RATIO = .8f; + private static final int DEFAULT_HALO_EFFECT_COLOR = 0x8dffffff; static final int DEF_STYLE_RES = R.style.Widget_MaterialComponents_Slider; static final int UNIT_VALUE = 1; @@ -384,6 +386,7 @@ abstract class BaseSlider< private boolean dirtyConfig; @NonNull private ColorStateList haloColor; + @NonNull private ColorStateList haloEffectColor; @NonNull private ColorStateList tickColorActive; @NonNull private ColorStateList tickColorInactive; @NonNull private ColorStateList trackColorActive; @@ -593,6 +596,15 @@ private void processAttributes(Context context, AttributeSet attrs, int defStyle ? haloColor : AppCompatResources.getColorStateList(context, R.color.material_slider_halo_color)); + if (VERSION.SDK_INT >= VERSION_CODES.S) { + ColorStateList haloEffectColor = + MaterialResources.getColorStateList(context, a, R.styleable.Slider_haloEffectColor); + setHaloEffectTintList( + haloEffectColor != null + ? haloEffectColor + : ColorStateList.valueOf(DEFAULT_HALO_EFFECT_COLOR)); + } + tickVisibilityMode = a.hasValue(R.styleable.Slider_tickVisibilityMode) ? a.getInt(R.styleable.Slider_tickVisibilityMode, -1) @@ -1700,6 +1712,39 @@ public void setHaloTintList(@NonNull ColorStateList haloColor) { invalidate(); } + /** + * Returns the effect color of the halo. + * + * @see #setHaloEffectTintList(ColorStateList) + * @attr ref com.google.android.material.R.styleable#Slider_haloEffectColor + */ + @RequiresApi(api = VERSION_CODES.S) + @NonNull + public ColorStateList getHaloEffectTintList() { + return haloEffectColor; + } + + /** + * Sets the effect color of the halo. + * + * @see #getHaloEffectTintList() + * @attr ref com.google.android.material.R.styleable#Slider_haloEffectColor + */ + @RequiresApi(api = VERSION_CODES.S) + public void setHaloEffectTintList(@NonNull ColorStateList haloEffectColor) { + if (haloEffectColor.equals(this.haloEffectColor)) { + return; + } + + this.haloEffectColor = haloEffectColor; + + final Drawable background = getBackground(); + if (!shouldDrawCompatHalo() && background instanceof RippleDrawable) { + ((RippleDrawable) background).setEffectColor(haloEffectColor); + postInvalidate(); + } + } + /** * Returns the color of the thumb. * diff --git a/lib/java/com/google/android/material/slider/res-public/values/public.xml b/lib/java/com/google/android/material/slider/res-public/values/public.xml index 89d2189fe93..047695dc805 100644 --- a/lib/java/com/google/android/material/slider/res-public/values/public.xml +++ b/lib/java/com/google/android/material/slider/res-public/values/public.xml @@ -20,6 +20,7 @@ + diff --git a/lib/java/com/google/android/material/slider/res/values/attrs.xml b/lib/java/com/google/android/material/slider/res/values/attrs.xml index 92b0fd7496b..2b4d25e5226 100644 --- a/lib/java/com/google/android/material/slider/res/values/attrs.xml +++ b/lib/java/com/google/android/material/slider/res/values/attrs.xml @@ -28,6 +28,9 @@ + + diff --git a/lib/java/com/google/android/material/slider/res/values/styles.xml b/lib/java/com/google/android/material/slider/res/values/styles.xml index 94ecabbb0c7..8a40137fb39 100644 --- a/lib/java/com/google/android/material/slider/res/values/styles.xml +++ b/lib/java/com/google/android/material/slider/res/values/styles.xml @@ -55,6 +55,7 @@