Luca Romagnoli
Luca Romagnoli

Reputation: 12475

set add background color to glide image

I'm using Glide for setting png images (with transparencies) in ImageView in this mode:

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

Is it possible to set the backgroundcolor of the image without setting the background color of ImageView ?

thanks

Upvotes: 6

Views: 7873

Answers (2)

Akaki Kapanadze
Akaki Kapanadze

Reputation: 2672

LayerDrawable should be used:

.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));
    }
});

Upvotes: 2

gauravmehra
gauravmehra

Reputation: 169

This will set background color according to your gif. Glide code:

 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);
                        }

Here is Pojo:

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

    }
}

and add in build.gradle: implementation 'com.android.support:palette-v7:27.1.1'

Upvotes: 1

Related Questions