Android 自定义View直线、圆形、椭圆、实心圆、矩形等

 项目中经常用到各种各样的样式,在此记录下;

直线:

<?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>

圆形:

<?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>

椭圆:

<?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>

实心圆:用 Stroke描边,对应 Paint.STROKE ,而用Solid 填充,对应 Paint.FILL,会出现实心圆形。

<?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>

矩形:

<?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>

猜你喜欢

转载自blog.csdn.net/qq_19714505/article/details/84561662