Android Lollipop new Features-Palette

Source: Internet
Author: User

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 . generateAsyncmethod 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.