Palette是一個可以從圖片(Bitmap)中提取顏色的幫助類,可以使UI更加美觀,根據圖片動態的顯示相應的顏色。現在最新的api是在版本22.0.0添加的,本篇文章也是使用的22.0.0的api版本(注意版本之間api的不同)。
	
應用項目:https://github.com/DingMouRen/PaletteImageView
應用中的效果 Demo 效果
	
     
Palette可以提取的顏色:
使用方法: module的build.gradle中引用
compile 'com.android.support:palette-v7:25.3.1'
使用步驟:
1.獲取Palette對象,也就是圖像調色板
2.獲取從圖像調色板生成的色樣
3.從色樣中提取相應顏色
1.獲取Palette對象,也就是圖像調色板
獲取Palette對象有同步和異步兩種方式,建議使用異步獲取Palette對象
 // Synchronous Palette p = Palette.from(bitmap).generate(); // Asynchronous Palette.from(bitmap).generate(new PaletteAsyncListener() {   public void onGenerated(Palette p) {     // Use generated instance   } });2.獲取從圖像調色板生成的色樣
可以獲取到六種色樣,但是有的時候獲取不到對應的色樣對象,必須注意非空判斷。
Palette.Swatch vibrant = palette.getVibrantSwatch();//有活力的Palette.Swatch vibrantDark = palette.getDarkVibrantSwatch();//有活力的,暗色Palette.Swatch vibrantLight = palette.getLightVibrantSwatch();//有活力的,亮色Palette.Swatch muted = palette.getMutedSwatch();//柔和的Palette.Swatch mutedDark = palette.getDarkMutedSwatch();//柔和的,暗色Palette.Swatch mutedLight = palette.getLightMutedSwatch();//柔和的,亮色
3.從色樣中提取相應顏色
通過 getRgb() 可以得到最終的顏色值并應用到UI中。getBodyTextColor() 和 getTitleTextColor() 可以得到此顏色下文字適合的顏色,這樣很方便我們設置文字的顏色,使文字看起來更加舒服。
swatch.getPopulation(): 樣本中的像素數量swatch.getRgb(): 顏色的RBG值swatch.getHsl(): 顏色的HSL值swatch.getBodyTextColor(): 主體文字的顏色值swatch.getTitleTextColor(): 標題文字的顏色值
Demo的代碼中沒有對獲取到的色樣對象進行非空判斷,注意一定要加上非空判斷
public class MainActivity extends AppCompatActivity {  private static final String TAG = MainActivity.class.getName();  private LinearLayout line1,line2,line3,line4,line5,line6;  private TextView tv1_1,tv1_2,tv2_1,tv2_2,tv3_1,tv3_2,tv4_1,tv4_2,tv5_1,tv5_2,tv6_1,tv6_2;  private List<LinearLayout> bgs = new ArrayList<>();  private List<TextView> bodyTexts = new ArrayList<>();  private List<TextView> titleTexts = new ArrayList<>();  private List<Palette.Swatch> swatchs = new ArrayList<>();  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    ImageView img = (ImageView) findViewById(R.id.img);    initView();    Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();    if (bitmap == null){      return;    }    Palette.from(bitmap).generate(listener);  }  private Palette.PaletteAsyncListener listener = new Palette.PaletteAsyncListener() {    @Override    public void onGenerated(Palette palette) {      if (palette != null){        Palette.Swatch vibrant = palette.getVibrantSwatch();//有活力的        Palette.Swatch vibrantDark = palette.getDarkVibrantSwatch();//有活力的,暗色        Palette.Swatch vibrantLight = palette.getLightVibrantSwatch();//有活力的,亮色        Palette.Swatch muted = palette.getMutedSwatch();//柔和的        Palette.Swatch mutedDark = palette.getDarkMutedSwatch();//柔和的,暗色        Palette.Swatch mutedLight = palette.getLightMutedSwatch();//柔和的,亮色        swatchs.clear();        swatchs.add(vibrant);swatchs.add(vibrantDark);swatchs.add(vibrantLight);        swatchs.add(muted);swatchs.add(mutedDark);swatchs.add(mutedLight);        show();      }    }  };  private void show() {    for (int i = 0; i < 6; i++) {      bgs.get(i).setBackgroundColor(swatchs.get(i).getRgb());      bodyTexts.get(i).setTextColor(swatchs.get(i).getBodyTextColor());      titleTexts.get(i).setTextColor(swatchs.get(i).getTitleTextColor());        }  }  private void initView() {    line1 = (LinearLayout) findViewById(R.id.line1);    line2 = (LinearLayout) findViewById(R.id.line2);    line3 = (LinearLayout) findViewById(R.id.line3);    line4 = (LinearLayout) findViewById(R.id.line4);    line5 = (LinearLayout) findViewById(R.id.line5);    line6 = (LinearLayout) findViewById(R.id.line6);    bgs.clear();    bgs.add(line1);bgs.add(line2);bgs.add(line3);bgs.add(line4);bgs.add(line5);bgs.add(line6);    tv1_1 = (TextView) findViewById(R.id.tv1_1);    tv2_1 = (TextView) findViewById(R.id.tv2_1);    tv3_1 = (TextView) findViewById(R.id.tv3_1);    tv4_1 = (TextView) findViewById(R.id.tv4_1);    tv5_1 = (TextView) findViewById(R.id.tv5_1);    tv6_1 = (TextView) findViewById(R.id.tv6_1);    tv1_2 = (TextView) findViewById(R.id.tv1_2);    tv2_2 = (TextView) findViewById(R.id.tv2_2);    tv3_2 = (TextView) findViewById(R.id.tv3_2);    tv4_2 = (TextView) findViewById(R.id.tv4_2);    tv5_2 = (TextView) findViewById(R.id.tv5_2);    tv6_2 = (TextView) findViewById(R.id.tv6_2);    bodyTexts.clear();titleTexts.clear();    bodyTexts.add(tv1_1);bodyTexts.add(tv2_1);bodyTexts.add(tv3_1);bodyTexts.add(tv4_1);bodyTexts.add(tv5_1);bodyTexts.add(tv6_1);    titleTexts.add(tv1_2);titleTexts.add(tv2_2);titleTexts.add(tv3_2);titleTexts.add(tv4_2);titleTexts.add(tv5_2);titleTexts.add(tv6_2);  }}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答