Palette:
引入v7里面的一個單獨項目Palette, android.support.graphics.Palette;
Palette 可以在一張圖片里分析出一些色彩特性,主色調,鮮艷的顏色,柔和的顏色等等。。
簡單 代碼實現:
Layout布局
<ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@mipmap/aaa" />MainActivity.java中帶嗎第一步 獲取imageview中的圖片
BitmapDrawable drawable = (BitmapDrawable)iv.getDrawable();Bitmap bitmap = drawable.getBitmap();第二步 獲取Palette實例 設置監聽事件Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { @Override public void onGenerated(Palette palette) { int lightMutedColor = palette.getLightMutedColor(Color.RED); 獲取一個圖片中比較柔和的顏色,如果獲取不到 設為紅色 Palette.Swatch darkMutedSwatch = palette.getDarkMutedSwatch(); text.setTextColor(darkMutedSwatch.getTitleTextColor()); 給text設置字體顏色 text.setBackgroundColor(getTransColor(0.5f,darkMutedSwatch.getRgb())); 給text設置背景顏色 0.5 設置透明度 }});/** * textview 背景 透明 * @param darkMutedSwatch * @return */PRivate int getTransColor(float percent,int darkMutedSwatch) { int i = darkMutedSwatch & 0xfff; int i1 = darkMutedSwatch >> 8 & 0xff; int i2 = darkMutedSwatch >> 16 & 0xff; int i3 = darkMutedSwatch >>> 24; //透明度 percent i3 = Math.round(i3* percent); return Color.argb(i,i1,i2,i3);}
新聞熱點
疑難解答