android shape的使用

今天,简单讲讲android里如何使用shape设计各种样式。

之前,我记住在开发中也很多次使用到了shape,因为比较简单,所以一直也没有写博客进行总结。但是,最近看代码时,发现了一些shape属性还是不了解,所以在网上查找了shape的资料,这里记录一下。




首先,使用shape画的图形,这个xml文件的根节点是shape,如下:





shape取值有四种,可以是rectangle(长方形),oval(椭圆),line(线条),ring(圆环),如果设置的话默认是长方形,只有当我们要画的图形是ring的时候,下面这几个属性才会生效:


  1. android:innerRadius:内环的半径。
  2. android:innerRadiusRatio:内环的比例,比如这个值为2,那么内环的半径就为环半径除以2,如果设置了第一个属性,则这个属性不起作用。
  3. android:thickness:环的厚度。
  4. android:thicknessRatio:环的厚度比例,比如这个值为2,那么环的厚度就为环半径除以2,如果设置了第三个属性,则这个属性不起作用。
  5. android:useLevel:只有当我们的shape使用在LevelListDrawable中的时候,这个值为true,否则为false。


以上是shape节点,在shape节点中我们还可以定义其他的节点:

圆角:

  1. android:radius= "20dp"
  2. android:topLeftRadius= "20dp"
  3. android:topRightRadius= "20dp"
  4. android:bottomLeftRadius= "0dp"
  5. android:bottomRightRadius= "0dp"
  6. />


android:radius表示长方形四个角的半径,当然也可以每个角单独设定,后面单独设定的圆角半径会覆盖android:radius。


渐变:


  1. android:angle= "90"
  2. android:centerColor= "#9ACD32"
  3. android:endColor= "#9AC0CD"
  4. android:startColor= "#9AFF9A"
  5. android:type= "linear"
  6. android:useLevel= "false" />


  1. android:angle="90"表示渐变的起始位置,这个值必须为45的倍数,包括0,0表示从左往右渐变,逆时针旋转,依次是45,90,135.....,90表示从下往上渐变,270表示从上往下渐变,剩下的大家依次去推理。
  2. android:startColor="#9AFF9A",表示渐变的起始颜色
  3. android:centerColor="#9ACD32"表示渐变的过渡颜色
  4. android:endColor="#9AC0CD"表示渐变的结束颜色
  5. type表示渐变的类型,有三种,分别是linear(线性变化),radial(辐射渐变)以及sweep(扫描渐变)
  6. 当type为radial时,我们要设置android:gradientRadius="",这个表示渐变的半径(线性渐变和扫描渐变不需要设置)


填充:

"#ADFF2F" />


这个比较简单,不多说。


描边:

  1. android:width= "1dp"
  2. android:color= "#FFFF00"
  3. android:dashWidth= "15dp"
  4. android:dashGap= "5dp"
  5. />


  1. android:dashWidth表示虚线的宽度
  2. android:dashGap表示虚线之间的间隔
  3. 以上两个属性如果不设置则为实线


大小:


  1. android:width= "1dp"
  2. android:height= "1dp"
  3. />


这个表示该shape的大小,默认情况下,shape的大小与它所在的容器大小成正比。如果我们在ImageView中使用这个shape,那么可以通过android:scaleType="center"属性来限制这种缩放。


当然,还有一种padding,这和我们在xml文件中用的一样,我这里就不多说了。


最后看看shape文件的整体效果:


    //如果当做是LevelListDrawable使用时值为true,否则为false.

        //右下角的圆角半径

      //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果

    

    

    

          //虚线的间隔

简单讲讲,这个内容很简单,只是要注意android:shape="ring"时,即为圆环时,android:innerRadius,android:innerRadiusRatio,android:thickness,android:thicknessRatio,android:useLevel才会起作用。

接下来举几个例子:

1. 圆角矩形,扫描式渐变




    

    

    

结果:



2. 圆形,线性渐变




    

    

    

结果:


3. 虚线




    

    

结果:


4. 环形,放射型渐变




    

    


结果:

简单讲讲,其实就是使用android:shape设置形状,使用corners 属性设置圆角,使用gradient属性设置渐变,使用solid 属性设置填充,使用stroke 属性设置描边,使用size 属性设置大小,使用padding属性设置内边距。


android shape的使用就讲完了。


就这么简单。



你可能感兴趣的:(android,Android基础)