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

最近被吐槽界面太丑,还是很尴尬的,全公司就一个UI设计师,所以很多事情还是不忍直视,一个同事问我,背景可不可以使用渐变的感觉,然后我就有种突然感觉眼前一亮的感觉。还真的没有做过这方面的东西,单纯使用渐变说真的,并不是很让人感兴趣,所以就思考着能不能在自定义视图里面直接定义一个xml参数,在xml中可以直接指定渐变的颜色,感觉还真的挺容易实现,唯一卡的一下子就是平常不太遇到的自定义字段reference/color也就是通过R.color.xxx来指定颜色而不是单纯使用字符串#111111来指定颜色,这个了解一下对应的用法其实也很快。整个过程1个小时吧,就实现了渐变颜色的标题,但是当前的情况只能指定起始渐变颜色和终结渐变颜色,如果想弄更加奇葩的颜色就需要继续研究了= =说真的,当前的公司感觉什么东西都缺,有点心有余而力不足的感觉呢。

转载于/感谢:https://www.cnblogs.com/popfisher/p/5606690.html

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

 

复制代码

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 标签使用详解 。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值