<span id="Label3"></p><p><p></p></p><p><p></p></p><p><p><strong>Selecting colors using the Palette API</strong></p></p><p><p></p></p><p><p>Good visual design is essential to the success of the app, and the color design system is the foundation of the DESIGN. The palette package is a support package that can parse out the highlighted colors from the image to help you create visually appealing apps</p></p><p><p>You can use the palette package to design layout themes and apply custom colors to visual Elements. For example, you can create a colored title card for a song based on the album cover, palette, or adjust the toolbar color when the background image Changes. Palette object gives you permission to access the color inside the bitmap image, and also provides 6 main colors according to bitmap, for you to fill out the design options.</p></p><p><p></p></p><p><p><strong>Setting dependencies</strong></p></p><p><p></p></p><p><p>Palette is introduced for libraries from version Android support Library 24.0.0, You can add palette dependencies in Build.gradle as Follows:</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #000000;">Android {</span><span style="color: #008080;">2</span> compilesdkversion<span style="color: #008080;">3</span><span style="color: #000000;"> </span> ... <span style="color: #008080;">4</span> <span style="color: #000000;">}</span><span style="color: #008080;">5</span><span style="color: #008080;">6</span><span style="color: #000000;">Dependencies {</span><span style="color: #008080;">7</span><span style="color: #000000;"> ...</span> <span style="color: #008080;">8</span> Implementation ' com.android.support:palette-v7:27.1.1 '<span style="color: #008080;">9</span> }</pre></pre><p><p></p></p><p><p><strong>Create a Palette object</strong></p></p><p><p></p></p><p><p>Palette gives you permission to access the basic colors in the picture, as well as the colors that correspond to the overlapping Text. It can dynamically change the color system of the application by using palette to design the application style according to the given resource Picture.</p></p><p><p>To create a palette, first instantiate the Palette.builder from the bitmap. You can then use Palette.builder to customize the palette before you generate Palette.</p></p><p><p></p></p><p><p><strong>Generating Palette instances</strong></p></p><p><p></p></p><p><p>first, generate Palette.builder from Bitmap using the palette from (Bitmap) method. This builder is then used to generate palette synchronously or asynchronously. If you want to create a palette object in the thread called by the Generate () method, use the synchronous palette Build. If you want to generate palette asynchronously on a different thread, you will use the Palette.PaletteAsyncListener.onGenerated method to access it immediately after the palette is Generated.<br>The following code snippet shows two different ways to generate a palette:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Generate palette synchronously and return it</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Palette createpalettesync (Bitmap Bitmap) {</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span>Palette p =<span style="color: #000000;"><span style="color: #000000;">palette.from (bitmap). Generate ();</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">p;</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Generate Palette asynchronously and use it on a different</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">thread using ongenerated ()</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Createpaletteasync (Bitmap Bitmap) {</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span>Palette.from (bitmap). Generate (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Paletteasynclistener () {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">ongenerated (Palette P) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">use <span style="color: #008000;">generated Instance</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> });</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>}</pre><p><p><br>If you need to continuously generate multiple palette for multiple sequential images, Consider caching the palette instance to avoid slowing UI Performance. You should also not create palette in the main thread.</p></p><p><p></p></p><p><p><strong>Custom Palette</strong></p></p><p><p></p></p><p><p>Palette.builder allows you to customize the Palette by selecting the number of colors from the result Palette, builder uses what area of the picture to generate Palette, what colors are allowed in Palette, and so On. For example, you can filter out black, or builder can only use the top half of the image to generate Palette.</p></p><p><p>You can fine-tune palette dimensions and colors by using the following methods in Palette.builder:</p></p> <ul> <ul> <li>AddFilter (): This method adds a filter to indicate what color is allowed in the result Palette. Pass in your palette.filter and modify the Isallowed () method to determine which methods palette filter.</li> <li>Maximumcolorcount (): This method sets the maximum number of colors in the Palette. The default value is 16, and the optimal value depends on the source Graph. For landscapes, the best values are in 8~16, and the portraits usually have 16~24 values. The more color the palette.builder, the more time it takes to generate Palette.</li> <li>Setregion (): This method indicates what area of bitmap the builder uses when generating Palette. You can only use this method to generate palette from bitmap without affecting the source Graph.</li> <li>AddTarget (): This method allows you to perform your own color matching by adding a target color profile to the Builder. If the default target is not valid, advanced developers can use Target.builder to create their own target.</li> </ul> </ul><p><p></p></p><p><p><strong>Extracting a color profile</strong></p></p><p><p></p></p><p><p>Based on the material design criteria, The Palette library provides a common color profile from the Picture. Each file is defined by a target, and the color extracted from the bitmap image is graded on the file based on saturation, brightness and distribution (number of pixels in the bitmap that represent the color). For each file, the color with the best score defines the color profile for the given Picture.</p></p><p><p>By default, The Palette object contains 16 basic colors for a given picture. When generating palette, You can customize the number of colors by palette.builder. Extracting more colors provides more potential matches for each color profile, but it also causes Palette.builder to take longer to generate Palette.</p></p><p><p>The Palette library tries to extract the following 6 color profiles:</p></p> <ul> <ul> <li>Light Vibrant</li> <li>Vibrant</li> <li>Dark Vibrant</li> <li>Light muted</li> <li>Muted</li> <li>Dark muted</li> </ul> </ul><p><p>Each Palette.get<profile>color () method returns the color in palette, and each palette is associated with a specific profile, where <Profile> is superseded by the above 6 color profile Names. For example, the way to get the dark vibrant color profile is Getdarkvibrantprofile (). Because not all of the images will contain all the color profiles, so you must also provide a default color to Return.</p></p><p><p></p></p><p><p></p></p><p><p><strong>Create a color system with samples</strong></p></p><p><p></p></p><p><p>The Palette object also generates a Palette.swatch object for each color Profile. The Palette.swatch object contains the pixel distribution of the color and color associated with the Object's configuration File.</p></p><p><p>Swatch has an extra way to access color profiles for more information, such as HSL values, pixel distributions, and so On. You can generate more complex color systems and application themes by using Getbodytextcolor () and Gettitletextcolor (). The colors returned by these methods exactly match the Swatch's Color.</p></p><p><p>Each Palette.get<profile>swatch () method returns a Swatch that associates a specific configuration File. Although the Get<profile>swatch () method does not have a default value parameter, the method returns null if a particular configuration file does not exist in the Picture. From this, you should determine if it is empty before using Swatch. For example, The following code gets the caption text color from palette and checks to see if vibrant Swatch is empty:</p></p><pre><pre><span style="color: #008080;">1</span> palette.swatch vibrant =<span style="color: #000000;"> Mypalette.getvibrantswatch ();</span> <span style="color: #008080;">2</span> <span style="color: #0000ff;">if</span> <span style="color: #0000ff;">NULL</span> <span style="color: #000000;">) {</span><span style="color: #008080;">3</span> <span style="color: #0000ff;">int</span> titlecolor =<span style="color: #000000;"> vibrant.gettitletextcolor ();</span> <span style="color: #008080;">4</span> <span style="color: #008000;">//</span> <span style="color: #008000;"> ...</span> <span style="color: #008080;">5</span> }</pre></pre><p><p><br>To access all colors in palette, the Getswatches () method returns a list of all the swatch generated in the picture, including a standard 6 color profile.</p></p><p><p>The following code snippet uses the code snippet above, synchronously generates the palette, acquires the vibrant Swatch, changes the toolbar color to match the bitmap Image. The image below shows the results and toolbar.</p></p><p><p></p></p><p><p></p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set the background and text colors of a toolbar given a</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">bitmap image to match</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Settoolbarcolor (Bitmap Bitmap) {</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Generate the palette and get the vibrant Swatch</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">See the <span style="color: #008000;">Createpalettesync () method</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">From the <span style="color: #008000;">code snippet above</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span>Palette p =<span style="color: #000000;"><span style="color: #000000;">Createpalettesync (bitmap);</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span>Palette.swatch Vibrantswatch =<span style="color: #000000;"><span style="color: #000000;">P.getvibrantswatch ();</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008080;"><span style="color: #008080;">Ten</span></span>Toolbar Toolbar =<span style="color: #000000;"><span style="color: #000000;">(Toolbar) Findviewbyid (r.id.toolbar);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Load Default Colors</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>BackgroundColor =<span style="color: #000000;"><span style="color: #000000;">Contextcompat.getcolor (getcontext (),</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">r.color.default_title_background);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>TextColor =<span style="color: #000000;"><span style="color: #000000;">Contextcompat.getcolor (getcontext (),</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">r.color.default_title_color);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Check The vibrant swatch is available</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(vibrantswatch! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">){</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>BackgroundColor =<span style="color: #000000;"><span style="color: #000000;">Vibrantswatch.getrgb ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>TextColor =<span style="color: #000000;"><span style="color: #000000;">Vibrantswatch.gettitletextcolor ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set the toolbar background and text colors</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Toolbar.setbackgroundcolor (backgroundcolor);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">Toolbar.settitletextcolor (textcolor);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p></p></p><p><p>Android Support palette using the detailed</p></p></span>
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