带阴影的弯曲 android 工具栏

Curved android toolbar with shadow

我需要为 Toolbar 或 CardView 设置一个弯曲的底部视图。

我尝试过的:

bg_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" />
    </item>
    <item
            android:bottom="0dp"
            android:left="-100dp"
            android:right="-100dp"
            android:top="-80dp">
        <shape android:shape="oval">
            <solid android:color="@color/colorAccent" />
        </shape>
    </item>
</layer-list>

将此设置为工具栏的背景会使应用栏提升(阴影)无效。此外,如果此背景应用于 CardView 背景,但阴影与背景的形状不一致。

看下图,我想让阴影环绕曲线。

Try with below layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        app:elevation="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/test1"
        android:gravity="center"
        android:padding="@dimen/_10sdp"
        android:text="My Title"
        android:textSize="@dimen/_20ssp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar" />

</android.support.constraint.ConstraintLayout>

test1.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" />
    </item>
    <item
        android:bottom="0dp"
        android:left="-100dp"
        android:right="-100dp"
        android:top="-80dp">
        <shape android:shape="oval">
            <solid android:color="#50CCCCCC" />
        </shape>
    </item>
    <item
        android:bottom="10dp"
        android:left="-100dp"
        android:right="-100dp"
        android:top="-80dp">
        <shape android:shape="oval">
            <solid android:color="@color/colorAccent"/>//Background Color
        </shape>
    </item>
</layer-list>

输出:

我使用 arcView 完成了此操作

依赖实现'com.github.florent37:shapeofview:1.3.2'

<com.github.florent37.shapeofview.shapes.ArcView
        android:layout_width="match_parent"
        android:layout_height="155dp"
        android:elevation="5.0dp"
        app:shape_arc_cropDirection="outside"
        app:shape_arc_height="35dp"
        app:shape_arc_position="bottom">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="155dp"
            android:background="@color/colorPrimary"
            android:elevation="5dp" />

    </com.github.florent37.shapeofview.shapes.ArcView>

查看附件图片

您可以使用以下 xml 文件作为背景,并根据需要提供特定的半径:

shadowfile.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
            <solid android:color="#18000000" />
            <corners android:radius="8dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
            <solid android:color="#05000000" />
            <corners android:radius="7dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
            <solid android:color="#10000000" />
            <corners android:radius="6dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
            <solid android:color="#15000000" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
            <solid android:color="#20000000" />
            <corners android:radius="4dp" />
        </shape>
    </item>

    <!-- Background -->
    <item>
        <shape>
            <solid android:color="#FFFFFF" />
            <corners android:radius="0dp" />
        </shape>
    </item>
</layer-list>

注意:此处一项等于一层,您可以根据需要添加或删除

在布局或工具栏中使用:

android:background="@drawable/shadowfile"  

来自