且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android中绘制自定义可绘制形状

更新时间:2022-12-30 22:10:55

更好的方法不要使用 layer-list

Even better way Don't create whole shape using layer-list

  • 使用它的主要缺点是您需要给出shape的固定hightwidth
  • The main disadvantage using that is you need to give fixed hight and width of shape

使用简单的方法只需使用 layer-list 创建顶部箭头,然后使用以下示例

Use Simple way just create top arrow using layer-list and than use as below sample

尝试一下

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="10dp"
        android:src="@drawable/test" />


    <LinearLayout
        android:id="@+id/rootView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="vertical">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text=" Lorem ipsum dolor sit amet, consectetur adipiscing elit"
            android:textColor="@color/colorAccent" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text=" Lorem ipsum dolor sit amet, consectetur adipiscing elit"
            android:textColor="@android:color/black" />


    </LinearLayout>


</LinearLayout>

@ drawable/test"

@drawable/test"

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
            android:fromDegrees="45"
            android:pivotX="-40%"
            android:pivotY="87%"
            android:toDegrees="45">
            <shape android:shape="rectangle">
                <stroke
                    android:width="10dp"
                    android:color="@android:color/white" />
                <solid android:color="@android:color/white" />
            </shape>
        </rotate>
    </item>
</layer-list>

输出