Android自定义图形-Shape
来源:互联网 发布:比淘宝好的购物网站 编辑:程序博客网 时间:2024/05/29 04:23
1.1 编写目的
学习Android自定义图形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。如下图所示:
<?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图形所示。
渐变中心相对位置,取值[0~1],默认为0.5正中间。如下图所示:
2.2.5 GradientRadius(渐变的半径)
只有当渐变类型为radial时才有用,且radial时必须使用gradientRadius。如下图所示。
2.2.6 UseLevel
UseLevel:通常不使用。该属性用于指定是否将该shape当成一个LevelListDrawable来使用,默认值为false。
<?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>
共有四个属性,分别是下、左、右、上的内边距(内容和边框的距离)。
<?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.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>
这是描边属性,可以定义描边的宽度,颜色,虚实线等。
Width:描边的宽度,Color:描边的颜色。
DashWidth:虚线的宽度(一段虚线的长度),DashGap:虚线的间隔(不定义或者为0时,不显示虚线)。
<?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>
Tint:Shape着色。
TintMode:着色模式。
Dither:是否使用抖动技术。
Visible:可见与不可见。
UseLevel:如果当做是LevelListDrawable使用时值为true,否则为false。
InnerRadius:内环的半径。
InnerRadiusRatio:以环的宽度比率来表示内环的半径。
Thickness:环的厚度
ThicknessRatio:以环的宽度比率来表示环的厚度。
Shape:取值["rectangle" | "oval" | "line" | "ring"]共有4中形状,分别是矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)。默认为矩形,下面分别是四种形状:
使用Tint后会覆盖solid属性,solid无效。TintMode(使用比较少,下面有不同模式显示状态,先画圆再画矩形框,设置蓝色矩形框的TintMode属性)。如下图所示:
2.7.3 Dither(抖动技术)
是否启用抖动技术。值为false和true,默认true。(是一种图像算法,在不同色域的显示屏上修正颜色)。
Visible控制图形是否可见。
如果当做是LevelListDrawable使用时值为true,否则为false;Shape取值"ring"(圆环)时UseLevel必须等于false,等于True无法显示。
2.7.6 InnerRadius、InnerRadiusRatio(内环半径)
InnerRadiusRatio在不定义InnerRadius时有效,内环半径=环的宽度/ InnerRadiusRatio。如下图所示:
2.7.7 Thickness、ThicknessRatio(环的厚度)
ThicknessRatio在不定义Thickness时有效,环的厚度=环的宽度/ ThicknessRatio。如下图所示: