2014-11-03 68 views
0

是否有適用於Android的API,可讓您提取主要背景顏色以創建透明覆蓋圖,以便我可以在其上編寫文本。用於提取背景顏色的材質設計API

例如,如果4張圖像的主色爲棕色,則覆蓋層爲透明棕色。

我期待創建類似以下圖像的東西,以在圖片頂部添加信息。它來自Google播放音樂。

google play music

謝謝

+1

你應該看看新的[Palette API](https://developer.android.com/reference/android/support/v7/graphics/Palette.html)和[there](https://developer.android .com/training/material/drawables.html#ColorExtract)是這個API函數的總和 – 2014-11-03 12:14:31

+0

謝謝!!這就是我正在尋找:) – wheeliez 2014-11-03 12:20:46

+0

我創建了一個答案,這是一個簡單的例子,你可以如何使用它來解決你的PB;) – 2014-11-03 12:27:48

回答

1

有可能是一個解決方案:

我假設你把4的ImageView在RelativeLayout的(稱爲parentLyt)

RelativeLayout view = (RelativeLayout) findViewById(R.id.parentLyt); 
view.setDrawingCacheEnabled(true); 
view.buildDrawingCache(); 

現在你可以讓你的4個圖像的位圖:

Bitmap bitmap = view.getDrawingCache(); 

並通過此位圖使用Palette API:

Palette colorPalette = Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() { 
    public void onGenerated(Palette palette) { 
     int color = palette.getLightVibrantSwatch().getRgb(); 
     // Do something with colors...just for example : 
     // Convert to hex format and add 88 alpha channel "#88RRGGBB" 
     String transparentColor = String.format("#88%06X", 0xFFFFFF & intColor); 
     // Convert back to Color object aka int 
     int finalColor = Color.parseColor(transparentColor); 
    } 
}); 
0

您可以使用Alpha來實現這種透明度。所以當你定義一種顏色時,你通常使用#RRGGBB(紅綠藍),但是你也可以使用#AARRGGBB,你也可以指定alpha。
#00RRGGBB是完全透明的,#80RRGGBB就像半透明。您可以選擇一種顏色並將其用作背景。

+0

我知道阿爾法透明度,但我問一個函數來檢索主色形成背景的圖像。然後使用該顏色作爲圖像的透明前景。對不起,如果問題不清楚,我更新它。 – wheeliez 2014-11-03 12:10:06

+0

好吧,'主導'有所作爲:) – helleye 2014-11-03 12:53:02