Skip to content
Permalink
Browse files

由于项目具体内容,增加了根据header的颜色来改变statusbar

  • Loading branch information
gaoahui
gaoahui committed May 12, 2017
1 parent 06dc4aa commit 23037e7b55e66f4fdaef59feb351ea83405e6946
@@ -3,10 +3,9 @@
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.githang.statusbar.StatusBarCompat;
import com.githang.statusbar.StatusBarUtil;
import com.githang.statusbarcompat.demo.R;
import com.larswerkman.holocolorpicker.ColorPicker;
import com.larswerkman.holocolorpicker.SVBar;

public class MainActivity extends AppCompatActivity implements ColorPicker.OnColorChangedListener {

@@ -15,17 +14,29 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ColorPicker picker = (ColorPicker) findViewById(R.id.picker);
SVBar svBar = (SVBar) findViewById(R.id.svbar);

picker.addSVBar(svBar);
picker.setOldCenterColor(picker.getColor());
picker.setOnColorChangedListener(this);
// ColorPicker picker = (ColorPicker) findViewById(R.id.picker);
// SVBar svBar = (SVBar) findViewById(R.id.svbar);
//
// picker.addSVBar(svBar);
// picker.setOldCenterColor(picker.getColor());
// picker.setOnColorChangedListener(this);
//
StatusBarUtil.enableStatusBar(this);









}



@Override
public void onColorChanged(int color) {
StatusBarCompat.setStatusBarColor(this, color);
StatusBarUtil.setStatusBarColor(this, color);
}
}
@@ -11,7 +11,8 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
android:text="Hello World!"
android:background="@color/colorPrimary"/>
<com.larswerkman.holocolorpicker.ColorPicker
android:id="@+id/picker"
android:layout_width="wrap_content"
@@ -1,5 +1,7 @@
package com.githang.statusbar;

import android.app.Activity;
import android.view.View;
import android.view.Window;

/**
@@ -39,7 +39,7 @@ public void setStatusBarColor(Window window, int color) {
decorViewGroup.addView(statusBarView);
}
statusBarView.setBackgroundColor(color);
StatusBarCompat.setFitsSystemWindows(window, true);
StatusBarUtil.setFitsSystemWindows(window, true);
}

private static int getStatusBarHeight(Context context) {
@@ -3,6 +3,8 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Environment;
import android.view.View;
@@ -23,7 +25,7 @@
* @since 0.1
*/

public class StatusBarCompat {
public class StatusBarUtil {

static final IStatusBar IMPL;

@@ -72,6 +74,42 @@ public static void setStatusBarColor(Activity activity, int color) {
setStatusBarColor(activity, color, isLightColor);
}

/**
* 使用限制: 显示的为布局中第一个有backgroud的控件的颜色
* 并且 设置的为colordrawable
* 其他条件下使用 setStatusBarColor();
* 或者自定义
*
* @param activity
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void enableStatusBar(Activity activity){
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
getHeaderColor(activity, viewGroup);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
static void getHeaderColor(Activity activity,View view){
if (view instanceof ViewGroup){
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View childView = ((ViewGroup) view).getChildAt(i);
Drawable drawable = childView.getBackground();
if (drawable instanceof ColorDrawable){
int color = ((ColorDrawable)drawable).getColor();
setStatusBarColor(activity,color);
}else {
getHeaderColor(activity,childView);
}
}
}else {
Drawable drawable = view.getBackground();
if (drawable instanceof ColorDrawable){
int color = ((ColorDrawable)drawable).getColor();
setStatusBarColor(activity,color);
}
}
}

/**
* 把颜色转换成灰度值。
* 代码来自 Flyme 示例代码

0 comments on commit 23037e7

Please sign in to comment.
You can’t perform that action at this time.