Android开发 shape详解

来源:互联网 发布:网络喷子 英文 编辑:程序博客网 时间:2024/06/16 13:05

在Android开发中,经常需要给很多ui设置背景,这时候就一般就需要用到shape了,为了方便以后的不时之需,同时也方便各大读者,这里总结一下shape的基本属性,及常用到shape形状,以后遇到直接copy就行了。


先来一张思维导图说明基本属性:


第一部分:shape的基本属性

1、corners (圆角)

这里便是shape的四个角的角度,它只适用于矩形shape,这里的角度是指圆角的程度

  <corners      android:radius="integer"      //四个圆角半径,radius属性不能和下面4个属性共用,优先级比较低,如果同时设置了下面4个属性则会被覆盖    android:topLeftRadius="integer"   //左上角的圆角半径    android:topRightRadius="integer"  //右上角的圆角半径    android:bottomLeftRadius="integer"    //左下角的圆角半径    android:bottomRightRadius="integer" />    //右下角的圆角半径

2、gradient(渐变色填充)

它和solid的属性是互相排斥的,毕竟,solid表示的是纯色填充,而gradient则表示的是渐变效果
 <gradient    android:type=["linear" | "radial" | "sweep"] //共有3中渐变类型,linear线性渐变/radial放射渐变/sweep扫描式渐变    android:angle="integer" //渐变角度,默认为0,其值必须为45的倍数,0为从左到右,90为从上到下    android:centerX="float" //渐变中心X的相当位置,范围为0~1    android:centerY="float" //渐变中心Y的相当位置,范围为0~1    android:startColor="color" //渐变开始点的颜色    android:centerColor="color" //渐变中间点的颜色,在开始与结束点之间    android:endColor="color" //渐变结束点的颜色    android:gradientRadius="float" //渐变的半径,只有当渐变类型为radial时才能使用    android:useLevel=["true" | "false"] /> //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果

3、padding(四个位置的内边距)

一般控件都有的属性,这里不介绍

4、size(定义图形的大小)

一般控件都有的属性,这里不介绍

5、solid(单色填充)

很简单,只有一个属性,只能填充一种颜色
    <solid        android:color="@color/yellow"        />

6、stroke(为图形绘制边框)

    <stroke     android:width="dimension"//描边的宽度    android:color="color" //描边的颜色    // 以下两个属性设置虚线    android:dashWidth="dimension" //虚线的宽度,值为0时是实线    android:dashGap="dimension" /> //虚线的间隔

第二部分:shape的四种形状(实战演练)

1、rectangel 矩形(默认)

(1)矩形

a、填充

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <!-- rectangle表示为矩形 -->    <!-- 里面填充的颜色 -->    <solid android:color="@color/green" /></shape>
b、不填充

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <stroke        android:width="1dp"        android:color="@color/green" /></shape>
c、虚线
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <stroke        android:width="1dp"        android:color="@color/green"        android:dashGap="2dp"        android:dashWidth="8dp" /></shape>

(2)圆角

a、填充


<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!-- rectangle表示为矩形 --><!-- 里面填充的颜色 --><solid android:color="@color/green" /><!-- android:radius 圆角的半径,数值越大,圆角越圆--><corners android:radius="9dp" /></shape>
b、不填充

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <stroke        android:width="1dp"        android:color="@color/green" />    <corners android:radius="9dp" /></shape>
c、虚线

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <stroke        android:width="1dp"        android:color="@color/green"        android:dashGap="2dp"        android:dashWidth="8dp" />    <corners android:radius="9dp" /></shape>

(3)局部圆角


<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <!-- rectangle表示为矩形 -->    <!-- 里面填充的颜色 -->    <solid android:color="@color/green" />    <!-- android:radius 圆角的半径,数值越大,圆角越圆-->    <corners        android:bottomLeftRadius="21dp"        android:bottomRightRadius="21dp"        android:topLeftRadius="0dp"        android:topRightRadius="0dp" /></shape>
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <solid android:color="@color/green" />    <corners        android:bottomLeftRadius="21dp"        android:bottomRightRadius="0dp"        android:topLeftRadius="0dp"        android:topRightRadius="21dp" /></shape>

2、oval 椭圆


3、line 线形


4、ring 环形



原创粉丝点击