学而实习之 不亦乐乎

Android:ShapeDrawable 资源

2022-07-17 10:45:59

ShapeDrawable 用于定义一个基本的几何图形,如矩形(rectangle)、圆形(oval)、线条(line)、圆环(ring)等,默认形状是矩形。line 和 ring 时必须要通过 < stroke > 标签来指定线的宽度和颜色信息。

一、使用简介

其 XML 根元素是 <shape>,可以指定以下属性:
android:shape=["rectangle"|"oval"|"line"|"ring"]

格式如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <!--定义几何图形四个角的弧度-->
    <corners
        android:radius="8dp"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer"/>
    <!--定义使用渐变色填充-->
    <gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear"|"radial"|"sweep"]
        android:usesLevel=["true"|"false"] />
    <!--定义内边距-->
    <padding
        android:bottom="integer"
        android:left="integer"
        android:right="integer"
        android:top="integer" />
    
    <!--定义几何形状大小-->
    <size
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer"/>
    
    <!--定义使用单种颜色填充-->
    <solid android:color="color" />    
    
    <!--定义为几何形状绘制边框-->
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer"/>
</shape>

二、实例

1.圆角背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充色 -->
    <solid android:color="@color/white" />
    <!--描边、 线的宽度,颜色 -->
    <!--<stroke-->
    <!--android:width="1dp"-->
    <!--android:color="@color/colorMainColor" />-->
    <!-- 矩形圆角半径 -->
    <corners android:radius="10dp" />
</shape>

2.带阴影的圆角背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners
        android:bottomLeftRadius="25dp"
        android:bottomRightRadius="25dp"
        android:radius="8dp"
        android:topLeftRadius="25dp"
        android:topRightRadius="25dp" />
    <!--    <gradient
            android:angle="45"
            android:endColor="#72cb60"
            android:startColor="#72cb60" />-->
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
    <size android:width="100dp" />
    <solid android:color="#72cb60" />
    <stroke
        android:width="2dp"
        android:color="#dcdcdc" />
</shape>