赞
踩
一、shape简介
是一种在XML文件中定义的通用形状,在 Android
开发中,可以使用 shape
定义各种各样的形状,也可以定义一些图片资源,相对于传统图片来说,使用 shape
可以减少资源占用,减少安装包大小,还能够很好地适配不同尺寸的手机。
文件位置:res / drawable / filename.xml,文件名一般用资源ID。
使用:
在 Java 中:R.drawable.filename
在 XML 中:@[package:]drawable/filename
二、shape使用
1、testshape.xml 配置
- <?xml version="1.0" encoding="utf-8"?>
- <shape
- xmlns:android="http://schemas.android.com/apk/res/android" >
- <corners android:radius="10dp"/>
- <solid android:color="#00ff00"/>
- <gradient
- android:type="sweep"
- android:angle="45"
- android:startColor="#0000ff"
- android:endColor="#ff00ff"/>
- <stroke
- android:color="#0ff0ff"
- android:width="30dp"
- android:dashGap="10dp"
- android:dashWidth="10dp"/>
- <size
- android:height="500dp"
- android:width="500dp"/>
-
- <padding
- android:bottom="10dp"
- android:top="10dp"
- android:left="10dp"
- android:right="10dp"/>
-
- </shape>
2、主activity.xml 中进行引用
- <TextView
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:text="hello world"
- android:background="@drawable/testshape"
- android:id="@+id/testShape"/>
三、shape属性说明
shape子标签属性可以定义控件的一些展示效果,例如 圆角
、渐变
、填充
、描边
、大小
、边距。
1、android:dither="false|true" //将在位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕)启用位图的抖动;值为“false”时则停用抖动,默认值为 true。
2、android:shape="rectangle|line|oval|ring"//分别为矩形、线、椭圆、环。默认为矩形rectangle。
3、android:innerRadius="integer" // shape为ring时可用,内环半径
4、android:innerRadiusRatio="float" // shape为ring时可用,内环的厚度比,即环的宽度比表示内环半径,默认为3,可被innerRadius值覆盖
5、android:thickness="integer" // shape为ring时可用,环的厚度
6、android:thicknessRatio="float" // shape为ring时可用,环的厚度比,即环的宽度比表示环的厚度,默认为9,可被thickness值覆盖
7、android:tint="color" // 给shape着色
8、android:tintMode="src_in|src_atop|src_over|add|multiply|screen" // 着色类型
9、android:useLevel="false|true" // 较少用,一般设为false,否则图形不显示。为true时可在LevelListDrawable使用
10、android:visible="false|true"//是否可见
11、圆角属性
- <corners
- android:radius="integer" // 圆角半径,该值设置时下面四个属性失效
- android:bottomLeftRadius="integer" // 左下角圆角半径
- android:bottomRightRadius="integer" // 右下角圆角半径
- android:topLeftRadius="integer" // 左上角圆角半径
- android:topRightRadius="integer" // 右上角圆角半径
- />
12、渐变属性
- <gradient
- android:useLevel="false|true" // 与上面shape中该属性的一致
- android:type="linear|radial|sweep" // 渐变类型,分别为线性、放射性、扫描性渐变,默认为线性渐变linear
- android:angle="integer" // 渐变角度,当上面type为线性渐变linear时有效。角度为45的倍数,0度时从左往右渐变,角度方向逆时针
- android:centerColor="color" // 渐变中间位置颜色
- android:startColor="color" // 渐变开始位置颜色
- android:endColor="color" // 渐变结束位置颜色
- android:centerX="float" // type为放射性渐变radial时有效,设置渐变中心的X坐标,取值区间[0,1],默认为0.5,即中心位置
- android:centerY="float" // type为放射性渐变radial时有效,设置渐变中心的Y坐标,取值区间[0,1],默认为0.5,即中心位置
- android:gradientRadius="integer" // type为放射性渐变radial时有效,渐变的半径
- />
13、内边距
- <padding
- android:bottom="integer" // 设置底部边距
- android:left="integer" // 左边边距
- android:right="integer" // 右边
- android:top="integer" // 顶部
- />
14、图形大小
- <size
- android:height="integer" // 宽度
- android:width="integer" // 高度
- />
15、填充色
- <solid
- android:color="color" // shape的填充色
- />
16、描边
- <stroke
- android:color="color" // 描边的颜色
- android:width="integer" // 描边的宽度
- android:dashGap="integer" // 虚线间隔
- android:dashWidth="integer" // 虚线宽度
- />
四、shape实例
原始配置参数如下:
- <?xml version="1.0" encoding="utf-8"?>
- <shape
- xmlns:android="http://schemas.android.com/apk/res/android" >
- <corners android:radius="10dp"/>
- <solid android:color="#00ff00"/>
- <gradient
- android:type="sweep"
- android:angle="45"
- android:startColor="#0000ff"
- android:endColor="#ff00ff"/>
- <stroke
- android:color="#0ff0ff"
- android:width="30dp"
- android:dashGap="10dp"
- android:dashWidth="10dp"/>
- <size
- android:height="500dp"
- android:width="500dp"/>
-
- <padding
- android:bottom="10dp"
- android:top="10dp"
- android:left="10dp"
- android:right="10dp"/>
-
- </shape>
效果图:
1、修改stroke的间隔后,如下:
- <stroke
- android:color="#0ff0ff"
- android:width="30dp"
- android:dashGap="100dp"
- android:dashWidth="10dp"/>
2、修改宽度后,如下:
- <stroke
- android:color="#0ff0ff"
- android:width="30dp"
- android:dashGap="10dp"
- android:dashWidth="100dp"/>
3、修改长度后,如下:
- <stroke
- android:color="#0ff0ff"
- android:width="50dp"
- android:dashGap="10dp"
- android:dashWidth="10dp"/>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。