美文网首页
android View预加载占位图

android View预加载占位图

作者: 我要棒棒糖 | 来源:发表于2021-06-22 11:29 被阅读0次
微信图片_20210621174528.png
导入依赖
    //加载占位
    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());
10123010-cb3695898740451c.gif
添加自定义动画的占位符
Broccoli   broccoli = new Broccoli();
broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                        .setView(控件)
                        .setAnimation(动画名称);
                        .setDrawable(DrawableUtils.createRectangleDrawable(Color.parseColor("#DDDDDD"), 0))
                        .build()); 
10123010-bce04accbf082b5c.gif
添加默认的占位图
broccoli.addPlaceholders(控件, 控件, 控件); 
10123010-63fa53eab2250106.gif
最后在数据加载完成调用方法关闭占位图
//关闭所有
     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;
    }
}

相关文章

网友评论

      本文标题:android View预加载占位图

      本文链接:https://www.haomeiwen.com/subject/qnipyltx.html