slideDownView优化
This commit is contained in:
parent
9be3666af1
commit
c19f4fd1a5
|
|
@ -38,6 +38,202 @@ import androidx.core.content.ContextCompat;
|
||||||
*/
|
*/
|
||||||
public class SlideDownView extends LinearLayout {
|
public class SlideDownView extends LinearLayout {
|
||||||
|
|
||||||
|
// ==================== Builder 模式支持 ====================
|
||||||
|
/**
|
||||||
|
* Builder类用于以链式调用方式构建SlideDownView实例
|
||||||
|
*/
|
||||||
|
public static class Builder {
|
||||||
|
private final Context context;
|
||||||
|
|
||||||
|
// 可选参数(都有默认值)
|
||||||
|
private String title = "";
|
||||||
|
private float titleSize = 15f;
|
||||||
|
private String content = "";
|
||||||
|
private float contentSize = 12f;
|
||||||
|
private int imageRes = 0;
|
||||||
|
private int type = 0;
|
||||||
|
private String backgroundColor = null;
|
||||||
|
private int hideDuration = 300;
|
||||||
|
private boolean viewCenter = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造函数
|
||||||
|
* @param context 上下文
|
||||||
|
*/
|
||||||
|
public Builder(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置标题文本
|
||||||
|
* @param title 标题
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置标题字号
|
||||||
|
* @param titleSize 字号(sp)
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setTitleSize(float titleSize) {
|
||||||
|
this.titleSize = titleSize;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置内容文本
|
||||||
|
* @param content 内容
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置内容字号
|
||||||
|
* @param contentSize 字号(sp)
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setContentSize(float contentSize) {
|
||||||
|
this.contentSize = contentSize;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置图标资源
|
||||||
|
* @param imageRes 资源ID
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setImage(int imageRes) {
|
||||||
|
this.imageRes = imageRes;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置类型(0=成功绿色,1=错误红色)
|
||||||
|
* @param type 类型
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setType(int type) {
|
||||||
|
this.type = type;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置背景颜色(字符串格式,如#FF0000)
|
||||||
|
* @param color 颜色字符串
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setBackgroundColor(String color) {
|
||||||
|
this.backgroundColor = color;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置背景颜色(ColorInt)
|
||||||
|
* @param color 颜色值
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setBackgroundColor(int color) {
|
||||||
|
this.backgroundColor = "#" + Integer.toHexString(color).substring(2);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置隐藏动画时长
|
||||||
|
* @param duration 时长(毫秒)
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setHideDuration(int duration) {
|
||||||
|
this.hideDuration = duration;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置内容是否居中
|
||||||
|
* @param center 是否居中
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setViewCenter(boolean center) {
|
||||||
|
this.viewCenter = center;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置成功样式(绿色背景)
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setSuccessStyle() {
|
||||||
|
this.type = 0;
|
||||||
|
this.backgroundColor = "#4CB050";
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置错误样式(红色背景)
|
||||||
|
* @return Builder实例
|
||||||
|
*/
|
||||||
|
public Builder setErrorStyle() {
|
||||||
|
this.type = 1;
|
||||||
|
this.backgroundColor = "#FF4443";
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建SlideDownView实例
|
||||||
|
* @return SlideDownView实例
|
||||||
|
*/
|
||||||
|
public SlideDownView build() {
|
||||||
|
SlideDownView view = new SlideDownView(context);
|
||||||
|
view.setTextTitle(this.title);
|
||||||
|
view.setTextTitleSize((int) this.titleSize);
|
||||||
|
view.setTextContent(this.content);
|
||||||
|
view.setTextContentSize((int) this.contentSize);
|
||||||
|
view.setCustomImage(this.imageRes);
|
||||||
|
view.setType(this.type);
|
||||||
|
if (this.backgroundColor != null) {
|
||||||
|
view.setBackgroundColor(this.backgroundColor);
|
||||||
|
}
|
||||||
|
view.setHide_milliseconds(this.hideDuration);
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 便捷静态方法 ====================
|
||||||
|
/**
|
||||||
|
* 创建成功提示视图
|
||||||
|
* @param context 上下文
|
||||||
|
* @param message 消息内容
|
||||||
|
* @return SlideDownView实例
|
||||||
|
*/
|
||||||
|
public static SlideDownView createSuccess(Context context, String message) {
|
||||||
|
return new Builder(context)
|
||||||
|
.setTitle("成功")
|
||||||
|
.setContent(message)
|
||||||
|
.setType(0)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建错误提示视图
|
||||||
|
* @param context 上下文
|
||||||
|
* @param message 消息内容
|
||||||
|
* @return SlideDownView实例
|
||||||
|
*/
|
||||||
|
public static SlideDownView createError(Context context, String message) {
|
||||||
|
return new Builder(context)
|
||||||
|
.setTitle("错误")
|
||||||
|
.setContent(message)
|
||||||
|
.setType(1)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 原有代码 ====================
|
||||||
// 动画相关属性
|
// 动画相关属性
|
||||||
private final boolean isExpanded = false;
|
private final boolean isExpanded = false;
|
||||||
private float mTextTitleSize = 15f;
|
private float mTextTitleSize = 15f;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue