Android自定义图形-Shape
2017-12-25 11:02
796 查看
1 概述
1.1 编写目的
学习Android自定义图形shape相关的知识,记录下来,方便后续学习及查询。2 Shape图形
Android上Shape有以下几种属性shape、 corners、 gradient、 padding、size、solid、stroke,其中shape为一级属性,其余为shape子属性,子属性可以选择定义。基本结构如下图:<?xml version="1.0"encoding="utf-8"?> <shape > <corners /> <gradient /> <padding /> <size /> <solid /> <stroke /> </shape>
2.1 Corners属性(圆角)
<corners android:radius="10dp" android:topLeftRadius="10dp" android:topRightRadius="20dp" android:bottomRightRadius="30dp" android:bottomLeftRadius="40dp"/>
共有五个属性,用于设置四个圆角半径。
2.1.1 Radius(圆角)
radius是统一设置四个圆角半径,如下图所示:2.1.2 TopLeftRadius、TopRightRadius、BottomRightRadius、BottomLeftRadius(左上、右上、左下、右下圆角)
分别设置左上、右上、左下、右下角的圆角半径,和radius同时设置时,会覆盖radius。如下图所示:2.2 Gradient属性(渐变色)
<?xml version="1.0"encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:type="radial" android:angle="0" android:startColor="@color/red" android:centerColor="@color/green" android:endColor="@color/white" android:centerX="0.3" android:centerY="0.5" android:useLevel="false" android:gradientRadius="200dp"/> </shape>
2.2.1 Type(渐变类型)
Type:取值["linear" | "radial" | "sweep"]共有3中渐变类型,分别是线性渐变(默认)、放射渐变、扫描式渐变。下面分别是三种渐变:2.2.2 Angle(渐变角度)
仅对线性渐变有效,0为从左到右(逆时针旋转),必须为45的倍数,90为从下到上。下面分别为90和225:2.2.3 StartColor、CenterColor、EndColor(渐变颜色)
分别是渐变开始、中间、结束颜色。中间色可以不用。如2.2.1和2.2.2图形所示。2.2.4 CenterX、CenterY(渐变中心相对位置)
渐变中心相对位置,取值[0~1],默认为0.5正中间。如下图所示:2.2.5 GradientRadius(渐变的半径)
只有当渐变类型为radial时才有用,且radial时必须使用gradientRadius。如下图所示。2.2.6 UseLevel
UseLevel:通常不使用。该属性用于指定是否将该shape当成一个LevelListDrawable来使用,默认值为false。2.3 Padding属性(内边距)
<?xml version="1.0"encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <padding android:bottom="2dp" android:left="3dp" android:right="3dp" android:top="2dp"/> </shape>
共有四个属性,分别是下、左、右、上的内边距(内容和边框的距离)。
2.4 Size属性(宽高)
<?xml version="1.0"encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <size android:width="180dp" android:height="100dp"/> </shape>
设置图形宽高,共两个属性,分别是图形的宽度和高度。
2.5 Solid属性(填充)
自身只有一个属性,用来指定填充颜色,没有定义就不填充。2.5.1 Solid和Gradient(填充和渐变)
填充和渐变同时使用时,定义在后面的生效,前面的被覆盖。如下图所示:2.6 Stroke属性(描边)
<?xml version="1.0"encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:width="5dp" android:color="@color/red" android:dashWidth="100dp" android:dashGap="50dp"/> </shape>
这是描边属性,可以定义描边的宽度,颜色,虚实线等。
2.6.1 Width、Color(宽度、颜色)
Width:描边的宽度,Color:描边的颜色。2.6.2 DashWidth、DashGap(虚线宽度、虚线间隔)
DashWidth:虚线的宽度(一段虚线的长度),DashGap:虚线的间隔(不定义或者为0时,不显示虚线)。2.7 Shape属性(形状)
<?xml version="1.0"encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" android:dither="false" android:tint="@color/red" android:tintMode="src_atop" android:visible="true" android:innerRadius="100dp" android:innerRadiusRatio="20" android:thickness="100dp" android:thicknessRatio="6" android:useLevel="false"> </shape>
其中shape、tint、tintMode、dither、visible、useLevel等属性是共有的,剩下都是shape等于ring时所特有的。
Tint:Shape着色。
TintMode:着色模式。
Dither:是否使用抖动技术。
Visible:可见与不可见。
UseLevel:如果当做是LevelListDrawable使用时值为true,否则为false。
InnerRadius:内环的半径。
InnerRadiusRatio:以环的宽度比率来表示内环的半径。
Thickness:环的厚度
ThicknessRatio:以环的宽度比率来表示环的厚度。
2.7.1 Shape(图形形状)
Shape:取值["rectangle" | "oval" | "line" | "ring"]共有4中形状,分别是矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)。默认为矩形,下面分别是四种形状:2.7.2 Tint、TintMode(着色和着色模式)
使用Tint后会覆盖solid属性,solid无效。TintMode(使用比较少,下面有不同模式显示状态,先画圆再画矩形框,设置蓝色矩形框的TintMode属性)。如下图所示:着色模式参考:Android图形 - Bitmap(2.13)
2.7.3 Dither(抖动技术)
是否启用抖动技术。值为false和true,默认true。(是一种图像算法,在不同色域的显示屏上修正颜色)。2.7.4 Visible(可见性)
Visible控制图形是否可见。2.7.5 UseLevel(LevelListDrawable)
如果当做是LevelListDrawable使用时值为true,否则为false;Shape取值"ring"(圆环)时UseLevel必须等于false,等于True无法显示。2.7.6 InnerRadius、InnerRadiusRatio(内环半径)
InnerRadiusRatio在不定义InnerRadius时有效,内环半径=环的宽度/ InnerRadiusRatio。如下图所示:2.7.7 Thickness、ThicknessRatio(环的厚度)
ThicknessRatio在不定义Thickness时有效,环的厚度=环的宽度/ ThicknessRatio。如下图所示:相关文章推荐
- Android 自定义图形 shape
- Android系列: 自定义图形shape
- Android 02 shape自定义图形工具
- Android自定义图形shape
- Android自定义图形-Shape的使用
- Android开发之shape自定义图形
- Android中使用自定义View实现shape图形绘制
- android中使用shape自定义图形
- Android 自定义shape图形效果
- Android自定义View实现shape图形绘制
- Android中图形的绘制-----自定义View(利用canvas和Path相结合)
- Android之用自定义的shape去实现shadow效果
- Android 编程下 shape 绘制图形
- Android Shape自定义纯色圆角按钮
- Android Shape自定义纯色圆角按钮
- Android自定义图形验证码
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- Android <shape>定义图形
- 实习杂记(20)---Android里面shape定义图形相关属性
- Android自定义Shape使EditText化身为Bootstrap 风格的 textarea (样式之Shape)