Hayes121
Hayes121

Reputation: 283

Android Apply Palette

I am trying to use the palette function of androids material design but I am having some trouble in applying it.

I have successfully generated the palette and now I am trying to pass the palette into a function that applies the it.

The problem that I am having is that when I pass in the palette to the applyPalette function none of the methods like palette.getDarkMutedColor().getRgb() , palette.getVibrantColor().getRgb() are being populated with the values from the palette.

The tutorial that I was following didnt mention anything else other then passing in the palette to the function, and in doing so the methods would be populated

This is the generator Function and the applying function, can any one see why this isnt working?

Code

private void colorize(Bitmap photo) {
    Palette palette = new Palette.Builder(photo).generate();
    applyPalette(palette);
}

private void applyPalette(Palette palette) {
    getWindow().setBackgroundDrawable(new ColorDrawable(palette.getDarkMutedColor().getRgb()));

    TextView titleView = (TextView) findViewById(R.id.title);
    titleView.setTextColor(palette.getVibrantColor().getRgb());

    TextView descriptionView = (TextView) findViewById(R.id.description);
    descriptionView.setTextColor(palette.getLightVibrantColor().getRgb());

    colorRipple(R.id.info, palette.getDarkMutedColor().getRgb(),
            palette.getDarkVibrantColor().getRgb());
    colorRipple(R.id.star, palette.getMutedColor().getRgb(),
            palette.getVibrantColor().getRgb());

    View infoView = findViewById(R.id.information_container);
    infoView.setBackgroundColor(palette.getLightMutedColor().getRgb());

    AnimatedPathView star = (AnimatedPathView) findViewById(R.id.star_container);
    star.setFillColor(palette.getVibrantColor().getRgb());
    star.setStrokeColor(palette.getLightVibrantColor().getRgb());
}

Upvotes: 9

Views: 905

Answers (4)

theJango
theJango

Reputation: 1108

First thing I don't know why you didn't get error when you write

palette.getVibrantColor().getRgb()

I will assume you didn't get error ,so you must be using the old library. As in the updated one it accepts one parameter as default color value. To extract RGB better thing is to get Palette.Swatch object and get RGB values. I did create a small working simple app to demo how to use the improved library , You can check that here. Hope this helps.

Upvotes: 0

phxhawke
phxhawke

Reputation: 2611

From the documentation, all of the calls you are using from Palette already return an RGB value but require a default color be passed. Perhaps you meant to use the ones that return a color swatch instead? For example, instead of doing this palette.getVibrantColor().getRgb() you would instead do this palette.getVibrantSwatch().getRgb() instead. Replace all your getColor calls with the appropriate getSwatch() call.

Also, make sure that you have import android.support.v7.graphics.Palette in your imports and that you include compile 'com.android.support:palette-v7:22.1.0' in your dependencies. Version 22.1.0 being the minimum as you are using Palette.Builder.

Upvotes: 0

Tharindu Welagedara
Tharindu Welagedara

Reputation: 2725

You have tried in synchronous way. So I think below code will solve your problem(In asynchronous way).

private void colorize(Bitmap photo) {   
Palette.from(photo).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                applyPalette(palette);
            }
        });
}

Upvotes: 0

Amit Desale
Amit Desale

Reputation: 1291

use picassopalette third party library and import it into your project then use following code:

try {
        ContextWrapper cw = new ContextWrapper(OtherUserProfileScreenActivity.this);
        Picasso.with(this).load(image + ".jpg").placeholder(R.drawable.ic_loading).error(R.drawable.ic_error).into(imageView, PicassoPalette.with(Image + ".jpg", imageView).use(PicassoPalette.Profile.MUTED_DARK).intoCallBack(new BitmapPalette.CallBack() {
            @Override
            public void onPaletteLoaded(Palette palette) {

                int mutedColor = palette.getMutedColor(R.attr.colorPrimary);
                mCollapsingToolbarLayout.setContentScrimColor(mutedColor);
            }
        }));
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
    }

Upvotes: 1

Related Questions