Android自定义图形-Shape

来源:互联网 发布:比淘宝好的购物网站 编辑:程序博客网 时间:2024/05/29 04:23
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属性)。如下图所示:

 


 
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。如下图所示:


 
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 海岛奇兵后资源怎么办 头大身子小怎么办 有黑头毛孔大怎么办 我太矮了怎么办 冲动消费后后悔怎么办 冰箱只能放阳台怎么办 不封阳台洗衣机怎么办 电梯显示屏坏了怎么办 mac重启不了怎么办 象棋两个帅相对怎么办? 吃奥利司他拉油怎么办 psp侍道2死了怎么办 拍婚纱照有刘海怎么办 发动机链条松了怎么办 家里有小蟑螂怎么办 家里厨房有蟑螂怎么办 红衣教仇恨了怎么办 阵眼图标不见了怎么办 剑三忘记账号怎么办 猪驱虫药中毒怎么办 猫咪不吃驱虫药怎么办 老是有骚扰电话怎么办 总是有骚扰电话怎么办 淘宝网证书错误怎么办 网站证书有问题怎么办 手机证书有问题怎么办 https证书存在错误怎么办 手机证书过期了怎么办 ie安全证书过期怎么办 xp安全证书过期怎么办 安全员证过期了怎么办 银行证书过期了怎么办 ie安全证书错误怎么办 ie打开证书错误怎么办 物流损坏不赔偿怎么办 政府违法卖土地怎么办 去香港个人签证怎么办 去香港怎么办自由行 去香港不会粤语怎么办 去香港手机上网怎么办 九阴碎银不够怎么办