How to use the palette in Android

Jieyi Wu
Jieyi Wu
Aug 27, 2017 · 2 min read

Sometimes we wanna extract the colors from the image so how can we do?

Start

Add the library in your app gradle.

compile 'com.android.support:palette-v7:25.1.0'

We have four ways to create the palette.

// Now Palette.from(bitmap) is the same as Palette.Builder(bitmap).

// Synchronous methods.
// --------------------------------
// These should be used when you have access to the underlying image loading thread.
// Picasso allows this through a Transformation. For other libraries, YMMV.
// Uses the default palette size (16).
Palette p = Palette.from(bitmap).generate();
// Allows you to specify the maximum palette size, in this case 24.
Palette p = Palette.from(bitmap).maximumColorCount(24).generate();

// Asynchronous methods
// --------------------------------
// This is the quick and easy integration path. Internally uses an AsyncTask so
// this may not be optimal (since you're dipping in and out of threads)
// Uses the default palette size (16).
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
// Here's your generated palette
}
});
// Allows you to specify the maximum palette size, in this case 24.
Palette.from(bitmap).maximumColorCount(24).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
// Here's your generated palette
}
});

Six swatchs

We often use Vibrant and Dark Vibrant.

// Vibrant.
Palette.getVibrantSwatch()
// Vibrant dark.
Palette.getDarkVibrantSwatch()
// Vibrant light.
Palette.getLightVibrantSwatch()
// Muted.
Palette.getMutedSwatch()
// Muted dark.
Palette.getDarkMutedSwatch()
// Muted light.
Palette.getLightMutedSwatch()
)
Jieyi Wu

Written by

Jieyi Wu

Making the impossible possible. github(https://github.com/pokk) gitbook(https://www.gitbook.com/@pokk)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade