Android - 如何使内圆角像纸标签一样?

时间:2021-08-01 18:26:04

标签: android android-drawable

enter image description here

我想绘制以下图像,但我对如何绘制橙色的开始区域感到困惑。我希望它是圆形的,但如果你从照片中看到我的意思,它是一个内圈。我尝试使用这样的负半径:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <solid android:color="@color/my_green" />
  <corners android:radius="-24dp" />
</shape>

但这没有任何作用。这是如何实现的?

p.s 不一定是橙色,可以是同一种颜色,我只是想强调这个问题。

1 个答案:

答案 0 :(得分:1)

您可以通过在自定义 <vector 文件中使用 drawable 来实现这一点,此处为 invert_shape.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="200dp"
    android:height="100dp"
    android:viewportWidth="359.5"
    android:viewportHeight="196.0">
    <path
        android:pathData="M-0.000,0.000 C108.667,0.000 217.333,0.000 326.000,0.000 C326.000,32.000 326.000,64.000 326.000,96.000 C217.333,96.000 108.667,96.000 -0.000,96.000 C46.617,64.186 37.267,32.149 -0.000,0.000 Z"
        android:fillColor="#2962FF"/>
</vector>

之后,您需要像这样在您的 drawable 中将此 android:background 设置为 LinearLayoutactivity_main.xml

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/linear_layout"
        android:background="@drawable/invert_shape"
        android:layout_centerInParent="true"
        android:orientation="horizontal" />

结果:

inverted shape

您可能会问自己如何在您的 android:path 中实现 <vector,为此我使用了一个简单的解决方案。您可以使用 PhotoshopIllustrator 等程序并在 layer 中创建您的形状。如果您已完成此操作,请右键单击您的 layer 并选择“Copy SVG”。现在您复制了形状,将其粘贴到 drawable 中的 invert_shape.xml(此处为 Android Studio)中,您将获得 Photoshop 形状的 android:pathandroid:stroke 等参数.现在,您的创造力永无止境。干杯! :)