先來看一張使用 ShadowLayout 庫實現的各種陰影的效果圖,如下圖所示:
如上圖所示,通過使用 ShadowLayout 可以控制陰影的顏色、范圍、顯示邊界(上下左右四個邊界)、x 軸和 y 軸的偏移量。
Gradle:
compile 'com.lijiankun24:shadowlayout:1.0.0'
在 xml 中添加如下布局文件:
<com.lijiankun24.shadowlayout.ShadowLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
app:shadowColor="#66000000"
app:shadowDx="0dp"
app:shadowDy="3dp"
app:shadowRadius="10dp"
app:shadowSide="all">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:contentDescription="@null"
android:src="@mipmap/ic_launcher"/>
</com.lijiankun24.shadowlayout.ShadowLayout>
上面 xml 布局文件實現的效果如下圖所示:
如上面 xml 中代碼顯示的那樣,總共有 5 個自定義屬性,其含義分別如下:
- app:shadowColor="#66000000" 控制陰影的顏色,注意:顏色必須帶有透明度的值
- app:shadowDx="0dp" 控制陰影 x 軸的偏移量
- app:shadowDy="3dp" 控制陰影 y 軸的偏移量
- app:shadowRadius="10dp" 控制陰影的范圍
- app:shadowSide="all|left|right|top|bottom" 控制陰影顯示的邊界,共有五個值