Android Lollipop new Features-Palette
Palette can extract colors from a single image, and we can incorporate the extracted colors into the app UI to make the UI style more aesthetically pleasing. For example, we can extract the color from the picture set to Actionbar to do the background color, so that the Actionbar color will change with the display of the image.
Palette can be extracted in the following colors:
- Vibrant (energetic)
- Vibrant Dark (vibrant dark color)
- Vibrant light (vibrant bright color)
- Muted (soft)
- Muted dark (soft dark color)
- Muted light (soft bright color)
How to use
To use palette, we need to import the Palette Compatibility Library and Gradle
add the following dependencies.
compile ‘com.android.support:palette-v7:21.0.0‘
In the first step, we need a bitmap object to generate a corresponding palette object. Palette provides four static methods for generating objects.
Palette generate(Bitmap bitmap)
Palette generate(Bitmap bitmap, int numColors)
generateAsync(Bitmap bitmap, PaletteAsyncListener listener)
generateAsync(Bitmap bitmap, int numColors, final PaletteAsyncListener listener)
It is not difficult to see, the generation method is divided into generate
(synchronous) and generateAsync
(asynchronous) Two, if the picture is too large to use the Generate method, may block the main thread, we are more inclined to use generateAsync
the method, in fact, the internal is created AsyncTask
. generateAsync
method requires an PaletteAsyncListener
object to listen for the generated callback. In addition to the required Bitmap
parameters, you can pass in a numColors
parameter specifying the number of colors, which is 16 by default.
The second step, get the Palette object, you can get the extracted color value
Palette.getVibrantSwatch()
Palette.getDarkVibrantSwatch()
Palette.getLightVibrantSwatch()
Palette.getMutedSwatch()
Palette.getDarkMutedSwatch()
Palette.getLightMutedSwatch()
The third step, using color, is to return a sample object in the Get method above, a Swatch
sample object that is an inner class of palette that provides some way to get the final color.
getPopulation()
: Number of pixels in the sample
getRgb()
: The RBG value of the color
getHsl()
: HSL Value of color
getBodyTextColor()
: The color value of the body text
getTitleTextColor()
: Color values for title text
The getRgb()
final color value can be obtained and applied to the UI. getBodyTextColor()
and getTitleTextColor()
can get this color under the text suitable color, so it is convenient for us to set the text color, make the text look more comfortable.
Instance Code
This method may block the main thread, and it is recommended that you use asynchronous methodsPalette Palette=Palette. Generate (bitmap);Extracting bitmap colors asynchronouslyPalette. Generateasync (Bitmap,NewPalette.Paletteasynclistener () {@OverridePublicvoidOngenerated (PalettePalette) {Extraction CompleteA vibrant color.Palette.Swatch Vibrant= Palette. Getvibrantswatch ();A vibrant dark color.Palette.Swatch darkvibrant= Palette. Getdarkvibrantswatch ();Vibrant bright colorsPalette.Swatch lightvibrant= Palette. Getlightvibrantswatch ();Soft colorsPalette.Swatch muted= Palette.getmutedswatch (); //soft dark palette. Swatch darkmuted = palette.getdarkmutedswatch (); //soft bright color palette. Swatch lightmuted = palette.getlightmutedswatch (); Use the color //modify Actionbar background color Getactionbar () .setbackgrounddrawable ( new colordrawable (Vibrant.getrgb ())); //Modify the color of the text Mtextview.settextcolor (Vibrant ... //color application with different effects depending on demand});
Effect
Android Lollipop new Features-Palette