1,示例

它们的代码如下:

shape_oval.xml

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:endColor="#fa1621"
android:gradientRadius="50"
android:startColor="#f793ac"
android:type="sweep"/>
<size
android:width="72dp"
android:height="72dp"/>
<!--<solid android:color="#e3e3e3"/>-->
</shape>

shape_rectangle.xml

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:endColor="#fa1621"
android:gradientRadius="50"
android:startColor="#f793ac"
android:type="radial"/>
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
<size android:width="72dp" android:height="72dp" />
</shape>

shape_ring.xml

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:thickness="2dp"
android:innerRadius="30dp"
android:useLevel="false">
<!--<gradient-->
<!--android:endColor="#fa1621"-->
<!--android:gradientRadius="50"-->
<!--android:startColor="#f793ac"-->
<!--android:type="radial"/>-->
<!--<padding-->
<!--android:left="10dp"-->
<!--android:top="10dp"-->
<!--android:right="10dp"-->
<!--android:bottom="10dp" />-->
<stroke
android:width="1dp"
android:color="color"
android:dashWidth="1dp"
android:dashGap="1dp" />
<solid android:color="#f8a17e" />
<size android:width="72dp" android:height="72dp"/>
</shape>

shape_line.xml

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<!-- 显示一条虚线,破折线的宽度为 dashWith,破折线之间的空隙的宽度为 dashGap,当 dashGap=0dp 时,为实线 -->
<stroke
android:dashGap="3dp"
android:dashWidth="2dp"
android:width="1dp"
android:color="#777777" /> <!-- 虚线的高度 -->
<size android:height="2dp" />
</shape>

ring_circle.xml

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="5555"
android:thicknessRatio="2"
android:useLevel="false">
<!--android:innerRadiusRatio="5555"含义:按比值计算内圆半径,整个图形宽度/该值,就是36dp/5555=0.006480648dp-->
<!--android:thicknessRatio="2"含义:按比值计算外环厚度。整个图形宽度/该值 就是36dp/2 = 18dp -->
<solid android:color="#ff0000" />
<size android:width="36dp" android:height="36dp"/>
</shape>

如图:

oval_circle.xml

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size
android:width="16dp"
android:height="60dp"/>
<solid android:color="#e3e3e3" />
</shape>

注意,虽然shape="oval",且width=16dp,height=60dp,但是在使用的时候将控件比如imageview或textview的高宽设置成一样就是正圆。

2,语法

    其中android:shape有4个类型,rectangle 矩形,oval椭圆,line线条,ring环形。

  <shape>有6个子元素。<corners>圆角,<gradient>渐变颜色,<padding>内边距,<size>大小,<solid>填充,<stoke>描边。

 <?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "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="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<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>

3,各元素属性详解

3.1<shape>形状

属性

含义

类型

取值

备注

android:shape

形状

枚举

rectagle矩形|正方形,oval椭圆|正圆,

line水平直线,ring环形

画line时,用<stroke>定义线的高度

android:innerRadius

内半径

尺寸

内圆的半径。一个尺寸值(dip等等)或者一个尺寸资源。

只在android:shape="ring"有效

注意,android:innerRadiusRatio

和 android:thicknessRatio

指定的都是一个被除数,比值。

android:thickness

外环厚度

尺寸

外环的厚度,是一个尺寸值或尺寸的资源

android:innerRadiusRatio

内圆半径比值

float

按比值计算内圆半径,整个图形宽度/该值

例如,如果android:innerRadiusRatio="5",

则,内圆半径=整个图形宽度的宽度/5。

这个值会被android:innerRadius重写。默认值是9

见上面 ring_circle.xml

android:thicknessRatio

外环厚度比值

float

按比值计算外环厚度。整个图形宽度/该值

例如,如果android:thicknessRatio="2",

则,外环厚度=整个图形宽度/2。

这个值是被android:innerRadius重写,默认值是3。

见上面ring_circle.xml

android:useLevel

是否用作图层

bool

如果用在 LevelListDrawable里,那么就是true。

如果通常不出现则为false。

3.2<size>大小

属性

含义

类型

取值

备注

android:width

长度

尺寸

android:height

高度

尺寸

3.3<padding>内边距

属性

含义

类型

取值

备注

android:left

左边距

尺寸

android:top

上边距

尺寸

android:right

右边距

尺寸

android:bottom

下边距

尺寸

3.4<corners>圆角

属性

含义

类型

取值

备注

android:radius

四角的弧度

尺寸

设置0dp,无圆角

设置>1dp, 有圆角

值越大角越圆

只适用于rectagle

android:topRightRadius

右上角弧度

尺寸

android:bottomLeftRadius

右下角弧度

尺寸

android:topLeftRadius

左上角弧度

尺寸

android:bottomRightRadius

左下角弧度

尺寸

3.5<stroke> 描边

属性

含义

类型

取值

备注

android:width

描边的宽度

尺寸

android:color

描边的颜色

颜色

android:dashWidth

边框分段长度

尺寸

2属性要同时设置

android:dashGap

边框分段间隙

尺寸

3.6<solid>填充

属性

含义

类型

取值

备注

android:color

填充的颜色

颜色

3.7<gradient> 渐变颜色

属性

含义

类型

取值

android:startColor

渐变开始颜色

颜色

android:centerColor

渐变中间颜色

颜色

android:endColor

渐变结束颜色

颜色

android:angle

渐变角度

整字

0,默认值表示从左到右渐变90表示从下到上渐变,

取值必须是45的倍数

android:centerX

渐变中心相对于x轴位置

小数

0-1.0

android:centerY

渐变中心相对于y轴位置

小数

0-1.0

android:type

渐变的样式

枚举

linear线性渐变radial环形渐变sweep

android:gradientRadius

渐变半径

小数

android:type="radial"必填

android:useLevel

是否用作图层

布尔

true用于LevelListDrawable

Shape Drawable Resources的更多相关文章

  1. layer-list shape drawable 层叠背景 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. Android Drawable - Shape Drawable使用详解(附图)

    TIPS shape图形 –简单介绍 shape图形 –如何画? shape图形 –参数详细解析 shape图形 –如何用? shape图形 –实际开发应用场景 shape图形简单介绍 用xml实现一 ...

  3. [安卓]应用程序资源(App Resources)

    谷歌推荐我们,在开发安卓系统应用程序的时候,要把资源从代码中分离出来,这样便于我们单独维护它们.采取分离的资源设计,我们还可以提供可选资源,支持特定的设备配置譬如不同的语言或屏幕尺寸,随着越来越多的A ...

  4. Drawable(1)各种Drawable Resource介绍

    简介 Drawable Resources(可绘资源) 是一系列可以在屏幕上被绘制的资源文件,它不只是图片,可以是 xml文件,在xml文件中配置各种绘制参数. 常见Drawable Resource ...

  5. 有关Color和Drawable你所不知道的那些内容

    Android开发中,我们经常会用到Color或Drawable,有时他们是可以混用的,有时却有严格的区别. Drawable 体系结构 Drawable是可绘制物件的一般抽象.与View不同,Dra ...

  6. android:shape属性详解

    这一类的shape定义在xml中 file location: res/drawable/filename.xml The filename is used as the resource ID.(这 ...

  7. 系统的Drawable(一)

    系统的Drawable(一) 学习自 <Android 开发艺术探索> <官方文档> https://www.cnblogs.com/popfisher/p/6238119.h ...

  8. [转]android中drawable资源的解释及例子

    原文链接:         http://blog.csdn.net/wode_dream/article/details/38584693 文章中的内容参考Dev Guide中的Drawable R ...

  9. Drawable资源的初步使用

    刚開始接触到Android的时候,看到类似以下的一个Button: 当时感觉这种button有点像Material Design风格.真的以为是裁剪好的图片,好奇心驱使我上网查找实现的方法,原来不是裁 ...

随机推荐

  1. abs 暴力

    Given a number x, ask positive integer y≥2y≥2, that satisfy the following conditions: 1. The absolut ...

  2. P1028 数的计算 洛谷

    https://www.luogu.org/problem/show?pid=1028 题目描述 我们要求找出具有下列性质数的个数(包含输入的自然数n): 先输入一个自然数n(n<=1000), ...

  3. ecstore

  4. 无线安全课堂:手把手教会你搭建伪AP接入点

    概述 *本文假设读者对设置伪AP接入点以及Apache配置有足够了解. 在本攻击场景中,我们将使用到alfa无线网卡以及用于网络访问的以太网连接(虚拟机环境下,物理机下无需此配置).你也可以使用虚拟接 ...

  5. 【C++/数据结构】单链表的基本操作

    #pragma once #ifndef _CLIST_H_ #define _CLIST_H_ #include <iostream> #include <assert.h> ...

  6. PS如何绘制虚线圆

    1 绘制一个圆的路径   2 选择铅笔工具,然后点击"画笔笔尖形状",选好笔尖的直径和间距(不同的直径对应不同的间距,没有标准数值,自己推拉滑动条就可以了)   3 在路径选项卡, ...

  7. PC_excel完毕一列英文小写变大写

    原创作品,出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...

  8. react 项目实战(三)表单验证

    我们需要记录每一个字段当前的有效状态,有效时隐藏错误信息,无效时显示错误信息. 而这个有效/无效,可以在表单值改变的时候进行判断. 我们对/src/pages/UserAdd.js进行修改: 首先修改 ...

  9. SpringMVC DispatcherServlet初始化过程

    先来上一张类的结构图: 图里仅仅画了跟初始化相关的方法. 首先DispatcherServlet也是一个Servlet,初始化从init()方法開始. 以下就详细看看ini()是怎么实现的吧. 1.S ...

  10. iOS开发——基础篇——get和post请求的区别

    HTTP 定义了与服务器交互的不同方法,最常用的有4种,Get.Post.Put.Delete,如果我换一下顺序就好记了,Put(增),Delete(删),Post(改),Get(查),即增删改查,下 ...