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

Android GradientDrawable使用优势:

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

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

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

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

  5. 还可以减少内存占用

  6. 方便修改与维护

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

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

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



 
<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>

动态创建 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);

动态改变 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标签定义) 静态使用和动态使用(圆角,渐变实现) - cnblogs - popfisher

Copyright© 2020-2022 li-xyz 冀ICP备2022001112号-1