GradientDrawable 动态设置背景的使用
2017-03-08 14:32
225 查看
我们经常会为控件按钮等设置背景图,一般的效果都是,圆角,颜色,渐变色,阴影,或者是选中效果等。正常情况下都是通过,xml文件来静态的配置。当我们在代码中需要动态配置的时候,我们可以选用GradientDrawable这个类来实现我们的效果。支持的形状有4种:
通过setShape属性来设置。
其中构造方法有两种:
第二种就是我们需要设置渐变色的效果,构造方法。2个参数,第一个是渐变色的方向其中是个枚举类型。可选如下:
有8种渐变方向可选,当然还有三种的渐变模式:
通过setGradientType方法设置属性。
第二各参数就是渐变色数组,从头到尾的颜色有哪些。当然对于矩形,我们还可以设置圆角,通过setCornerRadii这个属性来设置,第一个是drawable对象,后面便是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个角圆角的半径了。
相关文章推荐
- 动态使用代码为Textview设置drawableLeft
- 使用装饰器模式动态设置Drawable的ColorFilter
- 动态改变Drawable中我们自定义背景的颜色并设置颜色以16进制进行设置
- Android从drawable中动态获取图片设置为XXXLayout背景
- GradientDrawable 的简单使用(绘制控件的背景)
- TextView,Button的DrawableLeft,,Right 使用代码动态的设置
- 动态设置Button、ImageView等组件在不同状态下的背景/前景显示效果。 扩展下的话可以前景/背景的显示效果可以使用网络图片。
- GradientDrawable 可以用来通过程序方式给控件设置背景如圆角,边框等
- 设置SDWebImage中maxCacheSize动态管理图片内存缓存大小无效。maxCacheSize和maxMemoryCost的定义和使用。
- 动态设置Edit控件的背景及字体颜色
- 使用ASPOSE.WORD组件,在word文档指定的位置动态生成表格,支持单元格合并及背景填充
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- [置顶] Android中使用背景设置透明度问题总结
- Android-在XML和Java代码中设置背景在不同状态的效果: <selector>/StateListDrawable
- 动态设置android:drawableLeft|Rig…
- 使用RGB值填充窗口背景 设置字体颜色
- 使用StackPane布局来设置Scene的背景
- StateListDrawable 动态更换背景
- 使用SVG实现gradient背景渐变