且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-09-13 20:20:55

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

它只有四个属性:

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

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

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

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

代码如下:

[html] view plain copy 关于ShapeDrawable应用的一些介绍(下)关于ShapeDrawable应用的一些介绍(下)
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="rectangle">  
  4.     <solid android:color="#CCCCCC"/>  
  5.     <stroke android:width="1dp" android:color="#FF0000" android:dashWidth="10dp" android:dashGap="2dp"/>  
  6. </shape>  
下面是效果,

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

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

[html] view plain copy 关于ShapeDrawable应用的一些介绍(下)关于ShapeDrawable应用的一些介绍(下)
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="line">  
  4.     <solid android:color="#CCCCCC"/>  
  5.     <stroke android:width="1dp" android:color="#FF0000" android:dashWidth="10dp" android:dashGap="2dp"/>  
  6. </shape>  

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

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

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

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

[html] view plain copy 关于ShapeDrawable应用的一些介绍(下)关于ShapeDrawable应用的一些介绍(下)
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="ring"      
  4.     android:useLevel="false">  
  5.     <gradient android:startColor="#FF0000"  
  6.         android:endColor="#00FF00"  
  7.         android:type="linear"/>  
  8.     <stroke android:width="1dp" android:color="#0000FF"/>  
  9. </shape>  
  10. <!-- Ring1 -->  
  11. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  12.     android:shape="ring"  
  13.     android:innerRadiusRatio="3"      
  14.     android:thicknessRatio="9"  
  15.     android:useLevel="false">  
  16.     ...  
  17. </shape>  
  18. <!-- Ring2 -->  
  19. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  20.     android:shape="ring"  
  21.     android:innerRadiusRatio="2"    
  22.     android:thicknessRatio="2"  
  23.     android:useLevel="false">  
  24.     ...  
  25. </shape>  
  26. <!-- Ring3 -->  
  27. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  28.     android:shape="ring"      
  29.     android:innerRadiusRatio="2"    
  30.     android:innerRadius="30dp"  
  31.     android:thicknessRatio="2"  
  32.     android:useLevel="false">  
  33.     ...  
  34. </shape>  


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

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

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

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

3)thickness,设置环的厚度。

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

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

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

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

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

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

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


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5435501.html,如需转载请自行联系原作者