导入依赖
//加载占位
implementation 'me.samlss:broccoli:1.0.0'
添加带渐变动画的占位符
Broccoli broccoli = new Broccoli();
//添加带渐变动画的占位符
broccoli.addPlaceholder(new PlaceholderParameter.Builder()
.setView(控件名称)
.setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator())
).build());
添加自定义动画的占位符
Broccoli broccoli = new Broccoli();
broccoli.addPlaceholder(new PlaceholderParameter.Builder()
.setView(控件)
.setAnimation(动画名称);
.setDrawable(DrawableUtils.createRectangleDrawable(Color.parseColor("#DDDDDD"), 0))
.build());
添加默认的占位图
broccoli.addPlaceholders(控件, 控件, 控件);
最后在数据加载完成调用方法关闭占位图
//关闭所有
broccoli.clearAllPlaceholders();
//关闭指定
broccoli.clearPlaceholder(控件)
附上DrawableUtils工具类
public class DrawableUtils {
private DrawableUtils(){
throw new UnsupportedOperationException("Can not be instantiated.");
}
public static GradientDrawable createRectangleDrawable(int color, float cornerRadius) {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(cornerRadius);
gradientDrawable.setColor(color);
return gradientDrawable;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static GradientDrawable createRectangleDrawable(int[] colors, float cornerRadius) {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(cornerRadius);
gradientDrawable.setColors(colors);
return gradientDrawable;
}
public static GradientDrawable createOvalDrawable(int color) {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.OVAL);
gradientDrawable.setColor(color);
return gradientDrawable;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static GradientDrawable createOvalDrawable(int[] colors) {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.OVAL);
gradientDrawable.setColors(colors);
return gradientDrawable;
}
}