32、详解Android shape的使用方法(转载)
0、java绘制shape
在官方API介绍中:
ShapeDrawable:This object can be defined in an XML file with the <shape>
element(这个对象可以用<shape>元素在xml文件中定义)
GradientDrawable:This object can be defined in an XML file with the <shape>
element(这个对象可以用<shape>元素在xml文件中定义)
[父节点] shape -- ShapeDrawable
[子节点] gradient --
[子节点] padding --
[子节点] corners -- setCornerRadius 、setCornerRadii
[子节点] solid --
[子节点] stroke -- setStroke
[子节点] size -- setSize
1、概述
gradient -- 对应颜色渐变。 startcolor、endcolor就不多说了。 android:angle 是指从哪个角度开始变。
solid -- 填充。
stroke -- 描边。
corners -- 圆角。
padding -- 定义内容离边界的距离。 与android:padding_left、android:padding_right这些是一个道理。
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] //共有4种类型,矩形(默认)/椭圆形/直线形/环形
// 以下4个属性只有当类型为环形时才有效
android:innerRadius="dimension" //内环半径
android:innerRadiusRatio="float" //内环半径相对于环的宽度的比例,比如环的宽度为50,比例为2.5,那么内环半径为20
android:thickness="dimension" //环的厚度
android:thicknessRatio="float" //环的厚度相对于环的宽度的比例
android:useLevel="boolean"> //如果当做是LevelListDrawable使用时值为true,否则为false. <corners //定义圆角
android:radius="dimension" //全部的圆角半径
android:topLeftRadius="dimension" //左上角的圆角半径
android:topRightRadius="dimension" //右上角的圆角半径
android:bottomLeftRadius="dimension" //左下角的圆角半径
android:bottomRightRadius="dimension" /> //右下角的圆角半径 <gradient //定义渐变效果
android:type=["linear" | "radial" | "sweep"] //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变
android:angle="integer" //渐变角度,必须为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时才有渐变效果 <padding //内部边距
android:left="dimension"
android:top="dimension"
android:right="dimension"
android:bottom="dimension" /> <size //自定义的图形大小
android:width="dimension"
android:height="dimension" /> <solid //内部填充颜色
android:color="color" /> <stroke //描边
android:width="dimension" //描边的宽度
android:color="color" //描边的颜色
// 以下两个属性设置虚线
android:dashWidth="dimension" //虚线的宽度,值为0时是实线
android:dashGap="dimension" /> //虚线的间隔
</shape>
2、圆角矩形,扫描式渐变
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false" > <corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp" /> <gradient
android:type="sweep"
android:endColor="@android:color/holo_blue_bright"
android:startColor="@android:color/holo_green_dark"
android:centerColor="@android:color/holo_blue_dark"
android:useLevel="false" /> <size android:width="60dp" android:height="60dp" />
</shape>
2、 圆形,线性渐变
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="false" > <gradient
android:type="linear"
android:angle="45"
android:startColor="@android:color/holo_green_dark"
android:centerColor="@android:color/holo_blue_dark"
android:endColor="@android:color/holo_red_dark"
android:useLevel="false" /> <size android:width="60dp" android:height="60dp" /> <stroke android:width="1dp"
android:color="@android:color/white" /> </shape>
3、虚线
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" > <size android:width="60dp"
android:height="60dp" /> <stroke android:width="2dp"
android:color="@android:color/holo_purple"
android:dashWidth="10dp"
android:dashGap="5dp" />
</shape>
4、 环形,放射型渐变
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false"
android:innerRadius="40dp"
android:thickness="3dp"> <gradient android:type="radial"
android:gradientRadius="150"
android:centerY="0.1"
android:centerX="0.2"
android:startColor="@android:color/holo_red_dark"
android:centerColor="@android:color/holo_green_dark"
android:endColor="@android:color/white" /> <size android:width="90dp"
android:height="90dp" /> </shape>
5、demo
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TestShapeButton"
- android:background="@drawable/button_selector"
- />
button_selector.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <selector
- xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true" >
- <shape>
- <!-- 渐变 -->
- <gradient
- android:startColor="#ff8c00"
- android:endColor="#FFFFFF"
- android:type="radial"
- android:gradientRadius="50" />
- <!-- 描边 -->
- <stroke
- android:width="2dp"
- android:color="#dcdcdc"
- android:dashWidth="5dp"
- android:dashGap="3dp" />
- <!-- 圆角 -->
- <corners
- android:radius="2dp" />
- <padding
- android:left="10dp"
- android:top="10dp"
- android:right="10dp"
- android:bottom="10dp" />
- </shape>
- </item>
- <item android:state_focused="true" >
- <shape>
- <gradient
- android:startColor="#ffc2b7"
- android:endColor="#ffc2b7"
- android:angle="270" />
- <stroke
- android:width="2dp"
- android:color="#dcdcdc" />
- <corners
- android:radius="2dp" />
- <padding
- android:left="10dp"
- android:top="10dp"
- android:right="10dp"
- android:bottom="10dp" />
- </shape>
- </item>
- <item>
- <shape>
- <solid android:color="#ff9d77"/>
- <stroke
- android:width="2dp"
- android:color="#fad3cf" />
- <corners
- android:topRightRadius="5dp"
- android:bottomLeftRadius="5dp"
- android:topLeftRadius="0dp"
- android:bottomRightRadius="0dp"
- />
- <padding
- android:left="10dp"
- android:top="10dp"
- android:right="10dp"
- android:bottom="10dp" />
- </shape>
- </item>
- </selector>
运行效果如下图:
一般状态:
获得焦点状态:
按下状态:
6、官方资料
<shape>
Basic method for drawing shapes via XML.
Attributes
Name | Type | Default | Description |
---|---|---|---|
visible | boolean | parent|true | Determines if drawable is visible. |
shape | enum (rectangle, oval, line, ring) | rectangle | Determines the shape: rectangle (shape is a rectangle, possibly with rounded corners); oval (shape is an ellipse); line (shape is a line); ring (shape is a ring). |
innerRadiusRatio | float | 3.0 | Only valid if shape == 'ring'. Inner radius of the ring expressed as a ratio of the ring's width. For instance, if innerRadiusRatio=3, then the inner radius equals the ring's width divided by 3. This value is ignored if innerRadius is defined. |
innerRadius | float | -1 | Only valid if shape == 'ring'. Inner radius of the ring. When defined, innerRadiusRatio is ignored. When undefined, innerRadiusRatio's default is used. |
thicknessRatio | float | 9.0 | Only valid if shape == 'ring'. Thickness of the ring expressed as a ratio of the ring's width. For instance, if thicknessRatio=9, then the thickness equals the ring's width divided by 9. This value is ignored if thickness is defined. Default value is 9. |
thickness | float | -1 | Only valid if shape == 'ring'. Thickness of the ring. When defined, thicknessRatio is ignored. When undefined, thicknessRatio's default is used. |
useLevel | boolean | true | Only valid if shape == 'ring'. Allows one to draw only part of the ring (arc-wise), by modifying the drawable's level. This setting only makes sense in context of a <level-list> (LevelListDrawable). |
Children
Element | Description |
---|---|
<size> | Determines the size of the shape. |
<gradient> | Adds a background gradient to the shape. |
<solid> | Adds a solid background color to the shape. Overides gradient element. |
<stroke> | Adds a border to the shape. |
<corners> | Adds rounded corners to the shape. |
<padding> | The padding for the content within this drawable. (Does not pad graphics in any way.) |
<size>
Determines the size of the shape.
Attributes
Name | Type | Default | Description |
---|---|---|---|
width | dimension | -1 | Width of the shape. |
height | dimension | -1 | Height of the shape. |
<gradient>
Adds a background gradient to the shape.
Attributes
Name | Type | Default | Description |
---|---|---|---|
startColor | color | 0 | The color at the start of the gradient. |
centerColor | color | 0 | The color in the center of the gradient. Optional; if not included, there is no center color. |
endColor | color | 0 | The color at the end of the gradient. |
type | enum (linear, radial, sweep) | linear | Determines the type of gradient. |
centerX | float|fraction | .5 | Determines the location of the centerColor. Ranges from 0 to 1. Ignored if centerColor is undefined. |
centerY | float|fraction | .5 | Determines the location of the centerColor. Ranges from 0 to 1. Ignored if centerColor is undefined. |
angle | float | 0 | Only valid if type == 'linear'. Determines the angle of a linear gradient. Must be a multiple of 45 degrees. |
gradientRadius | float|fraction | N/A | Only valid if type == 'radial' or 'sweep'. Required if type == 'radial'. Determines the radius of the gradient. |
useLevel | boolean | false | Determines the amount of the gradient to be drawn, based on the level of the shape. Affects all three gradient types. |
<solid>
Adds a solid background color to the shape. Overides gradient element.
Attributes
Name | Type | Default | Description |
---|---|---|---|
color | color | 0 | The color of the background. |
<stroke>
Adds a border to the shape.
Attributes
Name | Type | Default | Description |
---|---|---|---|
width | dimension | 0 | The width of the stroke. |
color | color | 0 | The color of the stroke. |
dashWidth | dimension | 0 | The width of each dash. Ignored unless dashGap is also defined. |
dashGap | dimension | 0 | The width of gaps between eahc dash. Ignored unless dashWidth is also defined. |
<corners>
Adds rounded corners to the shape.
Attributes
Name | Type | Default | Description |
---|---|---|---|
radius | dimension | 0 | The radius of every corner. |
topLeftRadius | dimension | radius | Determines the radius of the top left corner. Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes. |
topRightRadius | dimension | radius | Determines the radius of the top right corner. Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes. |
bottomLeftRadius | dimension | radius | Determines the radius of the bottom left corner (buggy; is actually bottom right corner). Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes. |
bottomRightRadius | dimension | radius | Determines the radius of the bottom right corner (buggy; is actually bottom left corner). Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes. |
<padding>
The padding for the content within this drawable. (Does not pad graphics in any way.)
Attributes
Name | Type | Default | Description |
---|---|---|---|
left | dimension | 0 | Left padding. |
top | dimension | 0 | Top padding. |
right | dimension | 0 | Right padding. |
bottom | dimension | 0 | Bottom padding. |
32、详解Android shape的使用方法(转载)的更多相关文章
- (转载)实例详解Android快速开发工具类总结
实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...
- 详解Android首选项框架ListPreference
详解Android首选项框架ListPreference 原文地址 探索首选项框架 在深入探讨Android的首选项框架之前,首先构想一个需要使用首选项的场景,然后分析如何实现这一场景.假设你正在编写 ...
- 详解Android Activity---启动模式
相关的基本概念: 1.任务栈(Task) 若干个Activity的集合的栈表示一个Task. 栈不仅仅只包含自身程序的Activity,它也可以跨应用包含其他应用的Activity,这样有利于 ...
- Android Binder IPC详解-Android学习之旅(96)
linux内存空间与BInder Driver Android进程和linux进程一样,他们只运行在进程固有的虚拟空间中.一个4GB的虚拟地址空间,其中3GB是用户空间,1GB是内核空间 ,用户空间是 ...
- 分区工具parted的详解及常用分区使用方法【转】
来源:http://blog.51cto.com/zhangmingqian/1068779 分区工具parted的详解及常用分区使用方法 一. parted的用途及说明 概括使用说明 ...
- 图文详解 Android Binder跨进程通信机制 原理
图文详解 Android Binder跨进程通信机制 原理 目录 目录 1. Binder到底是什么? 中文即 粘合剂,意思为粘合了两个不同的进程 网上有很多对Binder的定义,但都说不清楚:Bin ...
- 详解Android Activity启动模式
相关的基本概念: 1.任务栈(Task) 若干个Activity的集合的栈表示一个Task. 栈不仅仅只包含自身程序的Activity,它也可以跨应用包含其他应用的Activity,这样有利于 ...
- 个人用户永久免费,可自动升级版Excel插件,使用VSTO开发,Excel催化剂安装过程详解及安装失败解决方法
因Excel催化剂用了VSTO的开发技术,并且为了最好的用户体验,用了Clickonce的布署方式(无需人工干预自动更新,让用户使用如浏览器访问网站一般,永远是最新的内容和功能).对安装过程有一定的难 ...
- 详解android:scaleType属性
详解android:scaleType属性 转自:http://blog.csdn.net/encienqi/article/details/7913262 http://juliaailse. ...
随机推荐
- 【Android开发笔记】程序崩溃异常总结
广播注册相关(broadcastReceiver) 没有注册广播就注销广播 注册广播但未注销广播 注册广播后重复注销广播 解决办法: 添加一个布尔变量,注册广播后为true,若为true在执行注销,注 ...
- 【extjs6学习笔记】1.11 初始: config
Ext JS有一个名为config的功能. 该配置允许您使用默认值声明公共属性,这些属性将被其他类成员完全封装. 通过config声明的属性将自动获取get()和set()方法,如果类没有定义这些方法 ...
- Mybatis-Spring整合Spring
因为 MyBatis 用 SqlSessionFactory 来创建 SqlSession ,SqlSessionFactoryBuilder 创建 SqlSessionFactory ,而在 Myb ...
- intelij idea相关笔记--持续更新
一.快捷键: Ctrl+F 文件内查找 Ctrl+Shift+F 全局查找 Ctrl+Shift+N 查找文件 Ctrl+Alt+← 返回上一步 Ctrl+Alt+→ 返回下一步 二.编译相关: 如果 ...
- 弹出页面第一次加载可以生成table和方法的绑定,第二次点击进来不能生成table和方法的帮定
问题原因: 弹出页面的写法是每次点击都会在原有页面基础之上新添加一个将其覆盖,原有页面不关闭.我用的生成table和点击事件的绑定是id选择器.页面中只绑定第一次的页面,第二次的页面作用不上. 解决: ...
- IOS 拖拽事件(手势识别)
@interface NJViewController () @property (weak, nonatomic) IBOutlet UIView *customView; @end @implem ...
- java +selenuim使用js显示控件
操作selenium控件时,往往需要有些控件提前显示,特别是后台的一些控件,或者需要使用鼠标显示下拉的操作,有时鼠标悬停,在点击不怎么好使,就可以依靠js方式,提前让控件显示. 1.使用style的方 ...
- hdu-1162 Eddy's picture---浮点数的MST
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1162 题目大意: 给n个点,求MST权值 解题思路: 直接prim算法 #include<bi ...
- 同余问题(一)——扩展欧几里得exgcd
前言 扩展欧几里得算法是一个很好的解决同余问题的算法,非常实用. 欧几里得算法 简介 欧几里得算法,又称辗转相除法. 主要用途 求最大公因数\(gcd\). 公式 \(gcd(a,b)=gcd(b,a ...
- NOIP2018赛前停课集训记(10.24~11.08)
前言 为了不久之后的\(NOIP2018\),我们的停课从今天(\(Oct\ 24th\))起正式开始了. 本来说要下周开始的,没想到竟提早了几天,真是一个惊喜.毕竟明天有语文考试.后天有科学考试,逃 ...