Skip to content

Commit 560c982

Browse files
cketchamafohrman
authored andcommitted
Enable shadows for BottomAppBar
PiperOrigin-RevId: 218868156
1 parent 613edaa commit 560c982

File tree

5 files changed

+15
-19
lines changed

5 files changed

+15
-19
lines changed

lib/java/com/google/android/material/bottomappbar/BottomAppBar.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.android.material.R;
2020

2121
import static com.google.android.material.internal.ThemeEnforcement.createThemedContext;
22+
import static com.google.android.material.shape.MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS;
2223

2324
import android.animation.Animator;
2425
import android.animation.AnimatorListenerAdapter;
@@ -137,7 +138,7 @@ public class BottomAppBar extends Toolbar implements AttachedBehavior {
137138
public @interface FabAnimationMode {}
138139

139140
private final int fabOffsetEndMode;
140-
private final MaterialShapeDrawable materialShapeDrawable;
141+
private final MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable();
141142
private final BottomAppBarTopEdgeTreatment topEdgeTreatment;
142143

143144
@Nullable private Animator modeAnimator;
@@ -207,6 +208,7 @@ public BottomAppBar(Context context, @Nullable AttributeSet attrs, int defStyleA
207208
ColorStateList backgroundTint =
208209
MaterialResources.getColorStateList(context, a, R.styleable.BottomAppBar_backgroundTint);
209210

211+
int elevation = a.getDimensionPixelSize(R.styleable.BottomAppBar_elevation, 0);
210212
float fabCradleMargin = a.getDimensionPixelOffset(R.styleable.BottomAppBar_fabCradleMargin, 0);
211213
float fabCornerRadius =
212214
a.getDimensionPixelOffset(R.styleable.BottomAppBar_fabCradleRoundedCornerRadius, 0);
@@ -225,12 +227,12 @@ public BottomAppBar(Context context, @Nullable AttributeSet attrs, int defStyleA
225227

226228
topEdgeTreatment =
227229
new BottomAppBarTopEdgeTreatment(fabCradleMargin, fabCornerRadius, fabVerticalOffset);
228-
ShapeAppearanceModel appBarModel = new ShapeAppearanceModel();
230+
ShapeAppearanceModel appBarModel = materialShapeDrawable.getShapeAppearanceModel();
229231
appBarModel.setTopEdge(topEdgeTreatment);
230-
materialShapeDrawable = new MaterialShapeDrawable(appBarModel);
232+
materialShapeDrawable.setShadowCompatibilityMode(SHADOW_COMPAT_MODE_ALWAYS);
231233
materialShapeDrawable.setPaintStyle(Style.FILL);
232234
materialShapeDrawable.setPaintFlags(Paint.ANTI_ALIAS_FLAG);
233-
materialShapeDrawable.setPaintShadowEnabled(true);
235+
setElevation(elevation);
234236
DrawableCompat.setTintList(materialShapeDrawable, backgroundTint);
235237
ViewCompat.setBackground(this, materialShapeDrawable);
236238
}
@@ -356,6 +358,11 @@ public void setHideOnScroll(boolean hide) {
356358
hideOnScroll = hide;
357359
}
358360

361+
@Override
362+
public void setElevation(float elevation) {
363+
materialShapeDrawable.setShadowElevation((int) elevation);
364+
}
365+
359366
/**
360367
* A convenience method to replace the contents of the BottomAppBar's menu.
361368
*

lib/java/com/google/android/material/bottomappbar/BottomAppBarTopEdgeTreatment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void getEdgePath(float length, float center, float interpolation, ShapePa
109109
float cutoutArcOffset = ARC_QUARTER - cornerRadiusArcLength;
110110

111111
// Draw the starting line up to the left rounded corner.
112-
shapePath.lineTo(/* x= */ leftRoundedCornerCircleX - roundedCornerOffset, /* y= */ 0);
112+
shapePath.lineTo(/* x= */ leftRoundedCornerCircleX, /* y= */ 0);
113113

114114
// Draw the arc for the left rounded corner circle. The bounding box is the area around the
115115
// circle's center which is at `(leftRoundedCornerCircleX, roundedCornerOffset)`.

lib/java/com/google/android/material/bottomappbar/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<declare-styleable name="BottomAppBar">
1919
<!-- Background for the BottomAppBar. -->
2020
<attr name="backgroundTint"/>
21+
<!-- Elevation for the BottomAppBar. -->
22+
<attr name="elevation"/>
2123
<!-- The alignment of the fab relative to the BottomAppBar. -->
2224
<attr name="fabAlignmentMode">
2325
<!-- Mode that aligns the fab to the center. -->

lib/java/com/google/android/material/bottomappbar/res/values/styles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<item name="fabCradleVerticalOffset">@dimen/mtrl_bottomappbar_fab_cradle_vertical_offset</item>
3939
<item name="android:minHeight">@dimen/mtrl_bottomappbar_height</item>
4040
<item name="maxButtonHeight">@dimen/mtrl_bottomappbar_height</item>
41+
<item name="elevation">8dp</item>
4142
</style>
4243

4344
<style name="Widget.MaterialComponents.BottomAppBar.Colored" parent="Widget.MaterialComponents.BottomAppBar">

lib/java/com/google/android/material/shape/MaterialShapeDrawable.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public class MaterialShapeDrawable extends Drawable implements TintAwareDrawable
116116

117117
private ShapeAppearanceModel shapeAppearanceModel;
118118
private int shadowCompatMode = SHADOW_COMPAT_MODE_DEFAULT;
119-
private boolean paintShadowEnabled = false;
120119
private boolean useTintColorForShadow = false;
121120
private float interpolation = 1f;
122121
private int shadowCompatElevation = 0;
@@ -467,15 +466,6 @@ public void setShadowEnabled(boolean shadowEnabled) {
467466
shadowEnabled ? SHADOW_COMPAT_MODE_DEFAULT : SHADOW_COMPAT_MODE_NEVER);
468467
}
469468

470-
/** TODO: Remove the paint shadow */
471-
public void setPaintShadowEnabled(boolean paintShadowEnabled) {
472-
this.paintShadowEnabled = paintShadowEnabled;
473-
shadowCompatMode = SHADOW_COMPAT_MODE_NEVER;
474-
// Backwards compatible defaults.
475-
shadowCompatElevation = 5;
476-
shadowCompatRadius = 10;
477-
}
478-
479469
/**
480470
* Get the interpolation of the path, between 0 and 1. Ranges between 0 (none) and 1 (fully)
481471
* interpolated.
@@ -716,10 +706,6 @@ public void draw(Canvas canvas) {
716706
final int prevStrokeAlpha = strokePaint.getAlpha();
717707
strokePaint.setAlpha(modulateAlpha(prevStrokeAlpha, alpha));
718708

719-
if (shadowCompatElevation > 0 && paintShadowEnabled) {
720-
fillPaint.setShadowLayer(shadowCompatRadius, 0, shadowCompatElevation, Color.BLACK);
721-
}
722-
723709
calculatePath(getBoundsInsetByStroke(), pathInsetByStroke);
724710
if (hasCompatShadow()) {
725711
// Save the canvas before changing the clip bounds.

0 commit comments

Comments
 (0)