how to create a semi oval shape (bending a line)

2019-05-30 13:17发布

问题:

I'm trying to create a custom shape for my NavigationView footer, as background. but it's not so clean. This is what I have done:

And this is what I need:

Code:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:gravity="bottom">
    <shape android:shape="rectangle">
        <solid android:color="@color/darkerGray" />
    </shape>
</item>
<item

    android:gravity="bottom|center_horizontal"
    android:top="50dp">
    <!--android:top="-40dp"-->

    <shape android:shape="oval">
        <solid android:color="#ffffffff" />
    </shape>
</item>
<item

    android:bottom="30dp"
    android:gravity="bottom">
    <shape android:shape="rectangle">
        <solid android:color="#ffffffff" />
    </shape>
</item>

Can you help me, please?

回答1:

#. Update your custom drawable XML as below:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Container GRAY rectangle -->
    <item>
        <shape android:shape="rectangle">

            <size
                android:width="250dp"
                android:height="100dp" />

            <solid
                android:color="@android:color/darker_gray" />
        </shape>
    </item>

    <!-- Top WHITE oval shape -->
    <item
        android:left="-25dp"
        android:right="-25dp"
        android:top="-50dp"
        android:bottom="50dp">

        <shape android:shape="oval">

            <solid
                android:color="@android:color/white" />
        </shape>
    </item>
</layer-list>

PREVIEW:

#. Use this custom drawable in your layout as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="@android:color/black"
    android:padding="16dp">

    <!-- Custom Footer -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/custom_shape">

    </LinearLayout>
</LinearLayout>

OUTPUT:

Hope this will help~



回答2:

I would use a vector drawable to achieve this.

<vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="8dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">

    <path
        android:pathData="M0 -5.4a13 7 0 1 0 24 0V24H0z"
        android:fillColor="#FF000000"/>

</vector>

The pathData attribute here is relatively straightforward:

  • Move the cursor to the top-left corner
  • Arc to the top-right corner
  • Line to the bottom-right corner
  • Line to the bottom-left corner
  • Close the path

You can adjust the appearance of the curve (how deep it goes) by fiddling with the first two numbers after the "a" (i.e. a13 7); these are the x-radius and the y-radius. A larger x-radius will make the curve flatter overall, and a larger y-radius will make the curve go farther down into the square.

The path starts at a negative y-axis value because we're (currently) using an x-radius that's larger than half of our shape, so the arc needs to be adjusted up so that it hits (0,0) when it first appears. Consequently, if you modify the x-radius you will also have to modify the origin of the path (the numbers after the initial "M", i.e. M0 -5.4).