当前位置: 移动技术网 > IT编程>移动开发>Android > Android中drawable使用Shape资源

Android中drawable使用Shape资源

2019年07月24日  | 移动技术网IT编程  | 我要评论

五颜六色,通缉女逃犯落网,内地首富

本文实例为大家分享了drawable使用shape资源的具体内容,供大家参考,具体内容如下

1.画一条水平方向的虚线

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="line" >

  <stroke
    android:dashgap="3dp"
    android:dashwidth="6dp"
    android:width="1dp"
    android:color="#ff8c69" />

</shape>

android:width=”1dp” 为线条的高度
android:dashgap=”3dp” 表示虚线间空隙的宽度,0表示一条实线;
android:dashwidth=”6dp” 表示每个虚线的宽度。

注意:在版本4.0以上,需要在控件中设置android:layertype=”software”,否则虚线无效,显示为实线。

2.画一条垂直方向的虚线

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromdegrees="90"
  android:todegrees="90"
  android:drawable="@drawable/line" >

</rotate>

或者使用下面的方法

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromdegrees="90"
  android:todegrees="90" >

  <shape android:shape="line" >
    <stroke
      android:dashgap="3px"
      android:dashwidth="6px"
      android:width="1dp"
      android:color="#ff8c69" />
  </shape>
</rotate>

3.画一个实心圆

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="oval" >

  <solid android:color="#ff8c69" />

</shape>

4.画一个圆环

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:innerradius="15dp"
  android:shape="ring"
  android:thickness="10dp"
  android:uselevel="false" >

  <solid android:color="#ff8c69" />

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

</shape>

android:innerradius=”15dp” 设置尺寸,内环的半径
android:thickness=”10dp” 设置尺寸,环的厚度
android:uselevel=”false” boolean值,如果当做是levellistdrawable使用时值为true,否则为false.

android:innerradiusratio=”9” 浮点型,以环的宽度比率来表示内环的半径,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.
android:thicknessratio=”2” 浮点型,以环的宽度比率来表示环的厚度, 表示环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.

5.画一个矩形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >

  <corners android:radius="30dp" />

  <solid android:color="#ff8c69" />

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

</shape>

总结

<?xml version="1.0" encoding="utf-8"?>
<!--
 shape drawable xml文件中定义的一个几何图形,定义在res/drawable/目录下,文件名filename称为访问的资源id 
 在代码中通过r.drawable.filename进行访问,在xml文件中通过@[package:]drawable/filename进行访问。 
-->
<!--
 android:shape=["rectangle" | "oval" | "line" | "ring"] 
 shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)下面的属性只有在android:shape="ring时可用: 
 android:innerradius     尺寸,内环的半径。 
 android:innerradiusratio   浮点型,以环的宽度比率来表示内环的半径,例如,如果android:innerradiusratio,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9. 
 android:thickness           尺寸,环的厚度 
 android:thicknessratio   浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessratio="2", 那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3. 
 android:uselevel       boolean值,如果当做是levellistdrawable使用时值为true,否则为false.

-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >

  <!--
      圆角 
  android:radius            整型半径 
  android:topleftradius        整型左上角半径 
  android:toprightradius        整型右上角半径 
  android:bottomleftradius       整型左下角半径 
  android:bottomrightradius      整型右下角半径

  -->
  <corners
    android:bottomleftradius="20dp"
    android:bottomrightradius="25dp"
    android:radius="8dp"
    android:topleftradius="5dp"
    android:toprightradius="15dp" />

  <!--
     渐变色 
  android:startcolor    颜色值 起始颜色 
  android:endcolor     颜色值结束颜色 
  android:centercolor   整型渐变中间颜色,即开始颜色与结束颜色之间的颜色 
  android:angle      整型渐变角度(ps:当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。angle必须为45的整数倍) 
  android:type       ["linear" | "radial" | "sweep"] 渐变类型(取值:linear、radial、sweep) 
               linear 线性渐变,这是默认设置 
               radial 放射性渐变,以开始色为中心。 
               sweep 扫描线式的渐变。 
  android:uselevel     ["true" | "false"]如果要使用levellistdrawable对象,就要设置为true。设置为true无渐变。false有渐变色 
  android:gradientradius  整型渐变色半径.当 android:type="radial" 时才使用。单独使用 android:type="radial"会报错。 
  android:centerx     整型渐变中心x点坐标的相对位置 
  android:centery     整型渐变中心y点坐标的相对位置

  -->
  <gradient
    android:angle="45"
    android:endcolor="#80ff00ff"
    android:startcolor="#ffff0000" />

  <!--
      内边距,即内容与边的距离  
  android:left       整型左内边距
  android:top       整型上内边距
  android:right      整型右内边距
  android:bottom      整型下内边距

  -->
  <padding
    android:bottom="10dp"
    android:left="10dp"
    android:right="10dp"
    android:top="10dp" />

  <!--
     size 大小 
  android:width  整型宽度 
  android:height 整型高度

  -->
  <size android:width="600dp" />

  <!--
      内部填充 
  android:color  颜色值填充颜色

  -->
  <solid android:color="#ffff9d77" />

  <!--
      描边 
  android:width        整型描边的宽度 
  android:color        颜色值描边的颜色 
  android:dashwidth      整型表示描边的样式是虚线的宽度, 值为0时,表示为实线。值大于0则为虚线。 
  android:dashgap       整型表示描边为虚线时,虚线之间的间隔 即“ - - - - ”

  -->
  <stroke
    android:width="2dp"
    android:color="#dcdcdc" />

</shape>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网