设置添加背景颜色以滑动图像

时间:2016-02-28 14:04:53

标签: android background android-glide

我在此模式下使用Glide在ImageView中设置png图像(使用透明胶片):

Glide.with(context).load(url)
                .crossFade()
                .placeholder(R.drawable.no_contest)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into((ImageView)container);

是否可以在不设置ImageView的背景颜色的情况下设置图像的背景颜色?

感谢

2 个答案:

答案 0 :(得分:1)

这将根据您的gif设置背景颜色。 滑行代码:

 Glide.with(context).load(newspojo.getImageLink())
                                   .asGif()
                                    .listener(new RequestListener<String, GifDrawable>() {
                                        @Override
                                        public boolean onException(Exception e, String model, Target<GifDrawable> target, boolean isFirstResource) {
                                            return false;
                                        }

                                        @Override
                                        public boolean onResourceReady(GifDrawable resource, String model, Target<GifDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {

                                            final Pojo pojo = new Pojo();
                                            final GifDrawable resources = resource;
                                            Palette.from(resource.getFirstFrame()).generate(new Palette.PaletteAsyncListener() {
                                                @Override
                                                public void onGenerated(@NonNull Palette palette) {

                                                    if (pojo.getPosterPalette() != null) {
                                                        setUpInfoBackgroundColor(holder.ivRow, palette);
                                                    } else {
                                                        Palette.from(resources.getFirstFrame()).generate(new Palette.PaletteAsyncListener() {
                                                            public void onGenerated(Palette palette) {
                                                                pojo.setPosterPalette(palette);
                                                                setUpInfoBackgroundColor(holder.ivRow, palette);
                                                            }
                                                        });
                                                    }
                                                }
                                            });
                                            return false;
                                        }
                                    })
                                    .into(holder.ivRow);
                        }

这是Pojo:

public class Pojo {
    public Palette posterPalette;
    public Palette getPosterPalette() {
        return posterPalette;
    }
    public void setPosterPalette(Palette posterPalette) {
        this.posterPalette = posterPalette;
    }
    public Pojo(){

    }
}

并添加build.gradle: 实施'com.android.support:palette-v7:27.1.1'

答案 1 :(得分:0)

LayerDrawable应该使用:

.into(new SimpleTarget<GlideDrawable>() {

    @Override
    public void onResourceReady(GlideDrawable resource,
                                GlideAnimation<? super GlideDrawable> glideAnimation) {
        final ShapeDrawable background = new ShapeDrawable();
        background.getPaint().setColor("color here");
        final Drawable[] layers = {background, resource};

        container.setImageDrawable(new LayerDrawable(layers));
    }
});