关于ShapeDrawable应用的一些介绍(下)

我们今天接着来看一下Shape中的stroke属性,stroke其实就是边缘的意思,当我们在定义画笔的时候,有很多时候会用到 FILL 和 STROKE,前者能够画出一个实心的形状,而后者就画出一个空心的图形,所以在这里,stroke表示同样的意思,就是描边。

它只有四个属性:

1)android:width,表示的是线的粗细。

2)android:color,表示的是线的颜色。

3)android:dashGap,表示的是一条虚线,gap表明是虚线中线与线间的的空隙。

4)android:dashWidth,这个属性表示的是虚线中线的长度,只有设置了dashGap来画一条虚线,这个属性值才能起作用。

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <solid android:color="#CCCCCC"/>
    <stroke android:width="1dp" android:color="#FF0000" android:dashWidth="10dp" android:dashGap="2dp"/>
</shape>
下面是效果,


最下面的按钮是把形状态变成了line的格式:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="line">
    <solid android:color="#CCCCCC"/>
    <stroke android:width="1dp" android:color="#FF0000" android:dashWidth="10dp" android:dashGap="2dp"/>
</shape>

关于stroke的属性其实就这么一点点,没了。

不过我们讲到shape="line“,我们之前做的例子都是rectangle(矩形),oval(椭圆),而line其实就是一条线,可以是虚线,也可以是直线,都是比较简单的。

Android中提供了四种形状,rectangle(矩形),oval(椭圆),line(直线)和 ring(环),前面几种我们其实已经基本说到了,现在就说说最后的ring(环)吧。

先看看xml中的定义,我定义了三个,可以看一下区别:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring"    
    android:useLevel="false">
    <gradient android:startColor="#FF0000"
        android:endColor="#00FF00"
        android:type="linear"/>
    <stroke android:width="1dp" android:color="#0000FF"/>
</shape>
<!-- Ring1 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring"
    android:innerRadiusRatio="3"    
    android:thicknessRatio="9"
    android:useLevel="false">
    ...
</shape>
<!-- Ring2 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring"
    android:innerRadiusRatio="2"  
    android:thicknessRatio="2"
    android:useLevel="false">
    ...
</shape>
<!-- Ring3 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring"    
    android:innerRadiusRatio="2"  
    android:innerRadius="30dp"
    android:thicknessRatio="2"
    android:useLevel="false">
    ...
</shape>


如果我们定义的形状是"ring",我们必须把android:useLevel设为false,不然的话,这个环是显示不出来的。

关于Ring还有几个其特有的属性:

1)innerRadius,设置环内圆的半径。

2)innerRadiusRatio,设置的是内圆半径的比例,默认是3,表明是整个环的宽度的1/3,但是如果我们设置了innerRadius,即具体的宽度的话,这个属性是会被覆盖的。

3)thickness,设置环的厚度。

4)thincknessRatio,设置环的厚度的比较,默认是9,表明是整个环的宽度的1/9,但是如果设置了thickness,同样的,这个属性也会被覆盖的。

(我觉得官文文档那里应该是说反了)

下面是上面定义的4个环的效果图:


第一个是默认的,我们只设置了useLevel=false,其它都是默认值,然后第二个就设置其innerradiusratio = 3 和thicknessratio = 9了。

再从左下角的环,可以看出,设置内径和环的厚度的时候,会先设置内径的区域,而环的厚度如果超过了控件的范围,也就不显示了。

关于shape的基础用法基本就是这样,在更多的时候,我们是结合了layer和shape来实现自定义的效果,比如自定义的进度条,在以后的篇章中,再来慢慢说吧。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值