效果比较生硬,其本质就是颜色的渐变,如下:
还有如让ui切阴影背景图,但由于控件大小规格差异较大,风格差异较大,并不推荐使用。
自定义阴影布局
这是我比较推荐的方式,可参考CardView的阴影实现自定义一个阴影布局实现。其实现是通过 setShadowLayer、setMaskFilter 实现。
// mPaint.setShadowLayer(blurRadius,0,0,shadowColor);
if (blurRadius>0){
mPaint.setMaskFilter(new BlurMaskFilter(blurRadius,BlurMaskFilter.Blur.NORMAL));
}
相较于 setShadowLayer 来说,setMaskFilter 可供选中的实现方式要多一个blur实现类型,效果更好些,所以我是通过使用 setMaskFilter 来实现自定义阴影布局。
<cn.enjoytoday.shadow.ShadowLayout
android:id=“@+id/shadow3”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toBottomOf=“@id/shadow1”
android:layout_marginTop=“20dp”
android:text=“”
app:shadowRadius=“0dp”
app:shadowColor=“#333”
app:blurRadius=“5dp”
app:xOffset=“0dp”
app:yOffset=“0dp”
android:layout_marginStart=“15dp”
android:gravity=“center”
android:background=“@drawable/shadow_layer”
android:textSize=“14sp”
android:textColor=“@color/colorBlack”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”>
</cn.enjoytoday.shadow.ShadowLayout>
使用
ShadowView 托管于GitHub, 仿照css的Box Shadow 的阴影实现效果设计实现,可通过设置水平、竖直偏移确认阴影方向,可设置模糊半径和圆角半径、阴影颜色等。可通过gradle直接依赖使用:
添加依赖
repositories {
//…
maven { url ‘https://jitpack.io’ }
}
dependencies {
implementation ‘com.github.amikoj:ShadowView:1.0.1’
}
xml中使用
<cn.enjoytoday.shadow.ShadowLayout
android:orientation=“vertical”
android:id=“@+id/shadowLayout”
app:layout_constraintBottom_toBottomOf=“parent”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintRight_toRightOf=“parent”
app:layout_constraintTop_toTopOf=“parent”
android:gravity=“center”
app:shadowRadius=“10dp”
app:shadowColor=“#bebebe”
app:bgColor=“#fff”
app:xOffset=“10dp”
app:yOffset=“0dp”
app:blurRadius=“5dp”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”>
</cn.enjoytoday.shadow.ShadowLayout>
属性说明
属性名 | 类型 | 说明 |
---|---|---|
shadowColor | color | 阴影渲染颜色 |
shadowRadius | dimension | 背景圆角半径(0为矩形) |
属性名 | 类型 | 说明 |
— | — | — |
shadowColor | color | 阴影渲染颜色 |
shadowRadius | dimension | 背景圆角半径(0为矩形) |