介绍

简单来说,shape就是用来在xml文件中定义形状,代码解析之后就可以当做Drawable一样使用

官方说明

关于shape定义的drawable

文件位置:res/drawable/filename.xml

编译资源类型:GradientDrawable

文件引用:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename

语法:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"android:shape=["rectangle" | "oval" | "line" | "ring"] > <corners
        android:radius="integer"android:topLeftRadius="integer"android:topRightRadius="integer"android:bottomLeftRadius="integer"android:bottomRightRadius="integer" /><gradient
        android:angle="integer"android:centerX="integer"android:centerY="integer"android:centerColor="integer"android:endColor="color"android:gradientRadius="integer"android:startColor="color"android:type=["linear" | "radial" | "sweep"]android:useLevel=["true" | "false"] /><padding
        android:left="integer"android:top="integer"android:right="integer"android:bottom="integer" /><size
        android:width="integer"android:height="integer" /><solid
        android:color="color" /><stroke
        android:width="integer"android:color="color"android:dashWidth="integer"android:dashGap="integer" />
</shape>

这里只做简单的描述,主要看看使用方式。关于元素的详细说明,请看 shape说明

实际使用

矩形

填充(solid)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--填充颜色 --><solid android:color="@color/colorAccent" />
</shape>


描边(stroke)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--填充颜色 --><solid android:color="@color/colorAccent" /><!--描边颜色--><!--android:dashGap  虚线间距,这里设置为0则显示的为实线--><!--android:dashWidth 虚线宽度--><stroke
        android:width="3dp"android:color="@color/colorPrimaryDark"android:dashGap="0dp"android:dashWidth="10dp" />
</shape>


圆角(corner)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--填充颜色 --><solid android:color="@color/colorAccent" /><!--描边颜色--><!--android:dashGap  虚线间距,这里设置为0则显示的为实线--><!--android:dashWidth 虚线宽度--><stroke
        android:width="3dp"android:color="@color/colorPrimaryDark"android:dashGap="4dp"android:dashWidth="10dp" /><!--圆角--><!--android:randius 设置4个叫的圆角半径,会被特定的圆角设定覆盖--><!--android:bottomLeftRadius 左下角的圆角半径--><!--android:bottomRightRadius 右下角的圆角半径--><!--android:topLeftRandius  左上角的圆角半径--><!--android:topRightRadius  右上角的圆角半径--><corners
        android:bottomLeftRadius="60dp"android:radius="30dp"android:topRightRadius="120dp" />
</shape>


渐变(gradient):linear

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--描边颜色--><!--android:dashGap  虚线间距,这里设置为0则显示的为实线--><!--android:dashWidth 虚线宽度--><stroke
        android:width="3dp"android:color="@color/colorPrimaryDark"android:dashGap="4dp"android:dashWidth="10dp" /><!--圆角--><!--android:randius 设置4个叫的圆角半径--><!--android:bottomLeftRadius 左下角的圆角半径--><!--android:bottomRightRadius 右下角的圆角半径--><!--android:topLeftRandius  左上角的圆角半径--><!--android:topRightRadius  右上角的圆角半径--><corners
        android:bottomLeftRadius="60dp"android:radius="30dp"android:topRightRadius="120dp" /><!--渐变--><gradient
        android:angle="45"android:centerColor="@color/stone"android:endColor="@color/pink"android:startColor="@color/yellow" /></shape>

渐变(gradient):radial

<gradientandroid:angle="90"android:startColor="@color/colorPrimary"android:centerColor="@color/pink"android:endColor="@color/yellow"android:gradientRadius="400dp"android:type="radial"/>

渐变(gradient):sweep

<gradientandroid:startColor="@color/colorPrimary"android:centerColor="@color/pink"android:endColor="@color/yellow"android:gradientRadius="400dp"android:type="sweep"/>


圆形

正圆

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
    android:centerColor="@color/pink"android:endColor="@color/yellow"android:startColor="@color/colorPrimary" />
<size
    android:width="400dp"android:height="400dp" />
</shape>


椭圆

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
    android:angle="90"android:centerColor="@color/pink"android:endColor="@color/yellow"android:startColor="@color/colorPrimary" />
</shape>


线条

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line"><stroke
        android:width="9dp"android:color="@color/pink" />
</shape>


环形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="ring"android:innerRadius="100dp"android:thickness="50dp"android:useLevel="false"><gradient android:startColor="@color/colorAccent"android:endColor="@color/yellow"android:centerColor="@color/pink"/>
</shape>

android:useLevel=”false”这个属性值一定要设置成false,根据google官网上的解释:

以下属性只能在android:shape="ring"的时候使用:

属性 意义
android:innerRadius 尺寸,内环的半径
android:thickness 尺寸,环的厚度
android:innerRadiusRatio 浮点型,以环的宽度比率来表示内环的半径, 例如,如果android:innerRadiusRatio=5,表示内环半径等于环的宽度除以5,这个值是可以被android:innerRadius的值覆盖,默认为9.
android:thicknessRatio 浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio=2, 那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.
android:useLevel boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.这个值一般为false,否则你的环可能不会出现

其他说明

这些自己定义的shape为根节点的drawable xml文件,可以用来当成背景使用在Button,TextView等视图

上,同时由于可以设置size大小,也可以用来制作简单的图标等。总而言之,每个细小的东西,都有挖掘的

价值,感觉这里面还有一些东西我没有注意到,还要好好的看一下文档。

最后,google镜像网站,xsoftlab,当然有条件的建议使用Google官网。

转载于:https://www.cnblogs.com/lanzhi/p/6467191.html

Android 基础(十三) shape相关推荐

  1. 【Android基础】android shape详解

    一,概述 我们在android开发中经常要用到图片,而一些简单的图片我们完全可以用shape形状drawable资源代替,使用shape有一个好处就是可以减小我们apk的大小,因为同样的效果,shap ...

  2. Android基础知识点学习总结

    Android基础知识点学习总结 安卓基础知识个人学习笔记分享~ 一.Android系统架构 Linux内核层→系统运行层→应用框架层→应用层 1.Linux内核层:Android系统是基于Linux ...

  3. Android样式开发---shape

    Thanks to:转载自Keegan小钢 原文链接:http://keeganlee.me/post/android/20150830 一个应用,应该保持一套统一的样式,包括Button.EditT ...

  4. 重学Android基础系列篇(五):Android虚拟机指令

    前言 本系列文章主要是汇总了一下大佬们的技术文章,属于Android基础部分,作为一名合格的安卓开发工程师,咱们肯定要熟练掌握java和android,本期就来说说这些~ [非商业用途,如有侵权,请告 ...

  5. Android基础入门教程——2.5.1 Toast(吐司)的基本使用

    Android基础入门教程--2.5.1 Toast(吐司)的基本使用 标签(空格分隔): Android基础入门教程 本节引言: 好的,终于学习完Adapter类相关的一些控件,当然除了讲解的那几个 ...

  6. Android基础知识——完善

    首页 下载App × Android基础知识--完善 布鲁马 2016.05.17 10:29* 字数 5478 阅读 2672评论 1喜欢 38 疯狂Android摘要,Android基础知识好乱好 ...

  7. Android学习笔记:Android基础知识点(不断更新中)

    1.Android学习笔记:OkHttp 2.Android学习笔记:更新UI的方法(UI线程和非UI线程) 3.Android学习笔记:Volley 4.Android学习笔记:Handler 5. ...

  8. Android基础入门教程——2.3.1 TextView(文本框)详解

    Android基础入门教程--2.3.1 TextView(文本框)详解 标签(空格分隔): Android基础入门教程 本节引言: 学习完Android中的六大布局,从本节开始我们来一个个讲解And ...

  9. 2022 最新 Android 基础教程,从开发入门到项目实战【b站动脑学院】学习笔记——第五章:中级控件

    第 5 章 中级控件 本章介绍App开发常见的几类中级控件的用法,主要包括:如何定制几种简单的图形.如何使用几种选择按钮.如何高效地输入文本.如何利用对话框获取交互信息等,然后结合本章所学的知识,演示 ...

最新文章

  1. php自动分配,PHP上传文件时自动分配路径的方法_php技巧
  2. Maven配置将war包部署到Tomcat(tomcat7-maven-plugin)
  3. 超低延迟直播架构解析
  4. CTF(Capture The Flag)
  5. 反射机制2,Class类的使用
  6. React向对象数组进行赋值
  7. django中的admin组件
  8. sublime配置python环境变量_Sublime Python环境配置
  9. Bootstrap 3: accordion menu 手风琴效果
  10. Tomcat集群实现源码级别剖析
  11. 格子染色问题 java_《Mathematical Olympiad——组合数学》——染色问题
  12. C语言以数据块的形式读写文件
  13. Redis系列--内存淘汰机制(含单机版内存优化建议)
  14. Maven打包时报Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war解决方案
  15. GD32VF103(riscv)与STM32F103性能对比
  16. xml文件的概述与应用场景
  17. excel文件修复工具_文件恢复工具 – 修复文件属性篡改
  18. Linux中断——request_irq
  19. 子类构造函数中super()方法的理解
  20. 分部积分法的一些特殊方法

热门文章

  1. python3 文件相关操作
  2. 素数筛法求十亿内所有质数的和(C++)
  3. 求最大公约数——欧几里得算法(JAVA)
  4. WebLogic清理缓存
  5. ElasticSearch Java SpringBoot根据时间范围分组求和
  6. 云服务器Linux安装,配置,使用nginx
  7. jSignature签名的用法,一文教会你(二)后台代码
  8. SpringMVC异常处理 自定义异常
  9. 如何给对方邮箱发照片_朋友圈如何发心形拼图九宫格照片?
  10. php symbol table,gdb方式遍历EG(symbol_table) 哈希表的key_PHP教程