安卓中资源文件中shape的用法

位于res/drawable/文件夹下;
一:shape标签的属性:
首先是<shape >标签,其中有九种属性;
xmlns,shape,visible,dither,useLevel,innerRadius,innerRadiusRatio,thickness,thicknessRatio;

<1>.shape=""; 有四种取值:rectangle(矩形,默认),oval(椭圆),line(直线,这种shape必须使用 元素来定义这条线的宽度 ),ring(圆环);
注意:下面的属性只有当 android:shape=”ring”才使用: android:innerRadius 尺寸 内环的半径。一个尺寸值(dip等等)或者一个尺寸资源。 android:innerRadiusRatio Float类型 
这个值表示内部环的比例,例如,如果android:innerRadiusRatio = ” 5 
“,那么内部的半径等于环的宽度除以5。这个值会被android:innerRadius重写。 默认值是9。 
android:thickness 尺寸 环的厚度,是一个尺寸值或尺寸的资源。 android:thicknessRatio 
Float类型 厚度的比例。例如,如果android:thicknessRatio= ” 2 
“,然后厚度等于环的宽度除以2。这个值是被android:innerRadius重写, 默认值是3。 android:useLevel 
Boolean类型 如果用在 LevelListDrawable里,那么就是true。如果通常不出现则为false。
<2>dither: (图像的抖动处理,当每个颜色值以低于8位表示时,对应图像做抖动处理可以实现在可显示颜色总数比较低(比如256色)时还保持较好的显示效果;取值为true或false;
我们一般不用这个属性;
<3>.visible:这个形状是否可见;取值为true或false;
<4>.xmlns:这个就不用说了,系统生成;
<5>.useLevel:        android:useLevel     Boolean类型。如果在LevelListDrawable中使用,则为true。


二:shape标签的子标签:
而后<shape>标签下又有六种子标签:
< solid>,<stroke.,<size>,<gradient.,<corner>,<padding>;

(1).    solid:填充;
android:color 指定填充的颜色
   
   
  1. <solid android:color="#0000ff"/>

(2).            stroke : 描边;
android:width="2dp" 描边的宽度,android:color 描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp" 
android:dashGap="3dp"
其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离
   
   
  1. <stroke android:width="3dp" android:color="#ff0000" 
  2.                   android:dashWidth="20dp" android:dashGap="50dp"/>

(3).             size : 大小;
android:width="300dp"     这是shape的宽度;
 height="60dp"     这是shape的高度;
但注意我们所用的控件是否比shape大或者小,保证shape与控件的匹配;
注意:默认情况下,这个shape会缩放到与他所在容器大小成正比。当你在一个ImageView中使用这个shape,你可以使用 
android:scaleType=”center”来限制这种缩放。
   
   
  1. <size android:width="300dp" android:height="160dp"/>

(4).             gradient:渐变
渐变有九种属性;
<1>.android:startColor="#ff0000" 起始颜色;
<2>.    centerColor="#00ff00"  中间颜色;
<3>.         endColor="#0000ff"     结束颜色;

<4>.android:angle是渐变角度,必须为45的整数倍

angle值对应的位置如图:


<5>.渐变模式:type="";分为三种:linear,radial,sweep;

另外渐变默认的模式为android:type="linear",即线性渐变,不需要指定半径;



可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。
 
也可以指定渐变为扫描渐变---围绕一个中心点扫描渐变就像电影里那种雷达扫描 android:type="sweep",不
需要指定半径;
 

<6>.渐变半径:android:gradientRadius="50dp",用于和type="radial"配合使用,只有在 android:type=”radial”才使用;
<7>.渐变中心的相对X坐标,在0到1.0之间            android:centerX="0.5" ;
 <8>.渐变中心的相对Y坐标,在0到1.0之间。   android:centerY="0.3"  ;
 

<9>useLevel:
android:useLevel Boolean类型。如果在LevelListDrawable中使用,则为true。

    
    
  1. <gradient android:startColor="#ff0000"
  2. android:centerColor="#00ff00"
  3. android:endColor="#0000ff"
  4. android:angle="0"
  5. android:type="radial"
  6. android:centerX="0.5"
  7. android:centerY="0.3"
  8. android:useLevel="false"
  9. android:gradientRadius="150"
  10. />


(5).            corner  : 圆角 
为Shape创建一个圆角,只有shape是rectangle时候才使用。

android:radius为角的弧度,值越大角越圆。 
我们还可以把四个角设定成不同的角度, 
同时设置五个属性,则Radius属性无效

android:Radius=”20dp” 设置四个角的半径

android:topLeftRadius=”20dp” 设置左上角的半径 
android:topRightRadius=”2

   
   
  1. <corners android:radius="20dp" />

  2. <corners android:topLeftRadius="60dp" android:topRightRadius="20dp"
  3.          android:bottomLeftRadius="5dp" android:bottomRightRadius="10dp"/>

0dp” 设置右上角的半径 
android:bottomLeftRadius=”20dp” 设置右下角的半径 
android:bottomRightRadius=”20dp” 设置左下角的半径


(6).        padding:间隔 

可以设置上下左右四个方向的间隔;

android:left="10dp";        //    左边间隔
android:right="20dp";        //右边间隔
android:top="30dp";        //上边间隔
android:bottom="20dp";        //下边间隔
    
    
  1. <padding android:left="10dp" android:right="10dp" 
  2.                        android:top="20dp" android:bottom="40dp"/>










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值