原文链接:https://blog.csdn.net/JM_beizi/article/details/77531042
https://developer.android.google.cn/guide/topics/resources/drawable-resource.html#Shape
里面有非常详细的介绍。包括shape 等标签参数介绍、使用规则、映射的Drawaable。
从view.setBackgroundResource()开始追。最后没法直接追下去了,既然这样我们在源码中直接搜索DrawableInflater.java这个类,然后查看它里面的inflateFromXml() 方法。(注:我这里的源码版本是android-25)
DrawableInflater.java
@NonNull
@SuppressWarnings("deprecation")
private Drawable inflateFromTag(@NonNull String name) {
switch (name) {
case "selector":
return new StateListDrawable();
case "animated-selector":
return new AnimatedStateListDrawable();
case "level-list":
return new LevelListDrawable();
case "layer-list":
return new LayerDrawable();
case "transition":
return new TransitionDrawable();
case "ripple":
return new RippleDrawable();
case "color":
return new ColorDrawable();
case "shape":
return new GradientDrawable();
case "vector":
return new VectorDrawable();
case "animated-vector":
return new AnimatedVectorDrawable();
case "scale":
return new ScaleDrawable();
case "clip":
return new ClipDrawable();
case "rotate":
return new RotateDrawable();
case "animated-rotate":
return new AnimatedRotateDrawable();
case "animation-list":
return new AnimationDrawable();
case "inset":
return new InsetDrawable();
case "bitmap":
return new BitmapDrawable();
case "nine-patch":
return new NinePatchDrawable();
default:
return null;
}
}
到这里总该明白与xml中的标签(shape等标签)相对应的对象是哪些了吧。项目中用的最多的就是圆角矩形背景了,然后有的会有描边,还有选择器的效果。我们就用GradientDrawable 来实现。GradientDrawable可以用来设置shape类型、shape填充色、描边色和矩形的边角弧度。
/**
* 获得一个指定填充色,边框宽度、颜色的圆角矩形drawable。
* Android 中 在xml中写的"shape"标签映射对象就是GradientDrawable。
* 通过设置solidColors 和strokeColors 可实现选择器的效果
*
* @param solidColors 填充色
* @param strokeColors 描边色
* @param strokeWidth 描边线宽度
* @param dashWidth 虚线(破折线)的长度(以像素为单位)
* @param dashGap 虚线(破折线)间距,当dashGap=0dp时,为实线
* @param radius 圆角角度
* @return GradientDrawable
*/
public static Drawable getShapeDrawable(ColorStateList solidColors,
ColorStateList strokeColors, int strokeWidth,
float dashWidth, float dashGap,
float radius) {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(radius);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
gradientDrawable.setColor(solidColors);
//显示一条虚线,破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线
gradientDrawable.setStroke(strokeWidth, strokeColors, dashWidth, dashGap);
} else {
gradientDrawable.setColor(solidColors.getDefaultColor());
//显示一条虚线,破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线
gradientDrawable.setStroke(strokeWidth, strokeColors.getDefaultColor(), dashWidth, dashGap);
}
return gradientDrawable;
}
/**
* 获得一个指定填充色,指定描边色的圆角矩形drawable
*
* @param solidColor 填充色
* @param strokeColor 描边色
* @param strokeWidth 描边线宽度
* @param dashWidth 虚线(破折线)宽度
* @param dashGap 虚线(破折线)间距,当dashGap=0dp时,为实线
* @param radius 圆角角度
* @return GradientDrawable
*/
public static Drawable getShapeDrawable(@ColorInt int solidColor,
@ColorInt int strokeColor, int strokeWidth,
float dashWidth, float dashGap,
float radius) {
return getShapeDrawable(ColorStateList.valueOf(solidColor),
ColorStateList.valueOf(strokeColor), strokeWidth, dashWidth, dashGap,
radius);
}
/**
* 获得一个选择器Drawable.
* Android 中 在xml中写的"selector"标签映射对象就是StateListDrawable 对象
*
* @param defaultDrawable 默认时显示的Drawable
* @param pressedDrawable 按下时显示的Drawable
* @return 选择器Drawable
*/
public static StateListDrawable getSelectorDrawable(Drawable defaultDrawable, Drawable pressedDrawable) {
if (defaultDrawable == null) return null;
if (pressedDrawable == null) pressedDrawable = defaultDrawable;
int[][] state = {{-android.R.attr.state_pressed}, {android.R.attr.state_pressed}};
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(state[0], defaultDrawable);
stateListDrawable.addState(state[1], pressedDrawable);
return stateListDrawable;
}
/**
* 获得一个选择器Drawable.
* Android 中 在xml中写的"selector"标签映射对象就是StateListDrawable 对象
*
* @param defaultColor 默认时显示的颜色
* @param pressedColor 按下时显示的颜色
* @return 选择器Drawable
*/
public static StateListDrawable getSelectorDrawable(int defaultColor, int pressedColor, float radius) {
Drawable defaultDrawable = getSolidShapeDrawable(defaultColor, radius);
Drawable pressedDrawable = getSolidShapeDrawable(pressedColor, radius);
return getSelectorDrawable(defaultDrawable, pressedDrawable);
}
更多功能参考Drawable工具类:http://blog.csdn.net/JM_beizi/article/details/77433558