Android: Shape 的使用

1,528 阅读1分钟

Android Shape 的使用

在Android开发中,我们可以使用shape定义各种各样的形状,也可以定义一些图片资源。相对于传统 图片来说,使用shape可以减少资源占用,减少安装包大小,还能够很好地适配不同尺寸的手机。

1. shape属性

  • shape 属性基本语法示例:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--    圆角属性-->
    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:radius="5dp"
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp" />
    <!--    渐变属性-->
    <gradient
        android:angle="-45"
        android:centerColor="#ff0099"
        android:centerX="20"
        android:centerY="30"
        android:endColor="#80FF00"
        android:gradientRadius="45dp"
        android:startColor="#FF0089BD"
        android:type="linear"
        android:useLevel="false" />
    <!--    边距属性-->
    <padding
        android:bottom="12dp"
        android:left="10dp"
        android:right="15dp"
        android:top="10dp" />
    <!--大小属性-->
    <size
        android:width="200dp"
        android:height="200dp" />
    <!--    填充属性-->
    <!--    <solid android:color="#ffff9d"/>-->
    <!--     描边属性-->
    <stroke
        android:width="2dp"
        android:color="#dcdcdc" />
</shape>

2. 基本属性

Shape可以定义控件的一些展示效果,例如圆角,渐变,填充,描边,大小,边距; shape 子标签就可以实现这些效果, shape 子标签有下面几个属性:corners,gradient,padding,size,solid,stroke:

  • corners(圆角)是用来字义圆角
<corners //定义圆角
     android:radius="10dp" //全部的圆角半径;
     android:topLeftRadius="5dp" //左上角的圆角半径;
     android:topRightRadius="5dp" //右上角的圆角半径;
     android:bottomLeftRadius="5dp" //左下角的圆角半径;
     android:bottomRightRadius="5dp" /> //右下角的圆角半径。
  • solid(填充色)是用以指定内部填充色;
  <solid android:color="#ffff00"/> //内部填充色
  • gradient(渐变)用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式;
<gradient
     android:type=["linear" | "radial" | "sweep"] //共有3中渐变类型,线性渐变
     (默认)/放射渐变/扫描式渐变;
     android:angle="90" //渐变角度,必须为45的倍数,0为从左到右,90为从上到下;
     android:centerX="0.5" //渐变中心X的相当位置,范围为01;
     android:centerY="0.5" //渐变中心Y的相当位置,范围为01;
     android:startColor="#24e9f2" //渐变开始点的颜色;
     android:centerColor="#2564ef" //渐变中间点的颜色,在开始与结束点之间;
     android:endColor="#25f1ef" //渐变结束点的颜色;
     android:gradientRadius="5dp" //渐变的半径,只有当渐变类型为radial时才能使用;
     android:useLevel="false" /> //使用LevelListDrawable时就要设置为true。设为
     false时才有渐变效果。     
  • stroke(描边)是描边属性,可以定义描边的宽度,颜色,虚实线等;
<stroke
    android:width="1dp" //描边的宽度
    android:color="#ff0000" //描边的颜色
    // 以下两个属性设置虚线
    android:dashWidth="1dp" //虚线的宽度,值为0时是实线
    android:dashGap="1dp" />//虚线的间隔
  • padding(内边距)是用来定义内部边距
<padding
    android:left="10dp" //左内边距;
    android:top="10dp" //上内边距;
    android:right="10dp" //右内边距;
    android:bottom="10dp" /> //下内边距。
  • size(大小)标签是用来定义图形的大小的
<size
    android:width="50dp" //宽度
    android:height="50dp" />// 高度

3. 特殊属性

Shape可以定义当前Shape的形状的,比如矩形,椭圆形,线形和环形;这些都是通过 shape 标签属性来定义的, shape 标签有下面几个属性:rectangle,oval,line,ring:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   //shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)环形(ring)
   android:shape=["rectangle" | "oval" | "line" | "ring"] 
   //下面的属性只有在android:shape="ring"时可用:
   android:innerRadius="10dp" // 内环的半径;
   android:innerRadiusRatio="2" // 浮点型,以环的宽度比率来表示内环的半径;
   android:thickness="3dp" // 环的厚度;
   android:thicknessRatio="2" // 浮点型,以环的宽度比率来表示环的厚度;
   android:useLevel="false"> // boolean值,如果当做是LevelListDrawable使用时值为
   true,否则为false。   
</shape>
  • rectangle(矩形)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimary"/>
</shape>
  • oval(椭圆)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/colorPrimary"/>
    <size android:height="100dp"
          android:width="100dp"/>
</shape>
  • line(线)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="line">
      <stroke
            android:width="1dp"
            android:color="@color/colorAccent"
            android:dashGap="3dp"//虚线间距
            android:dashWidth="4dp"/>//虚线宽度
      <size android:height="3dp"/>
</shape>
  • ring(圆环)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false"
    android:innerRadius="20dp" // 内环的半径
    android:thickness="10dp"> // 圆环宽度
    <!--useLevel需要设置为false-->
    <solid android:color="@color/colorAccent"/>
</shape>

4.shape用法

  • 在res/drawable下新建 shape_text.xml 文件;
//参考 1.shape属性
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--    圆角属性-->
    <corners ... />
    <!--    渐变属性-->
    <gradient ... />
    <!--    边距属性-->
    <padding ... />
    <!--大小属性-->
    <size ... />
    <!--     描边属性-->
    <stroke ... />
</shape>
  • 在布局中引用 shape_text.xml 文件;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_text"
        android:text="Shape测试"
        android:textColor="@android:color/black"
        android:textSize="15sp" />
</LinearLayout>