學習palette,好吧我是不知道的,偶然看到,看這個之前又看了一下沉浸式
http://blog.csdn.net/hard_working1/article/details/54318936
在Android API 19以上可以使用****.TranslucentDecor***有關的主題,自帶相應半透明效果,Theme.Holo.NoActionBar.TranslucentDecor和Theme.Holo.Light.NoActionBar.TranslucentDecor兩種主題為新增加的,所以要新建values-v19文件夾并創建styles文件添加如下代碼
實現全屏
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor">
<!--Customize your theme here.-->
</style>
Android 4.4以上可以添加如下代碼
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.KITKAT) { //透明狀態欄
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//透明導航欄
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);}
Android 5.0 以上也可以使用下面的代碼實現全屏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}```
######此時狀態欄占有的位置消失
//在主題里加
<item name="android:fitsSystemWindows">true</item>
//layout
android:fitsSystemWindows="true"
//java代碼里
view.setFitsSystemWindows(true);
######fitsSystemWindows只作用在sdk>=19的系統上就是高于4.4的系統,只要設置了這個屬性此view的所有padding屬性失效.只有在設置了透明狀態欄(StatusBar)或者導航欄(NavigationBar)此屬性才會生效
以上為沉浸式的一部分,接下來Palette
## Palette
Palette的作用是可以從圖像中提取圖片的顏色。我們可以把提取的顏色融入到App UI中,可以使UI風格更加美觀融洽。
附上**dalao MaterialDesign系列控件講解**
https://github.com/loonggg/MaterialDesignDemo
Palette.Swatch s = p.getVibrantSwatch(); //獲取到充滿活力的這種色調
Palette.Swatch s = p.getDarkVibrantSwatch(); //獲取充滿活力的黑
Palette.Swatch s = p.getLightVibrantSwatch(); //獲取充滿活力的亮
Palette.Swatch s = p.getMutedSwatch(); //獲取柔和的色調
Palette.Swatch s = p.getDarkMutedSwatch(); //獲取柔和的黑
Palette.Swatch s = p.getLightMutedSwatch(); //獲取柔和的亮
swatch對象對應的顏色方法
- getPopulation(): 像素的數量
- getRgb(): RGB顏色
- getHsl(): HSL顏色
- getBodyTextColor(): 用于內容文本的顏色
- getTitleTextColor(): 標題文本的顏色
添加依賴
```compile 'com.android.support:palette-v7:23.4.0'```
創建Palette對象,并獲取圖片的顏色值
使用generateAsync方法傳入當前圖片的bitmap,在傳入一個監聽,在監聽里面我們拿到圖片上顏色充滿活力的顏色,最后設置標題背景和字體的顏色
// 用來提取顏色的Bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.bitmap.icon_palette);
// Palette的部分
Palette.Builder builder = Palette.from(bitmap);
builder.generate(new Palette.PaletteAsyncListener() {@Override public void onGenerated(Palette palette) {
//獲取到充滿活力的這種色調
Palette.Swatch vibrant = palette.getVibrantSwatch();
//根據調色板Palette獲取到圖片中的顏色設置到toolbar和tab中背景,標題等,使整個UI界面顏色統一
toolbar_tab.setBackgroundColor(vibrant.getRgb());
toolbar_tab.setSelectedTabIndicatorColor(colorBurn(vibrant.getRgb()));
toolbar.setBackgroundColor(vibrant.getRgb());
if (android.os.Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.setStatusBarColor(colorBurn(vibrant.getRgb()));
window.setNavigationBarColor(colorBurn(vibrant.getRgb()));
}
}
});
/**
* 顏色加深處理
*
* @param RGBValues RGB的值,由alpha(透明度)、red(紅)、green(綠)、blue(藍)構成,
* Android中我們一般使用它的16進制,
* 例如:"#FFAABBCC",最左邊到最右每兩個字母就是代表alpha(透明度)、
* red(紅)、green(綠)、blue(藍)。每種顏色值占一個字節(8位),值域0~255
* 所以下面使用移位的方法可以得到每種顏色的值,然后每種顏色值減小一下,在合成RGB顏色,顏色就會看起來深一些了
* @return
*/
private int colorBurn(int RGBValues) {
int alpha = RGBValues >> 24;
int red = RGBValues >> 16 & 0xFF;
int green = RGBValues >> 8 & 0xFF;
int blue = RGBValues & 0xFF;
red = (int) Math.floor(red * (1 - 0.1));
green = (int) Math.floor(green * (1 - 0.1));
blue = (int) Math.floor(blue * (1 - 0.1));
return Color.rgb(red, green, blue);
}