关于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>
下面是效果,

关于ShapeDrawable应用的一些介绍(下)_第1张图片

最下面的按钮是把形状态变成了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个环的效果图:

关于ShapeDrawable应用的一些介绍(下)_第2张图片

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

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

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

你可能感兴趣的:(android,环,shape,ring,innerradius)