(Reproduced, please indicate the source: http://www.kennethyo.me/post/android/palettechu-ji-shi-yong)
I believe many people have begun to pay attention to Android lollipop. The brand-new material design style is eye-catching. Material emphasizes bold shadows and highlight matching, referencing unexpected and vibrant colors.
We will talk about the palette updated by Android in the V7 package not long ago. Palette makes it easier for us to construct a colorful interface.bitmap
To obtain the color of the image.
Initialize palette
Palette
Initialize through a static method, and you need to inputbitmap
, Thisbitmap
We want to obtain the bitmap of the color image.
Palette palette = Palette.generate(bitmap);
There is also an Asynchronous Method:
// Provides an asynchronous method called palette. generateasync (bitmap, new palette. paletteasynclistener () {@ override public void ongenerated (palette ){}});
Obtain
Palette.Swatch
Obtain the correspondingPalette.Swatch
,Palette.Swatch
Is a carrier that contains the color we get
palette.getVibrantSwatch();palette.getMutedSwatch();palette.getLightVibrantSwatch();palette.getLightMutedSwatch();palette.getDarkVibrantSwatch();palette.getDarkMutedSwatch();
Pass
Palette.Swatch
Obtain color
For Android,Palette.Swatch.getRgb()
Returnsint
RGB color value. For example:
Textview TV1 = (textview) findviewbyid (R. id. TV1); // return a vibrant color tv1.settextcolor (palette. getvibrantswatch (). getrgb (); // return a soft color tv1.setbackgroundcolor (palette. getmutedswatch (). getrgb (); textview TV2 = (textview) findviewbyid (R. id. TV2); // return a vibrant bright color tv2.settextcolor (palette. getlightvibrantswatch (). getrgb (); // returns a soft bright tv2.setbackgroundcolor (palette. getlightmutedswatch (). getrgb (); textview TV3 = (textview) findviewbyid (R. id. TV3); // return a vibrant dark color tv3.settextcolor (palette. getdarkvibrantswatch (). getrgb (); // returns a gentle dark color tv3.setbackgroundcolor (palette. getdarkmutedswatch (). getrgb (); textview TV4 = (textview) findviewbyid (R. id. TV4); // return the color tv4.settextcolor (palette. getvibrantswatch (). gettitletextcolor (); // returns a color tv4.setbackgroundcolor (palette. getdarkmutedswatch (). getbodytextcolor ());
In general, palette is easy to use and there are no complicated methods, making it easier for us to develop the material style.
Demo address
Use of palette