Android GradientDrawable使用优势:

  1. 快速实现一些基本图形(线,矩形,圆,椭圆,圆环)

  2. 快速实现一些圆角,渐变,阴影等效果

  3. 代替图片设置为View的背景

  4. 可以减少apk大小,提升用户下载意愿

  5. 还可以减少内存占用

  6. 方便修改与维护

  基于上面几种优势,我们很多时候都会选择使用android的shape,下面分别介绍shape的静态使用和动态使用

1. GradientDrawable的静态使用(xml中使用shape标签定义)

  在drawable中创建一个xml文件,在布局文件中直接引用这个xml文件即可

<?xml version="1.0" encoding="utf-8"?>

 <!--
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:width 整型 宽度
android:height 整型 高度
-->
<size
android:width="50dp"
android:height="50dp"/> <!--圆角
android:radius     整型 半径
android:topLeftRadius    整型 左上角半径
android:topRightRadius 整型 右上角半径
android:bottomLeftRadius 整型 左下角半径
android:bottomRightRadius 整型 右下角半径
-->
<corners
android:radius="10dp"/><!-- 设置圆角半径,可以分别设置4个角 --> <!--渐变,这个设置之后一般就不要设置solid填充色了
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:startColor="@android:color/white"
android:centerColor="@android:color/black"
android:endColor="@android:color/black"
android:useLevel="true"
android:angle="45"
android:type="radial"
android:centerX="0"
android:centerY="0"
android:gradientRadius="90"/> <!-- 间隔
内边距,即内容与边的距离
android:left 整型 左内边距
android:top 整型 上内边距
android:right 整型 右内边距
android:bottom 整型 下内边距
-->
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/> <!--填充
android:color 颜色值 填充颜色
-->
<solid
android:color="@android:color/white"/><!-- 填充的颜色 --> <!--描边
android:width 整型 描边的宽度
android:color 颜色值 描边的颜色
android:dashWidth 整型 表示描边的样式是虚线的宽度, 值为0时,表示为实线。值大于0则为虚线。
android:dashGap 整型 表示描边为虚线时,虚线之间的间隔 即“ - - - - ”
-->
<stroke
android:width="1dp" <!-- 边框宽度 -->
android:color="@android:color/black"
android:dashWidth="1dp"
android:dashGap="2dp"/> </shape>

2. 动态创建GradientDrawable并使用

  用shape标签定义的xml,最终都是转化为GradientDrawable对象,而不是ShapeDrawable, 也不是起类型对应的 OvalShape,RoundRectShape等。

  GradientDrawable可以动态设置类型如下图所示,跟xml文件中类型android:shape的值一一对应。

View view = null;    // 这个view是你需要设置背景的view
int strokeWidth = 1; // 1dp 边框宽度
int roundRadius = 5; // 5dp 圆角半径
int strokeColor = Color.parseColor("#FFFF0000");//边框颜色
int fillColor = Color.parseColor("#FF00FF00"); //内部填充颜色

GradientDrawable gd = new GradientDrawable();//创建drawable
gd.setColor(fillColor);
gd.setCornerRadius(roundRadius);
gd.setStroke(strokeWidth, strokeColor);
gd.setGradientType(GradientDrawable.RECTANGLE);
view.setBackgroundDrawable(gd);
// 创建渐变的shape drawable
int colors[] = { 0xff255779 , 0xff3e7492, 0xffa6c0cd };//分别为开始颜色,中间夜色,结束颜色
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
view.setBackgroundDrawable(gd);

3. 动态改变GradientDrawable的属性

  既然GradientDrawable都能动态创建,那么肯定能过动态修改,我们可以通过先获取view上设置的background drawable

  如果是GradientDrawable则强制转换为GradientDrawable,这个时候就可以修改里面的属性,像动态创建时一样设置,设置好之后重新设置给view.

GradientDrawable drawable =(GradientDrawable)view.getBackground();
drawable.setColor(fillColor); // 设置填充色
drawable.gd.setStroke(strokeWidth, strokeColor); // 设置边框宽度和颜色
gd.setColors(colors); // 设置渐变颜色数组

总结:

  请注意区分 GradientDrawable 和 ShapeDrawable,这两个 Drawable 官方文档解释都是可以使用 shape 标签来定义,但实际使用过程却发现使用 shape 标签定义的 Drawable 属于 GradientDrawabl。使用 shape 标签能定义多种多样的 Drawable,能够方便实现圆角,渐变等效果,更多 shape 标签定义请参考 Drawable实战解析:Android XML shape 标签使用详解 。

Android GradientDrawable(shape标签定义) 静态使用和动态使用(圆角,渐变实现)的更多相关文章

  1. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  2. Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  3. Android NDK生成及连接静态库与动态库

    对于Android应用开发,大部分情况下我们使用Java就能完整地实现一个应用.但是在某些情况下,我们需要借助C/C++来写JNI本地代码.比如,在使用跨平台的第三方库的时候:为了提升密集计算性能的时 ...

  4. android NDK 使用(多个)静态库生成动态库

    android NDK 使用(多个)静态库生成动态库. 1.编写Android.mk文件:如下两种方式都可以,用于NDK编译工具生成的两个.a文件来生成最终的libtwolib-second.so动态 ...

  5. Android 使用shape定义不同控件的的颜色、背景色、边框色

    Android 使用shape定义不同控件的的颜色.背景色.边框色 设置按钮的右边框和底边框颜色为红色,边框大小为3dp: 在drawable新建一个 buttonstyle.xml的文件,内容如下: ...

  6. 详解shape标签

    转载自:http://blog.csdn.net/harvic880925/article/details/41850723 一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标 ...

  7. Android实现AppWidget、Broadcast静态注册

    Android实现AppWidget.Broadcast静态注册 本篇博客是基于我上一篇博客继续修改的,详情请看Android实现AppWidget.Broadcast动态注册 开发工具:Andori ...

  8. Android中shape属性详解

    一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用. 1.新建shape文件 首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.x ...

  9. 【转】【Android】Android Drawable Shape 组合画田字格

    使用layer-list组合多个Shap <?xml version="1.0" encoding="utf-8"?> <layer-list ...

随机推荐

  1. Web大前端时代之:HTML5+CSS3入门系列

    准备来一波新技术,待续.... Old: 联系源码:https://github.com/dunitian/LoTHTML5 文档下载:https://github.com/dunitian/LoTD ...

  2. iOS逆向工程之KeyChain与Snoop-it

    今天博客的主题是Keychain, 在本篇博客中会通过一个登陆的Demo将用户名密码存入到KeyChain中,并且查看一下KeyChain中存的是什么东西,把这些内容给导出来.当然本篇博客的重点不是如 ...

  3. ASP.NET Core project.json imports 是什么意思?

    示例代码: "frameworks": { "netcoreapp1.0.0": { "imports" : "portable- ...

  4. 自己写的数据交换工具——从Oracle到Elasticsearch

    先说说需求的背景,由于业务数据都在Oracle数据库中,想要对它进行数据的分析会非常非常慢,用传统的数据仓库-->数据集市这种方式,集市层表会非常大,查询的时候如果再做一些group的操作,一个 ...

  5. WEB安全隐患

    org.apache.commons.lang.StringEscapeUtils 进行输入框内容处理 [StringEscapeUtils.escapeSql(str);StringEscapeUt ...

  6. Linux学习

    Linux 命令英文全称su:Swith user 切换用户,切换到root用户cat: Concatenate 串联uname: Unix name 系统名称df: Disk free 空余硬盘du ...

  7. 【一起学OpenFoam】02 软件准备

    "工欲善其事必先利其器",在利用OpenFoam解决我们的工程问题之前,首先要做的事情是搭建一个OpenFoam运行环境.很遗憾的是,OpenFoam的原生开发系统是Linux,因 ...

  8. v14.0\AspNet\Microsoft.Web.AspNet.Props 找不到

    错误 E:\Github\AutoMapper\src\AutoMapper\AutoMapper.CoreCLR.kproj : error  : 未找到导入的项目"C:\Program ...

  9. Storm介绍(二)

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是Storm系列之一,主要介绍Storm的架构设计,推荐读者在阅读 ...

  10. ABP框架 - 缓存

    文档目录 本节内容: 简介 ICacheManager ICache ITypedCache 配置 实体缓存 EntityCache 是如何工作 Redis 缓存集成 简介 ABP提供了一个缓存接口, ...