|
37 | 37 | import androidx.annotation.RestrictTo; |
38 | 38 | import androidx.dynamicanimation.animation.SpringForce; |
39 | 39 | import com.google.android.material.color.MaterialColors; |
| 40 | +import com.google.android.material.focus.FocusRingDrawable; |
40 | 41 | import com.google.android.material.internal.ViewUtils; |
41 | 42 | import com.google.android.material.resources.MaterialResources; |
42 | 43 | import com.google.android.material.ripple.RippleUtils; |
@@ -74,7 +75,7 @@ class MaterialButtonHelper { |
74 | 75 | private boolean cornerRadiusSet = false; |
75 | 76 | private boolean checkable; |
76 | 77 | private boolean toggleCheckedStateOnClick = true; |
77 | | - private LayerDrawable rippleDrawable; |
| 78 | + private RippleDrawable rippleDrawable; |
78 | 79 | private int elevation; |
79 | 80 |
|
80 | 81 | MaterialButtonHelper(MaterialButton button, @NonNull ShapeAppearance shapeAppearance) { |
@@ -147,6 +148,13 @@ private void updateBackground() { |
147 | 148 | // RippleDrawable lose their states, we need to reset the state here. |
148 | 149 | materialShapeDrawable.setState(materialButton.getDrawableState()); |
149 | 150 | } |
| 151 | + |
| 152 | + // Similar to the comment above, we need to set up the focus ring -> shape drawable connection |
| 153 | + // here, because the ripple's child drawables will be recreated when the background is set. |
| 154 | + FocusRingDrawable focusRingDrawable = FocusRingDrawable.find(materialButton.getBackground()); |
| 155 | + if (focusRingDrawable != null) { |
| 156 | + focusRingDrawable.setFocusRingMaterialShapeDrawable(materialShapeDrawable); |
| 157 | + } |
150 | 158 | } |
151 | 159 |
|
152 | 160 | /** |
@@ -246,6 +254,7 @@ private Drawable createBackground() { |
246 | 254 | new LayerDrawable( |
247 | 255 | new Drawable[] {surfaceColorStrokeDrawable, backgroundDrawable})), |
248 | 256 | maskDrawable); |
| 257 | + FocusRingDrawable.layer(context, rippleDrawable); |
249 | 258 | return rippleDrawable; |
250 | 259 | } |
251 | 260 |
|
@@ -420,15 +429,12 @@ private void updateButtonShape() { |
420 | 429 |
|
421 | 430 | @Nullable |
422 | 431 | public Shapeable getMaskDrawable() { |
423 | | - if (rippleDrawable != null && rippleDrawable.getNumberOfLayers() > 1) { |
424 | | - if (rippleDrawable.getNumberOfLayers() > 2) { |
425 | | - // This is a LayerDrawable with 3 layers, so return the mask layer |
426 | | - return (Shapeable) rippleDrawable.getDrawable(2); |
| 432 | + if (rippleDrawable != null) { |
| 433 | + Drawable mask = rippleDrawable.findDrawableByLayerId(android.R.id.mask); |
| 434 | + if (mask instanceof Shapeable) { |
| 435 | + return (Shapeable) mask; |
427 | 436 | } |
428 | | - // This is a RippleDrawable, so return the mask layer |
429 | | - return (Shapeable) rippleDrawable.getDrawable(1); |
430 | 437 | } |
431 | | - |
432 | 438 | return null; |
433 | 439 | } |
434 | 440 |
|
|
0 commit comments