BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
bottomSheetDialog.setContentView(R.layout.bottom_sheet_dialog);
View bottomSheet = bottomSheetDialog.getWindow().findViewById(R.id.design_bottom_sheet);
View scrimView = bottomSheet.findViewById(com.google.android.material.R.id.scrim_view);
ValueAnimator animator = ObjectAnimator.ofFloat(0.5f, 1.0f);
animator.setDuration(500);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
scrimView.setAlpha((float) animation.getAnimatedValue());
}
});
bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
animator.start();
}
});
bottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
animator.reverse();
}
});
这里的蒙层动画为透明度渐变动画(从0.5f到1.0f),并将动画的时长设置为500毫秒。最后,在BottomSheetDialog的显示和消失监听器中启动和反转动画。
注意:要使用com.google.android.material.R.id.scrim_view获取蒙层控件ID。
最终效果如下: