一般情况下我们的app,都会有banner图,而banner图配合透明状态栏会让人感觉更美观,废话不多说先上图
思路
其实要实现透明状态栏只需要给window 设置Flags属性,让应用的主体内容占用系统状态栏的空间,最后再调用Window的setStatusBarColor()方法将状态栏设置成透明色就可以了,而setStatusBarColor()只有版本大于等于5.0才可以调用,适配的时候而有的5.0手机也有不支持此方法的,所以当低于6.0时,我们创建一个和状态栏大小相等的view,覆盖在状态栏的位置,然后给这个view设置颜色和透明度。
值得注意的是,设置Flags属性只适用于4.4以上版本,有一部分4.4手机也不支持此方法,所以适配5.0及以上即可。下边上代码:
public class LzhStatusBarUtils {
public static final int IS_SET_PADDING_KEY = 123456789;
private static final int TRANSLUCENT_VIEW_ID = R.id.translucent_view;
//
/**
* 设置ImageView为第一控件的可以调整透明度的状态栏
*
* @param activity
*/
public static void setTranslucentStatusBar(Activity activity, View topView, int alpha) {
setARGBStatusBar(activity, topView, 0, 0, 0, alpha);
}
//
/**
* 设置透明状态栏版本的状态栏的ARGB
* @param activity
* @param topView
* @param r
* @param g
* @param b
* @param alpha
*/
public static void setARGBStatusBar(Activity activity, View topView, int r, int g, int b, int alpha) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().setStatusBarColor(Color.argb(alpha, r, g, b));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//activity.getWindow().setStatusBarColor(Color.argb(alpha, r, g, b));
setARGBStatusViewToAct(activity, r, g, b, alpha);
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
setARGBStatusViewToAct(activity, r, g, b, alpha);
}
if (topView != null) {
boolean isSetPadding = topView.getTag(IS_SET_PADDING_KEY) != null;
if (!isSetPadding) {
topView.setPadding(topView.getPaddingLeft(), topView.getPaddingTop() + getStatusBarHeight(activity), topView.getPaddingRight(), topView.getPaddingBottom());
topView.setTag(IS_SET_PADDING_KEY, true);
}
}
}
//
/**
* 在有fragment的activity中使用
* 注:需要在有状态栏的fragment的最顶端加一个状态栏大小的view
*
* @param activity
* @param alpha
*/
public static void setTranslucentForImageViewInFragment(Activity activity, int alpha) {
setTranslucentStatusBar(activity, null, alpha);
}
//
/**
* 设置状态栏view的ARGB,如果找到状态栏view则直接设置,否则创建一个再设置
*
* @param activity
* @param statusBarAlpha
*/
private static void setARGBStatusViewToAct(Activity activity, int r, int g, int b, int statusBarAlpha) {
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
View fakeStatusBarView = contentView.findViewById(TRANSLUCENT_VIEW_ID);
if (fakeStatusBarView != null) {
if (fakeStatusBarView.getVisibility() == View.GONE) {
fakeStatusBarView.setVisibility(View.VISIBLE);
}
fakeStatusBarView.setBackgroundColor(Color.argb(statusBarAlpha, r, g, b));
} else {
contentView.addView(createARGBStatusBarView(activity, r, g, b, statusBarAlpha));
}
}
//
/**
* 创建和状态栏一样高的矩形,用于改变状态栏ARGB
*
* @param activity
* @param r
* @param g
* @param b
* @param alpha
* @return
*/
private static View createARGBStatusBarView(Activity activity, int r, int g, int b, int alpha) {
// 绘制一个和状态栏一样高的矩形
View statusBarView = new View(activity);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(Color.argb(alpha, r, g, b));
statusBarView.setId(TRANSLUCENT_VIEW_ID);
return statusBarView;
}
//
/**
* 得到statusbar高度
*
* @param activity
* @return
*/
private static int getStatusBarHeight(Activity activity) {
int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
return activity.getResources().getDimensionPixelSize(resourceId);
}
}
调用的话只需要 LzhStatusBarUtils.setTranslucentStatusBar(this,null,10);就可以了。
知识补充: gif图上的导航栏渐变效果
因为运用的RecyclerView,而RecyclerView有一个滑动监听方法,不啰嗦上代码.
rc.addOnScrollListener(new RecyclerView.OnScrollListener() {
private int totalDy = 0;
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalDy += dy;
if (totalDy <= bannerHeight) {
float alpha = (float) totalDy / bannerHeight;
toolbar.setAlpha(alpha);
} else {
toolbar.setAlpha(1.0f);
}
}
});
在这个方法进行滑动监听,根据距离设置透明度。