Android在v7包中更新的 Palette可以讓我們?cè)跇?gòu)造色彩鮮艷的界面時(shí)更加方便,通過一個(gè)圖片的bitmap
來獲取圖片當(dāng)中明暗對(duì)比的顏色
1添加palette
compile 'com.android.support:palette-v7:24.1.1'-
2初始化
- 靜態(tài)初始化
Palette palette =
Palette.from(BitmapFactory.decodeResource(getResources(), R.drawable.demo))
.generate();
- 靜態(tài)初始化
-
異步方法
Palette.from(BitmapFactory.decodeResource(getResources(), R.drawable.demo))
.generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {} }); -
3獲取 Palette.Swatch
Palette.Swatch swatch=palette.getVibrantSwatch();//充滿活力的樣本
swatch=palette.getLightVibrantSwatch();;//亮
swatch=palette.getDarkVibrantSwatch();//暗swatch=palette.getMutedSwatch();//柔和的樣本 swatch=palette.getLightMutedSwatch();//亮 swatch=palette.getDarkMutedSwatch();//暗 4把顏色值賦給控件
mTextView.setBackgroundColor(swatch.getRgb());//設(shè)置控件背景色
mTextView.setTextColor(swatch.getBodyTextColor());//設(shè)置字體顏色