Palette的使用

来源:互联网 发布:国产windows平板推荐 编辑:程序博客网 时间:2024/06/08 19:09

(转载,请注明出处:http://www.kennethyo.me/post/android/palettechu-ji-shi-yong)


相信有不少的人已经开始关注Android Lollipop,全新的Material设计风格让人眼前一亮,Material强调大胆的阴影和高亮搭配,引用那些意料之外和充满活力的颜色。

这里就要说到,不久前Android在v7包中更新的Palette。Palette可以让我们构造色彩鲜艳的界面更加方便,通过一个图片的bitmap来获取图片当中明暗对比的颜色。

初始化Palette

Palette通过一个静态方法进行初始化,并且需要传入一个bitmap,这个bitmap是我们要获取颜色图片的位图。

Palette palette = Palette.generate(bitmap);

还有一个异步方法:

//提供了一个异步方法Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {            @Override            public void onGenerated(Palette palette) {            }});

获取Palette.Swatch

通过以下几种方法获取相应的Palette.SwatchPalette.Swatch是包含我们获取到颜色的一个载体

palette.getVibrantSwatch();palette.getMutedSwatch();palette.getLightVibrantSwatch();palette.getLightMutedSwatch();palette.getDarkVibrantSwatch();palette.getDarkMutedSwatch();

通过Palette.Swatch获取到颜色

针对Android,Palette.Swatch.getRgb()返回一个intRGB颜色值。例如如下的用法:

TextView tv1 = (TextView) findViewById(R.id.tv1);//返回一个活力的颜色tv1.setTextColor(palette.getVibrantSwatch().getRgb());//返回一个柔和的颜色tv1.setBackgroundColor(palette.getMutedSwatch().getRgb());TextView tv2 = (TextView) findViewById(R.id.tv2);//返回一个活力的亮色tv2.setTextColor(palette.getLightVibrantSwatch().getRgb());//返回一个柔和的亮色tv2.setBackgroundColor(palette.getLightMutedSwatch().getRgb());TextView tv3 = (TextView) findViewById(R.id.tv3);//返回一个活力的暗色tv3.setTextColor(palette.getDarkVibrantSwatch().getRgb());//返回一个柔和的暗色tv3.setBackgroundColor(palette.getDarkMutedSwatch().getRgb());TextView tv4 = (TextView) findViewById(R.id.tv4);//返回一个适合做标题的颜色tv4.setTextColor(palette.getVibrantSwatch().getTitleTextColor());//返回一个适合做主题的颜色tv4.setBackgroundColor(palette.getDarkMutedSwatch().getBodyTextColor());


总体上看,Palette的使用简单,没有复杂的方法,更方便我们针对material风格进行开发。
demo地址

2 0