Android GradientDrawable(shape标签定义) 静态使用和动态使用(圆角,渐变实现)

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的值一一对应。

Android GradientDrawable(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之shape属性详解


下一篇:salesforce 零基础学习(四十)Custom Settings简单使用