当前位置:   article > 正文

Android shape使用_android shap android:uselevel="false

android shap android:uselevel="false

一、shape简介

是一种在XML文件中定义的通用形状,在 Android 开发中,可以使用 shape 定义各种各样的形状,也可以定义一些图片资源,相对于传统图片来说,使用 shape 可以减少资源占用,减少安装包大小,还能够很好地适配不同尺寸的手机。

文件位置:res / drawable / filename.xml,文件名一般用资源ID。

使用:

在 Java 中:R.drawable.filename 

在 XML 中:@[package:]drawable/filename

二、shape使用

1、testshape.xml 配置

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape
  3. xmlns:android="http://schemas.android.com/apk/res/android" >
  4. <corners android:radius="10dp"/>
  5. <solid android:color="#00ff00"/>
  6. <gradient
  7. android:type="sweep"
  8. android:angle="45"
  9. android:startColor="#0000ff"
  10. android:endColor="#ff00ff"/>
  11. <stroke
  12. android:color="#0ff0ff"
  13. android:width="30dp"
  14. android:dashGap="10dp"
  15. android:dashWidth="10dp"/>
  16. <size
  17. android:height="500dp"
  18. android:width="500dp"/>
  19. <padding
  20. android:bottom="10dp"
  21. android:top="10dp"
  22. android:left="10dp"
  23. android:right="10dp"/>
  24. </shape>

2、主activity.xml 中进行引用

  1. <TextView
  2. android:layout_width="100dp"
  3. android:layout_height="100dp"
  4. android:text="hello world"
  5. android:background="@drawable/testshape"
  6. android:id="@+id/testShape"/>

三、shape属性说明

shape子标签属性可以定义控件的一些展示效果,例如 圆角渐变填充描边大小边距。

1、android:dither="false|true" //将在位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕)启用位图的抖动;值为“false”时则停用抖动,默认值为 true。

2、android:shape="rectangle|line|oval|ring"//分别为矩形、线、椭圆、环。默认为矩形rectangle。

3、android:innerRadius="integer" // shape为ring时可用,内环半径

4、android:innerRadiusRatio="float" // shape为ring时可用,内环的厚度比,即环的宽度比表示内环半径,默认为3,可被innerRadius值覆盖

5、android:thickness="integer" // shape为ring时可用,环的厚度

6、android:thicknessRatio="float" // shape为ring时可用,环的厚度比,即环的宽度比表示环的厚度,默认为9,可被thickness值覆盖

7、android:tint="color" // 给shape着色

8、android:tintMode="src_in|src_atop|src_over|add|multiply|screen" // 着色类型

9、android:useLevel="false|true" // 较少用,一般设为false,否则图形不显示。为true时可在LevelListDrawable使用

10、android:visible="false|true"//是否可见

11、圆角属性

  1. <corners
  2. android:radius="integer" // 圆角半径,该值设置时下面四个属性失效
  3. android:bottomLeftRadius="integer" // 左下角圆角半径
  4. android:bottomRightRadius="integer" // 右下角圆角半径
  5. android:topLeftRadius="integer" // 左上角圆角半径
  6. android:topRightRadius="integer" // 右上角圆角半径
  7. />

12、渐变属性

  1. <gradient
  2. android:useLevel="false|true" // 与上面shape中该属性的一致
  3. android:type="linear|radial|sweep" // 渐变类型,分别为线性、放射性、扫描性渐变,默认为线性渐变linear
  4. android:angle="integer" // 渐变角度,当上面type为线性渐变linear时有效。角度为45的倍数,0度时从左往右渐变,角度方向逆时针
  5. android:centerColor="color" // 渐变中间位置颜色
  6. android:startColor="color" // 渐变开始位置颜色
  7. android:endColor="color" // 渐变结束位置颜色
  8. android:centerX="float" // type为放射性渐变radial时有效,设置渐变中心的X坐标,取值区间[0,1],默认为0.5,即中心位置
  9. android:centerY="float" // type为放射性渐变radial时有效,设置渐变中心的Y坐标,取值区间[0,1],默认为0.5,即中心位置
  10. android:gradientRadius="integer" // type为放射性渐变radial时有效,渐变的半径
  11. />

 13、内边距

  1. <padding
  2. android:bottom="integer" // 设置底部边距
  3. android:left="integer" // 左边边距
  4. android:right="integer" // 右边
  5. android:top="integer" // 顶部
  6. />

14、图形大小

  1. <size
  2. android:height="integer" // 宽度
  3. android:width="integer" // 高度
  4. />

15、填充色

  1. <solid
  2. android:color="color" // shape的填充色
  3. />

16、描边

  1. <stroke
  2. android:color="color" // 描边的颜色
  3. android:width="integer" // 描边的宽度
  4. android:dashGap="integer" // 虚线间隔
  5. android:dashWidth="integer" // 虚线宽度
  6. />

四、shape实例

原始配置参数如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape
  3. xmlns:android="http://schemas.android.com/apk/res/android" >
  4. <corners android:radius="10dp"/>
  5. <solid android:color="#00ff00"/>
  6. <gradient
  7. android:type="sweep"
  8. android:angle="45"
  9. android:startColor="#0000ff"
  10. android:endColor="#ff00ff"/>
  11. <stroke
  12. android:color="#0ff0ff"
  13. android:width="30dp"
  14. android:dashGap="10dp"
  15. android:dashWidth="10dp"/>
  16. <size
  17. android:height="500dp"
  18. android:width="500dp"/>
  19. <padding
  20. android:bottom="10dp"
  21. android:top="10dp"
  22. android:left="10dp"
  23. android:right="10dp"/>
  24. </shape>

效果图: 

1、修改stroke的间隔后,如下:

  1. <stroke
  2. android:color="#0ff0ff"
  3. android:width="30dp"
  4. android:dashGap="100dp"
  5. android:dashWidth="10dp"/>

 

 2、修改宽度后,如下:

  1. <stroke
  2. android:color="#0ff0ff"
  3. android:width="30dp"
  4. android:dashGap="10dp"
  5. android:dashWidth="100dp"/>

3、修改长度后,如下:

  1. <stroke
  2. android:color="#0ff0ff"
  3. android:width="50dp"
  4. android:dashGap="10dp"
  5. android:dashWidth="10dp"/>

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号