一下代码可以一定程度上代替我们编写xml来设置背景。
有报错自己修改一下就好。
使用方法
//实现红色圆形背景,注意控件的layout_width和layout_height要相等
MyDrawable.instance().back(Color.RED).backRadius(100).into(binding.myButton1);
实现
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.graphics.drawable.StateListDrawable;
import android.view.View;
import android.widget.TextView;
/**
* 动态样式构造器
*/
public class MyDrawable {
/**
* 前景色,字体
*/
private final ForeStyle foreStyle = new ForeStyle();
/**
* 背景色
*/
private final BackStyle backStyle = new BackStyle();
/**
* 入口
*
* @return MyDrawable
*/
public static MyDrawable instance() {
return new MyDrawable();
}
/**
* 私有构造方法---空
*/
private MyDrawable() {
}
/**
* 设定字体颜色
*
* @param color 颜色值
* @return MyDrawable
*/
public MyDrawable fore(int color) {
this.foreStyle.setColor(color);
return this;
}
/**
* 设定字体颜色
*
* @param color 颜色值,如#e5e0e5
* @return MyDrawable
*/
public MyDrawable fore(String color) {
this.foreStyle.setColor(color);
return this;
}
// /**
// * 设定字体颜色
// *
// * @param resId 资源id
// * @return MyDrawable
// */
// public MyDrawable foreRes(int resId) {
// this.foreStyle.setColorRes(resId);
// return this;
// }
/**
* 设定点击字体颜色样式
*
* @param color 常规颜色
* @param colorSel 选中颜色
* @return MyDrawable
*/
public MyDrawable fore(int color, int colorSel) {
this.foreStyle.setColor(color, colorSel);
return this;
}
/**
* 设定点击字体颜色样式
*
* @param color 常规颜色
* @param colorSel 选中颜色
* @return MyDrawable
*/
public MyDrawable fore(String color, String colorSel) {
this.foreStyle.setColor(color, colorSel);
return this;
}
// /**
// * 设定点击字体颜色样式
// *
// * @param resId 常规颜色
// * @param resIdSel 选中颜色
// * @return MyDrawable
// */
// public MyDrawable foreRes(int resId, int resIdSel) {
// this.foreStyle.setColorRes(resId, resIdSel);
// return this;
// }
/**
* 设定字体颜色样式,当颜色数量超为1,则只有常规颜色;当颜色数量为2,则表现为点击效果;
* 当颜色数控为3以上,颜色从上到下渐变
*
* @param colors 颜色值,列表
* @return MyDrawable
*/
public MyDrawable fore(int... colors) {
this.foreStyle.setColors(colors);
return this;
}
/**
* 背景圆角值,使用该方法请注意要设置背景颜色,否则可能没有圆角值效果
*
* @param radius 圆角值0~100有效
* @return MyDrawable
*/
public MyDrawable backRadius(int radius) {
this.backStyle.setRadius(radius);
return this;
}
/**
* 设定背景圆角值,使用要求同{@link #backRadius(int)}
*
* @param topLeft 左上圆角值
* @param topRight 右上圆角值
* @param bottomLeft 左下圆角值
* @param bottomRight 右下圆角值
* @return MyDrawable
*/
public MyDrawable backRadius(int topLeft, int topRight, int bottomLeft, int bottomRight) {
this.backStyle.setRadius(topLeft, topRight, bottomLeft, bottomRight);
return this;
}
/**
* 设定背景颜色
*
* @param color 颜色值
* @return MyDrawable
*/
public MyDrawable back(int color) {
this.backStyle.setColor(color);
return this;
}
/**
* 设备背景颜色
*
* @param color 颜色值
* @return MyDrawable
*/
public MyDrawable back(String color) {
this.backStyle.setColor(color);
return this;
}
// /**
// * 设定背景颜色
// *
// * @param resId 颜色值-资源id
// * @return MyDrawable
// */
// public MyDrawable backRes(int resId) {
// this.backStyle.setColorRes(resId);
// return this;
// }
// /**
// * 设定点击背景颜色样式
// *
// * @param resId 默认背景颜色资源id
// * @param colorResSel 选中背景颜色资源id
// * @return MyDrawable
// */
// public MyDrawable backRes(int resId, int colorResSel) {
// this.backStyle.setColorRes(resId, colorResSel);
// return this;
// }
/**
* 设定点击背景颜色样式
*
* @param color 默认背景颜色
* @param colorSel 选中背景颜色样式
* @return MyDrawable
*/
public MyDrawable back(int color, int colorSel) {
this.backStyle.setColor(color, colorSel);
return this;
}
/**
* 设定背景样式
*
* @param colors 参考{@link #fore(int...)}
* @return MyDrawable
*/
public MyDrawable back(int... colors) {
this.backStyle.setColors(colors);
return this;
}
/**
* 设定边框样式
*
* @param color 边框颜色
* @param width 边框宽度
* @return MyDrawable
*/
public MyDrawable backBorder(int color, int width) {
this.backStyle.setBorderColor(color, width);
return this;
}
// /**
// * 设定边框样式
// *
// * @param resId 边框颜色资源id
// * @param width 边框宽度
// * @return MyDrawable
// */
// public MyDrawable backBorderRes(int resId, int width) {
// this.backStyle.setBorderColorRes(resId, width);
// return this;
// }
/**
* 设定边框样式
*
* @param color 边框颜色
* @param width 边框宽度
* @return MyDrawable
*/
public MyDrawable backBorderRes(String color, int width) {
this.backStyle.setBorderColor(color, width);
return this;
}
/**
* 将参数设定到view中
*
* @param target 指定的View,不为空
*/
public void into(View target) {
if (target instanceof TextView) {
// target.setClickable(true);
if (this.foreStyle.colors != null && this.foreStyle.colors.length == 2) {
LinearGradient mLinearGradient = new LinearGradient(0.0F, 0.0F, 0.0F, ((TextView) target).getPaint().getTextSize(), this.foreStyle.colors[0], this.foreStyle.colors[1], TileMode.CLAMP);
((TextView) target).getPaint().setShader(mLinearGradient);
target.invalidate();
} else {
if (this.foreStyle.normalColor == 2147483647) {
this.foreStyle.setColor(((TextView) target).getCurrentTextColor());
}
if (this.foreStyle.selectColor == 2147483647) {
((TextView) target).setTextColor(this.foreStyle.normalColor);
} else {
ColorStateList colorStateList = new ColorStateList(new int[][]{{-16842919}, {16842919}}, new int[]{this.foreStyle.normalColor, this.foreStyle.selectColor});
((TextView) target).setTextColor(colorStateList);
}
}
}
float tl = toFloat(1.6D, (double) this.backStyle.topLeft);
float tr = toFloat(1.6D, (double) this.backStyle.topRight);
float bl = toFloat(1.6D, (double) this.backStyle.bottomLeft);
float br = toFloat(1.6D, (double) this.backStyle.bottomRight);
float[] radius = new float[]{tl, tl, tr, tr, br, br, bl, bl};
GradientDrawable normal;
if (this.backStyle.colors != null && this.backStyle.colors.length > 0) {
normal = new GradientDrawable(Orientation.TOP_BOTTOM, this.backStyle.colors);
normal.setCornerRadii(radius);
target.setBackground(normal);
} else if (this.backStyle.normalColor != 2147483647) {
normal = new GradientDrawable();
normal.setColor(this.backStyle.normalColor);
normal.setCornerRadii(radius);
normal.setStroke(this.backStyle.borderWidth, this.backStyle.borderColor);
if (this.backStyle.selectColor == 2147483647) {
target.setBackground(normal);
} else {
// target.setClickable(true);
GradientDrawable select = new GradientDrawable();
select.setColor(this.backStyle.selectColor);
select.setCornerRadii(radius);
select.setStroke(this.backStyle.borderWidth, this.backStyle.borderColor);
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{16842913}, select);
drawable.addState(new int[]{16842919}, select);
drawable.addState(new int[]{16842910}, normal);
drawable.addState(new int[0], normal);
target.setBackground(drawable);
}
}
}
/**
* 将所有已设定的数据打包返回
*
* @return StateListDrawable
*/
public StateListDrawable get() {
float tl = toFloat(1.6D, (double) this.backStyle.topLeft);
float tr = toFloat(1.6D, (double) this.backStyle.topRight);
float bl = toFloat(1.6D, (double) this.backStyle.bottomLeft);
float br = toFloat(1.6D, (double) this.backStyle.bottomRight);
float[] radius = new float[]{tl, tl, tr, tr, br, br, bl, bl};
GradientDrawable normal;
StateListDrawable drawable;
if (this.backStyle.colors != null && this.backStyle.colors.length > 0) {
normal = new GradientDrawable(Orientation.TOP_BOTTOM, this.backStyle.colors);
normal.setCornerRadii(radius);
drawable = new StateListDrawable();
drawable.addState(new int[]{16842913}, normal);
drawable.addState(new int[]{16842919}, normal);
drawable.addState(new int[]{16842910}, normal);
drawable.addState(new int[0], normal);
return drawable;
} else if (this.backStyle.normalColor != 2147483647) {
normal = new GradientDrawable();
normal.setColor(this.backStyle.normalColor);
normal.setCornerRadii(radius);
normal.setStroke(this.backStyle.borderWidth, this.backStyle.borderColor);
if (this.backStyle.selectColor == 2147483647) {
drawable = new StateListDrawable();
drawable.addState(new int[]{16842913}, normal);
drawable.addState(new int[]{16842919}, normal);
drawable.addState(new int[]{16842910}, normal);
drawable.addState(new int[0], normal);
return drawable;
} else {
GradientDrawable select = new GradientDrawable();
select.setColor(this.backStyle.selectColor);
select.setCornerRadii(radius);
select.setStroke(this.backStyle.borderWidth, this.backStyle.borderColor);
drawable = new StateListDrawable();
drawable.addState(new int[]{16842913}, select);
drawable.addState(new int[]{16842919}, select);
drawable.addState(new int[]{16842910}, normal);
drawable.addState(new int[0], normal);
return drawable;
}
} else {
return null;
}
}
private static Float toFloat(double value, double per) {
return Double.valueOf(value * per).floatValue();
}
private static int colorParse(String str) {
try {
return Color.parseColor(str);
} catch (Exception var2) {
return -3355444;
}
}
// private static int colorByRes(Context context,int resId) {
// try {
// return ContextCompat.getColor(context, resId);
// } catch (Exception var2) {
// return -3355444;
// }
// }
/***
* 背景样式实体类
*/
public static class BackStyle {
protected int topLeft;
protected int topRight;
protected int bottomLeft;
protected int bottomRight;
protected int borderColor = 2147483647;
protected int borderWidth;
protected int normalColor = 2147483647;
protected int selectColor = 2147483647;
protected int[] colors;
protected BackStyle() {
}
public BackStyle setColor(int normalColor, int selectColor) {
this.normalColor = normalColor;
this.selectColor = selectColor;
return this;
}
public BackStyle setColor(String normalColor, String selectColor) {
this.normalColor = MyDrawable.colorParse(normalColor);
this.selectColor = MyDrawable.colorParse(selectColor);
return this;
}
// public BackStyle setColorRes(int normalColor, int selectColor) {
// this.normalColor = MyDrawable.colorByRes(normalColor);
// this.selectColor = MyDrawable.colorByRes(selectColor);
// return this;
// }
public BackStyle setColor(int normalColor) {
this.normalColor = normalColor;
return this;
}
public BackStyle setColor(String normalColor) {
this.normalColor = MyDrawable.colorParse(normalColor);
return this;
}
// public BackStyle setColorRes(int normalColor) {
// this.normalColor = MyDrawable.colorByRes(normalColor);
// return this;
// }
public BackStyle setColors(int... colors) {
this.colors = colors;
return this;
}
public BackStyle setRadius(int radius) {
this.topLeft = radius;
this.topRight = radius;
this.bottomLeft = radius;
this.bottomRight = radius;
return this;
}
public BackStyle setRadius(int topLeft, int topRight, int bottomLeft, int bottomRight) {
this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
this.bottomRight = bottomRight;
return this;
}
public BackStyle setBorderColor(int borderColor, int borderWidth) {
this.borderColor = borderColor;
this.borderWidth = borderWidth;
return this;
}
// public BackStyle setBorderColorRes(int borderColor, int borderWidth) {
// this.borderColor = MyDrawable.colorByRes(borderColor);
// this.borderWidth = borderWidth;
// return this;
// }
public BackStyle setBorderColor(String borderColor, int borderWidth) {
this.borderColor = MyDrawable.colorParse(borderColor);
this.borderWidth = borderWidth;
return this;
}
}
/**
* 前景样式实体类
*/
public static class ForeStyle {
protected int normalColor = 2147483647;
protected int selectColor = 2147483647;
protected int[] colors;
protected ForeStyle() {
}
public ForeStyle setColor(int normalColor, int selectColor) {
this.normalColor = normalColor;
this.selectColor = selectColor;
return this;
}
public ForeStyle setColor(String normalColor, String selectColor) {
this.normalColor = MyDrawable.colorParse(normalColor);
this.selectColor = MyDrawable.colorParse(selectColor);
return this;
}
// public ForeStyle setColorRes(int normalColor, int selectColor) {
// this.normalColor = MyDrawable.colorByRes(normalColor);
// this.selectColor = MyDrawable.colorByRes(selectColor);
// return this;
// }
public ForeStyle setColor(int normalColor) {
this.normalColor = normalColor;
return this;
}
public ForeStyle setColor(String normalColor) {
this.normalColor = MyDrawable.colorParse(normalColor);
return this;
}
// public ForeStyle setColorRes(int normalColor) {
// this.normalColor = MyDrawable.colorByRes(normalColor);
// return this;
// }
public ForeStyle setColors(int... colors) {
this.colors = colors;
return this;
}
}
}