android中shape的使用

        在android中,我们可以使用shape画线、圆、环、矩形。shape的存储位置在项目res/drawable目录下。
一、创建shape
       1、在res/drawable目录下新建一个xml文件,将根节点改为shape。

       2、shape根节点有个shape属性,用来设置该shape文件是用来画哪个形状的,

            rectangle:矩形,默认是矩形

            line:线

            oval:圆

            ring:环

二、使用shape画线。

       1、将shape属性设为line。

       2、在shape中使用size节点,设置线图片的大小。

<size android:width="300dp" android:height="100dp"/>
       3、使用stroke节点,设置线的宽度和颜色。
<stroke android:color="#ff0000"
        android:width="1dp"/>
三、使用shape画圆。

       1、将shape属性设为oval。

       2、在shape中使用size节点,设置圆图片的大小。

<size android:height="100dp" android:width="100dp"/>
       3、在shape中使用stroke节点,设置填充画圆线的颜色和线的宽度。
<stroke android:color="#ff0000" android:width="1dp"/>
       4、在shape中使用solid节点,设置圆的填充颜色。
<solid android:color="#0000ff"/>

       5、在shape中使用gradient节点,可以设置圆的渐变颜色,该节点与solid节点冲突,设置了该节点,solid节点将没有作用。在gradient节点中,有个type属性,该属性用来设置渐变的方式,可以设置的方式有linear(线性的渐变,从左到右)、sweep(扫描式的渐变,从0角度顺时针到360度)、radial(发散式渐变,从中心点往外发散,设置了该方式,则必须设置gradientRadius属性,否则会出现错误,设置的值的大小应该在0.522左右,我自己试出来的,具体我也不是清楚,要不然感觉没效果)

<gradient android:startColor="#ff0000"
          android:centerColor="#00ff00"
          android:endColor="#0000ff"
          android:type="radial"
          android:gradientRadius="0.522dp"/>
四、使用shape画环

      1、将shape属性设置为ring,设置内圆半径innerRadius,设置环的宽度thickness,useLevel设置为false,否则不能出现效果。

android:innerRadius="50dp"
android:thickness="20dp"
android:useLevel="false"

      2、使用size节点,设置环图片的大小,设置的大小一定要比圆的大小大,否则显示不出来。(你画的圆比你的纸都大,那肯定显示不出来)

<size android:height="200dp" android:width="200dp"/>

      3、使用stroke节点,设置画环的边线的宽度和画环边线的颜色。

<stroke android:width="1dp"
    android:color="#00ff00"/>

      4、使用solid节点,设置环的填充颜色。

<solid android:color="#ff0000"/>
      5、 在shape中使用gradient节点,可以设置圆的渐变颜色,该节点与solid节点冲突,设置了该节点,solid节点将没有作用。在gradient节点中,有个type属性,该属性用来设置渐变的方式,可以设置的方式有linear(线性的渐变,从左到右)、sweep(扫描式的渐变,从0角度顺时针到360度)、radial(发散式渐变,从中心点往外发散,设置了该方式,则必须设置gradientRadius属性,否则会出现错误,设置的值的大小应该在0.522左右,我自己试出来的,具体我也不是清楚,要不然感觉没效果)

<gradient android:startColor="#ff0000"
          android:centerColor="#00ff00"
          android:endColor="#0000ff"
          android:type="sweep"/>
五、使用shape画不同角的矩形。

     1、设置shape属性为rectangle。

android:shape="rectangle"
     2、使用corners节点,设置矩形四个角的圆角半径,使用radius属性可以直接设置四个角的圆角半径,

android:radius="20dp"
使用bottomLeftRadius设置左下角圆角半径

android:bottomLeftRadius="20dp"
使用bottomRightRadius设置右下角圆角半径

android:bottomRightRadius="20dp"
使用topLeftRadius设置左上角圆角半径

android:topLeftRadius="20dp"
使用topRightRadius设置右上角圆角半径

android:topRightRadius="20dp"
以上五个属性,设置的值越大,矩形显示的更圆。

     3、使用size节点设置画矩形图片的大小

<size android:height="100dp"
      android:width="100dp"/>
     4、使用stroke节点,设置画矩形边线的宽度和画矩形边线的颜色,画矩形边线是实线还是虚线,如果是虚线,每个节虚线的长度,和间隔的长度是多少。

<stroke android:width="1dp"
        android:color="#00ff00"
        android:dashWidth="2dp"
        android:dashGap="2dp"/>
     5、使用solid节点,设置矩形填充颜色。

<solid android:color="#ff0000"/>
     6、使用gradient节点,设置填充颜色的渐变效果。

<gradient android:startColor="#ff0000"
          android:centerColor="#00ff00"
          android:endColor="#0000ff"
          android:useLevel="false"
          android:type="linear"
          android:angle="270"
          android:centerX="0.25"
          android:centerY="0.25"/>

angle属性是只能在type类型为linear时,才能设置,它设置的渐变效果是从哪个角度开始渐变,设置的值只能是45的倍数。

centerX与centerY设置的是围绕那个点渐变。(可能有误)

官网API shape

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值