Android custom View straight line, circle, ellipse, solid circle, rectangle, etc.

 Various styles are often used in projects and are recorded here;

straight line:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="5dp"
        android:color="@color/blue" />
</shape>

Round:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="1dp"
        android:color="@color/blue" />
    <!--定义宽和高相等的话就是圆形-->
    <size
        android:width="100dp"
        android:height="100dp" />
</shape>

oval:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="3dp"
        android:color="@color/blue" />
</shape>

Solid circle: Use Stroke to outline, corresponding to Paint.STROKE, and use Solid to fill, corresponding to Paint.FILL, and a solid circle will appear.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <!-- 渐变色-->
    <gradient
        android:endColor="@color/blue"
        android:startColor="@color/red" />
    <size
        android:width="100dp"
        android:height="100dp" />

</shape>

rectangle:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 圆角-->
    <corners android:radius="5dp" />
    <!-- 高度-->
    <stroke
        android:width="1dp"
        android:color="@color/blue" />
    <!-- 大小-->
    <size
        android:width="100dp"
        android:height="100dp" />

</shape>

Guess you like

Origin blog.csdn.net/qq_19714505/article/details/84561662