说明

在Android开发中,使用shape可以很方便的帮我们画出想要的背景,相对于png图片来说,使用shape可以减少安装包的大小,而且能够更好的适配不同的手机。

使用

先贴出官网上的说明:

<?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属性。


android:shape=["rectangle" | "oval" | "line" | "ring"]
这里可以看出,shape可以画四种图形,分别是:矩形(rectangle)、椭圆(oval)、线(line)、圆环(ring)。

先上效果图:

这里写图片描述

矩形(rectangle)

直角矩形:

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

solid:填充颜色

圆角矩形:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> <corners android:radius="10dp"></corners> <solid android:color="@color/colorPrimary"></solid> <padding android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp"></padding> </shape>

corners:圆角大小。

        android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer"

android:radius:表示4个角的圆角大小;
还可以分别设置四个角的,使用下面四个属性:android:topLeftRadius、android:topRightRadius、android:bottomLeftRadius、android:bottomRightRadius分别表示:左上、右上、左下、右下。

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

padding:设置内边距。

无填充带边框:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> <corners android:radius="10dp"></corners> <padding android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp"></padding> <stroke android:width="5dp"
android:color="@color/colorAccent"></stroke> </shape>

stroke
android:width:边框大小
android:color:边框颜色

渐变:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> <solid android:color="@color/colorPrimary"></solid> <padding android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp"></padding> <!--angle 渐变角度,0:左到右;90:下到上;180:右到左;270:上到下-->
<gradient android:startColor="@android:color/white"
android:endColor="@android:color/black"
android:angle="0"></gradient>
</shape>

gradient:
android:startColor:渐变起始颜色
android:endColor:渐变结束颜色
android:angle:渐变角度:0:左到右;90:下到上;180:右到左;270:上到下

椭圆(oval)

一般用来画圆。

纯色的圆:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"> <solid android:color="@color/colorPrimary"></solid> <size android:height="100dp"
android:width="100dp"></size> </shape>

size的height和width设置为一样大小就是一个圆了。
然后直接使用solid填充颜色即可。

渐变效果:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"> <size android:height="100dp"
android:width="100dp"></size> <gradient android:centerX="0.5"
android:centerY="0.5"
android:type="sweep"
android:startColor="@color/colorPrimary"
android:endColor="@color/colorAccent"></gradient>
</shape>

android:centerX:表示渐变的X轴起始位置,范围0-1,0.5表示圆心。
android:centerY:表示渐变的Y轴起始位置,范围0-1,0.5表示圆心。
android:type:渐变类型,有三种
分别是:
linear 线性渐变,默认的渐变类型
radial 放射渐变,设置该项时,android:gradientRadius也必须设置
sweep 扫描性渐变

线(line)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line"> <stroke
android:width="1dp"
android:color="@color/colorAccent"
android:dashGap="3dp"
android:dashWidth="4dp"></stroke> <size android:height="3dp"></size>
</shape>

线是居中显示的。
android:width:填充颜色的高度
android:dashGap:虚线间距宽度
android:dashWidth:虚线宽度
<size android:height="3dp"></size>:line的高度,size大小必须大于android:width

圆环(ring)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false"
android:thickness="10dp">
<!--useLevel需要设置为false--> <solid android:color="@color/colorAccent"></solid> </shape>

android:thickness:圆环宽度

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false"
android:thickness="10dp">
<!--useLevel需要设置为false--> <solid android:color="@color/colorAccent"></solid> <gradient android:startColor="@color/colorAccent"
android:endColor="@color/colorPrimary"
android:type="sweep"></gradient> </shape>

以上只是简单的介绍了一下shape的用户,里面有很多属性还没有用到,需要大家自己去实践一下,写出来看到效果才能更好的理解。

完整代码地址:https://github.com/fccaikai/ShapeDemo
以上如果有什么不对的地方希望大家能提出来。

Android Shape使用的更多相关文章

  1. android shape使用总结

    今天使用到shape,这个里面有很多属性,在这里我记录一下各个属性的使用的情况以及所代表的意思 <?xml version="1.0" encoding="utf- ...

  2. android:shape

    android:shape=["rectangle" | "oval" | "line" | "ring"] shape ...

  3. Android shape的使用(圆角矩形)

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  4. android shape详解

    shape--> shape属性: rectangle: 矩形,默认的形状,可以画出直角矩形.圆角矩形.弧形等 solid: 设置形状填充的颜色,只有android:color一个属性 andr ...

  5. android shape(如自定义Button)

    Shape 前言:有时候会去自己去画一些Button的样式来展现在UI当中,其中主要用到的就是Shape 先来看一段代码: <?xml version="1.0" encod ...

  6. android shape的使用详解以及常用效果(渐变色、分割线、边框、半透明阴影效果等)

    shape使用.渐变色.分割线.边框.半透明.半透明阴影效果. 首先简单了解一下shape中常见的属性.(详细介绍参看  api文档 ) 转载请注明:Rflyee_大飞: http://blog.cs ...

  7. Android Shape画圆,矩形

    画圆环代码如下: 画圆环,外边的边界宽度大一点即可: <?xml version="1.0" encoding="utf-8"?> <shap ...

  8. ANDROID SHAPE画圆形背景_ANDROID实现角标布局

    ANDROID SHAPE画圆形背景_ANDROID实现角标布局 <?xml version="1.0" encoding="UTF-8"?> &l ...

  9. android shape总结 和控制的风格定制

    1:shape总结 1):shape文件是放置在drawable文件下的.res/drawable/filename.xml. 2):shape类型:android:shape. 一共同拥有四种:re ...

  10. [转]Android Shape渲染的使用(经典,学习研究不后悔)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://mzh3344258.blog.51cto.com/1823534/1215749 ...

随机推荐

  1. Docker可视化管理工具对比(DockerUI、Shipyard、Rancher、Portainer)

    1.前言 谈及docker,避免不了需要熟练的记住好多命令及其用法,对于熟悉shell.技术开发人员而言,还是可以接受的,熟练之后,命令行毕竟是很方便的,便于操作及脚本化.但对于命令行过敏.非技术人员 ...

  2. JavaFX学习之道:JavaFX之TableView

    TableView表     TableColumn列  构建一个表主要有TableView,TableColumn,ObservableList,Bean.  加入列table.getColumns ...

  3. hashmap 循环取出全部值 取出特定的值 两种方法

    //第一种 Iterator menus = menu.iterator(); while(menus.hasNext()) { Map userMap = (Map) menus.next(); S ...

  4. oracle实现自增id

    --oracle实现自增id --创建一张T_StudentInfo表 create table T_StudentInfo ( "id" integer not null pri ...

  5. 16.boost图深度优先遍历DFS

    #include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...

  6. IOS系统设置页面跳转

    目录: 跳转 iOS10- 版本跳转url转 iOS10+ 版本跳转url转 跳转符 跳转到系统设置界面代码: // 自己应用的设置界面:url = UIApplicationOpenSettings ...

  7. MyBatis数据持久化(五)数据源配置优化

    在前面的教程中,我们把数据库的驱动.用户名.密码等配置项全部写在 SqlMapConfig.xml中: <dataSource type="POOLED"> <p ...

  8. (转载) Scrollview 嵌套 RecyclerView 及在Android 5.1版本滑动时 惯性消失问题

    Scrollview 嵌套 RecyclerView 及在Android 5.1版本滑动时 惯性消失问题 标签: scrollviewandroid滑动嵌套 2015-07-16 17:24 1112 ...

  9. 《Unix环境高级编程》读书笔记 第13章-守护进程

    1. 引言 守护进程是生存期长的一种进程.它们常常在系统引导装入时启动,仅在系统关闭时才终止.它们没有控制终端,在后台运行. 本章说明守护进程结构.如何编写守护进程程序.守护进程如何报告出错情况. 2 ...

  10. Acme Corporation UVA - 11613 费用流

    Code: #include<cstdio> #include<cstring> #include<vector> #include<queue> #i ...