赞
踩
我想大家平常都用过shape标签来定义一个Drawable,来实现一些例如圆角、设置描边等一些需求。但是,最近发现项目中res/drawable/下的shape标签文件越来越多,每当我们实现一些稍微不同的小需求时(例如圆角半径不同)就要新建一个shape标签的文件,这不仅很繁琐,还增加了内存、增加了apk的大小。
在我们使用shape标签定义的xml时,其实最终转化为了GradientDrawable。所以,我们可以通过动态设置GradientDrawable来实现圆角、设置描边等功能需求。
有关GradientDrawable设置圆角、描边等API如下:
void |
setColor(ColorStateList colorStateList) 将此drawable更改为使用单个颜色状态列表而不是渐变。 |
---|---|
void |
setColor(int argb) 将此可绘制更改为使用单一颜色而不是渐变。 |
void |
setColorFilter(ColorFilter colorFilter) 为绘图指定一个可选的颜色过滤器。 |
void |
setColors(int[] colors) 设置用于绘制渐变的颜色。 |
void |
setCornerRadii(float[] radii) 指定四个角的每一个的半径。 |
void |
setCornerRadius(float radius) 指定渐变角的半径。 |
void |
setDither(boolean dither) 此方法已弃用。该属性被忽略。 |
void |
setGradientCenter(float x, float y) 设置渐变的像素中心位置。 |
void |
setGradientRadius(float gradientRadius) 设置渐变的半径。 |
void |
setGradientType(int gradient) 设置此drawable使用的渐变类型。 |
void |
setOrientation(GradientDrawable.Orientation orientation) 设置此drawable中定义的渐变的方向。 |
void |
setShape(int shape) 设置用于绘制渐变的形状的类型。 |
void |
setSize(int width, int height) 设置由该drawable绘制的形状的大小。 |
void |
setStroke(int width, ColorStateList colorStateList) 设置绘图的笔触宽度和颜色状态列表。 |
void |
setStroke(int width, ColorStateList colorStateList, float dashWidth, float dashGap) 设置绘图的笔触宽度和颜色状态列表。 |
void |
setStroke(int width, int color, float dashWidth, float dashGap) 设置绘图的笔触宽度和颜色。 |
void |
setStroke(int width, int color) 设置绘图的笔触宽度和颜色。 |
void |
setTintList(ColorStateList tint) 指定该drawable的色彩颜色作为颜色状态列表。 |
void |
setTintMode(PorterDuff.Mode tintMode) 指定该drawable的色调混合模式。 |
void |
setUseLevel(boolean useLevel) 设置这个drawable是否会遵守它的 level 属性。 |
更多用法请查看官方API:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。