GradientDrawable 动态设置背景的使用
来源:互联网 发布:淘宝店铺可以买卖吗 编辑:程序博客网 时间:2024/05/01 23:19
我们经常会为控件按钮等设置背景图,一般的效果都是,圆角,颜色,渐变色,阴影,或者是选中效果等。正常情况下都是通过,xml文件来静态的配置。当我们在代码中需要动态配置的时候,我们可以选用GradientDrawable这个类来实现我们的效果。支持的形状有4种:
/** * Shape is a rectangle, possibly with rounded corners */矩形 public static final int RECTANGLE = 0; /** * Shape is an ellipse */圆形 public static final int OVAL = 1; /** * Shape is a line */线 public static final int LINE = 2; /** * Shape is a ring. */环形 public static final int RING = 3;
通过setShape属性来设置。
其中构造方法有两种:
public GradientDrawable() { this(new GradientState(Orientation.TOP_BOTTOM, null), null); } /** * Create a new gradient drawable given an orientation and an array * of colors for the gradient. */ public GradientDrawable(Orientation orientation, @ColorInt int[] colors) { this(new GradientState(orientation, colors), null); }
第二种就是我们需要设置渐变色的效果,构造方法。2个参数,第一个是渐变色的方向其中是个枚举类型。可选如下:
/** * Controls how the gradient is oriented relative to the drawable's bounds */ public enum Orientation { /** draw the gradient from the top to the bottom */ TOP_BOTTOM, /** draw the gradient from the top-right to the bottom-left */ TR_BL, /** draw the gradient from the right to the left */ RIGHT_LEFT, /** draw the gradient from the bottom-right to the top-left */ BR_TL, /** draw the gradient from the bottom to the top */ BOTTOM_TOP, /** draw the gradient from the bottom-left to the top-right */ BL_TR, /** draw the gradient from the left to the right */ LEFT_RIGHT, /** draw the gradient from the top-left to the bottom-right */ TL_BR, }
有8种渐变方向可选,当然还有三种的渐变模式:
/** * Gradient is linear (default.) */ public static final int LINEAR_GRADIENT = 0; /** * Gradient is circular. */ public static final int RADIAL_GRADIENT = 1; /** * Gradient is a sweep. */ public static final int SWEEP_GRADIENT = 2;
通过setGradientType方法设置属性。
第二各参数就是渐变色数组,从头到尾的颜色有哪些。当然对于矩形,我们还可以设置圆角,通过setCornerRadii这个属性来设置,第一个是drawable对象,后面便是4个角圆角的半径了。
0 0
- GradientDrawable 动态设置背景的使用
- GradientDrawable 的简单使用(绘制控件的背景)
- GradientDrawable的使用(shape)
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果[整理后的源码]
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- 【Android 界面效果20】Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- Android GradientDrawable类的详解,设置activity的背景颜色渐变效果
- 使用GradientDrawable
- ShapeDrawable和GradientDrawable的简单使用
- GradientDrawable动态渐变
- Android 动态设置Activity背景的方法
- Android利用GradientDrawable定义动态的shape形状探索
- jar执行乱码
- Launcher3源码分析(DeviceProfile)
- BAT 批处理脚本 教程
- centos7 LVS 配置
- 自定义控件(倒计时篇)
- GradientDrawable 动态设置背景的使用
- 1.Gorontine和Channel
- Hibernate中Criteria的完整用法
- 大话数据结构之:稠密索引
- 编译时注解Butterknife源码解析之深入篇(雷惊风)
- 多台Mac电脑使用同一个apple开发者账号测试
- 后台开发学习(六)PHP语法基础
- 深度学习笔记(1):caffe 添加新层 attention LSTM layer和LSTM layer代码精读
- 关于数据库取出的数据回车换行符无法替换问题!