Android 资源简介(三)ShapeDrawable

ShapeDrawable 用于定义一个基本的几何图形(如矩形、圆形、直线等),定义 ShapeDrawable 的 XML 文件的根元素是
<shape> 元素,该元素可以指定如下属性:
android:shape=[“rectangle”|”oval”|”line”|”ring”]: 指定定义哪种类型的几何图形。

定义 ShapeDrawable 对象的完整语法格式如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line|rectangle|oval|ring">

    <!--定义几何图形的四个角的弧度-->
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />

    <!--定义渐变色填充-->
    <gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="color"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type="linear|radial|sweep"
        android:useLevel="boolean" />

    <!--定义几何形状的内边距-->
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />

    <!--定义几何形状的大小-->
    <size
        android:width="integer"
        android:height="integer" />

    <!--定义使用单种颜色填充-->
    <solid
        android:color="color" />

    <!--定义为几何形状绘制边框-->
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />

</shape>

下面是一个简单的使用示例,首先是三个自定义的 Drawable 文件:

自定义的 Drawable 文件 test_shape_01.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#fff" />
    <padding
        android:bottom="7dp"
        android:left="7dp"
        android:right="7dp"
        android:top="7dp" />
    <stroke
        android:width="3dip"
        android:color="#FFFF00" />
</shape>

自定义的 Drawable 文件 test_shape_02.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="45"
        android:endColor="#80FF00FF"
        android:startColor="#FFFF0000" />

    <padding
        android:bottom="7dp"
        android:left="7dp"
        android:right="7dp"
        android:top="7dp" />

    <corners android:radius="8dp" />
</shape>

自定义的 Drawable 文件 test_shape_02.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <gradient
        android:angle="45"
        android:endColor="#FFFF00"
        android:startColor="#0000FF"
        android:type="sweep" />

    <padding
        android:bottom="7dp"
        android:left="7dp"
        android:right="7dp"
        android:top="7dp" />

    <corners android:radius="8dp" />
</shape>

然后是主布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorGray"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/test_shape_01"
        android:layout_marginTop="20dp"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/test_shape_02"
        android:layout_marginTop="20dp"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/test_shape_03"
        android:layout_marginTop="20dp"
        />
</LinearLayout>

本示例无须特别处理 Activity 文件,只需要加载主布局文件即可运行,运行效果如下图所示:

《Android 资源简介(三)ShapeDrawable》 运行效果

参考文献:《疯狂Android讲义(第2版)》

    原文作者:赵者也
    原文地址: https://www.jianshu.com/p/312053a0ac32
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞