Palette(调色板)官方文档的描述是:用于从图片中提取颜色的工具类。
可以从图片中提取出多种风格的颜色:
- Vibrant(充满活力的)
- Vibrant Dark(充满活力,黑暗的)
- Vibrant Light(充满活力的,明亮的)
- Muted(柔和的)
- Muted Dark(柔和的,黑暗的)
- Muted Light(柔和的,明亮的)
用来获取 Palette的两种途径:
// 同步:适用于较小尺寸的图片
Palette p = Palette.from(bitmap).generate();
// 异步:避免阻塞,适用于加载较大尺寸的图片
Palette.from(bitmap).generate(new PaletteAsyncListener() {
public void onGenerated(Palette p) {
// Use generated instance
}
});
下面使用的是同步的方式提取颜色:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//可能会阻塞线程
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.tree);
Palette palette = Palette.from(bitmap).generate();
if(palette!=null){
int rgb_LightMutedColor = palette.getLightMutedColor(Color.BLUE);
int rgb_DarkMutedColor = palette.getDarkMutedColor(Color.BLUE);
int rgb_VibrantColor = palette.getVibrantColor(Color.BLUE);
int rgb_LightVibrantColor = palette.getLightVibrantColor(Color.BLUE);
int rgb_DarkVibrantColor = palette.getDarkVibrantColor(Color.BLUE);
int rgb_MutedColor = palette.getMutedColor(Color.BLUE);
findViewById(R.id.textview).setBackgroundColor(rgb_LightMutedColor);
findViewById(R.id.textview1).setBackgroundColor(rgb_VibrantColor);
findViewById(R.id.textview2).setBackgroundColor(rgb_DarkMutedColor);
findViewById(R.id.textview3).setBackgroundColor(rgb_LightVibrantColor);
findViewById(R.id.textview4).setBackgroundColor(rgb_DarkVibrantColor);
findViewById(R.id.textview5).setBackgroundColor(rgb_MutedColor);
}else{
Log.i("Tag","提示:"+"为空");
}
}
}
关键的代码其实也就几行,使用起来还是蛮简单的,能有效的提升app的
逼格,配色很重要嘛。
AndroidStudio使用前要添加依赖:
compile 'com.android.support:palette-v7:23.0.1'
下面附上运行效果图: