Commit b22dc20a0fe9dbe0f0b47818693e8420aa2ff3ed

Authored by YangShuai
1 parent f40dc0f9

优化基类

app/strike/src/main/java/com/cnlive/strike/MyFragment.java
... ... @@ -5,9 +5,9 @@ import android.os.Bundle;
5 5 import android.view.LayoutInflater;
6 6 import android.view.View;
7 7 import android.view.ViewGroup;
8   -import android.widget.TextView;
9 8  
10 9 import com.cnlive.libs.base.frame.fragment.BaseFragment;
  10 +import com.cnlive.libs.base.util.StatusBarConfig;
11 11  
12 12 import androidx.annotation.NonNull;
13 13 import androidx.annotation.Nullable;
... ... @@ -22,7 +22,8 @@ public class MyFragment extends BaseFragment<MyFragmentView, MyPresenter> {
22 22  
23 23 @Override
24 24 protected void initView() {
25   -
  25 + setStatusBarColor(R.color.green, 0);
  26 + setStatusBarTextColor(StatusBarConfig.BLACK);
26 27 }
27 28  
28 29 @Override
... ... @@ -31,4 +32,5 @@ public class MyFragment extends BaseFragment<MyFragmentView, MyPresenter> {
31 32 getMvpView().setData("test");
32 33 }
33 34  
  35 +
34 36 }
... ...
app/strike/src/main/java/com/cnlive/strike/MyFragmentView.java
... ... @@ -13,10 +13,10 @@ public class MyFragmentView extends BaseView {
13 13 // *
14 14 // * @param fragment
15 15 // */
16   -// public MyFragmentView(MyFragment fragment) {
  16 + public MyFragmentView(MyFragment fragment) {
17 17 // this.fragment = fragment;
18   -// Log.e(TAG, "MyFragmentView: " + fragment);
19   -// }
  18 + Log.e(TAG, "MyFragmentView: " + fragment);
  19 + }
20 20  
21 21 public void setData(String msg) {
22 22 Log.e(TAG, "setData: ");
... ...
core/base/src/main/java/com/cnlive/libs/base/frame/activity/BaseActivity.java
... ... @@ -13,11 +13,16 @@ import android.view.WindowManager;
13 13 import android.view.inputmethod.InputMethodManager;
14 14 import android.widget.Toast;
15 15  
  16 +import androidx.annotation.ColorRes;
  17 +import androidx.annotation.IntRange;
16 18 import androidx.annotation.Nullable;
17 19 import androidx.appcompat.app.AppCompatActivity;
  20 +import androidx.core.content.ContextCompat;
18 21 import androidx.databinding.DataBindingUtil;
19 22 import androidx.databinding.ViewDataBinding;
20 23  
  24 +import com.cnlive.libs.base.util.StatusBarConfig;
  25 +import com.cnlive.libs.base.util.StatusBarUtil;
21 26 import com.cnlive.libs.base.util.lib_permisshelper.PermissionsUtils;
22 27 import com.cnlive.libs.base.util.ActivityCollector;
23 28  
... ... @@ -210,4 +215,25 @@ public abstract class BaseActivity extends AppCompatActivity {
210 215 //activity管理
211 216 ActivityCollector.removeActivity(this);
212 217 }
  218 +
  219 + /**
  220 + * 设置状态栏颜色
  221 + *
  222 + * @param color
  223 + * @param statusBarAlpha
  224 + */
  225 + protected void setStatusBarColor(@ColorRes int color, @IntRange(from = 0, to = 255) int statusBarAlpha) {
  226 + StatusBarUtil.setColor(this, ContextCompat.getColor(this, color), statusBarAlpha);
  227 + }
  228 +
  229 + /**
  230 + * 设置状态栏字体颜色
  231 + */
  232 + protected void setStatusBarTextColor(int type) {
  233 + if (type == StatusBarConfig.BLACK) {
  234 + StatusBarUtil.setStatusBarWrite(this);
  235 + } else if (type == StatusBarConfig.WHITE) {
  236 + StatusBarUtil.setStatusBarDarkMode(this);
  237 + }
  238 + }
213 239 }
214 240 \ No newline at end of file
... ...
core/base/src/main/java/com/cnlive/libs/base/frame/fragment/BaseFragment.java
1 1 package com.cnlive.libs.base.frame.fragment;
2 2  
3 3 import android.os.Bundle;
4   -import android.view.LayoutInflater;
5 4 import android.view.View;
6   -import android.view.ViewGroup;
7 5  
  6 +import androidx.annotation.ColorRes;
  7 +import androidx.annotation.IntRange;
8 8 import androidx.annotation.NonNull;
9 9 import androidx.annotation.Nullable;
  10 +import androidx.core.content.ContextCompat;
10 11  
11 12 import com.cnlive.libs.base.frame.presenter.BasePresenter;
12 13 import com.cnlive.libs.base.frame.view.BaseView;
13 14 import com.cnlive.libs.base.util.ReflectUtil;
14   -import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
15   -import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
16   -import com.hannesdorfmann.mosby3.mvp.MvpView;
  15 +import com.cnlive.libs.base.util.StatusBarConfig;
  16 +import com.cnlive.libs.base.util.StatusBarUtil;
17 17  
18 18 /**
19 19 * 基类fragment
... ... @@ -23,7 +23,8 @@ import com.hannesdorfmann.mosby3.mvp.MvpView;
23 23 * @author ys
24 24 */
25 25 public abstract class BaseFragment<V extends BaseView, P extends BasePresenter<V>> extends com.hannesdorfmann.mosby3.mvp.MvpFragment<V, P> {
26   - public V views;
  26 + public V view;
  27 + public P presenter;
27 28  
28 29 @Override
29 30 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
... ... @@ -36,21 +37,27 @@ public abstract class BaseFragment&lt;V extends BaseView, P extends BasePresenter&lt;V
36 37  
37 38 @NonNull
38 39 public V createView() {
39   - return ReflectUtil.instanceView(0, this.getClass(), this, BaseView.class);
  40 + if (null == view) {
  41 + view = ReflectUtil.instanceView(0, this.getClass(), this, BaseView.class);
  42 + }
  43 + return view;
40 44 }
41 45  
42 46 @Override
43 47 public P createPresenter() {
44   - return ReflectUtil.instancePresenter(1, this.getClass(), BasePresenter.class);
  48 + if (null == presenter) {
  49 + presenter = ReflectUtil.instancePresenter(1, this.getClass(), BasePresenter.class);
  50 + }
  51 + return presenter;
45 52 }
46 53  
47 54 @NonNull
48 55 @Override
49 56 public V getMvpView() {
50   - if (null == views) {
  57 + if (null == view) {
51 58 return createView();
52 59 } else {
53   - return views;
  60 + return view;
54 61 }
55 62 }
56 63  
... ... @@ -59,4 +66,27 @@ public abstract class BaseFragment&lt;V extends BaseView, P extends BasePresenter&lt;V
59 66 protected abstract void initView();
60 67  
61 68 protected abstract void initData();
  69 +
  70 + /**
  71 + * 设置状态栏颜色
  72 + *
  73 + * @param color
  74 + * @param statusBarAlpha
  75 + */
  76 + protected void setStatusBarColor(@ColorRes int color, @IntRange(from = 0, to = 255) int statusBarAlpha) {
  77 + StatusBarUtil.setColor(getActivity(), ContextCompat.getColor(getActivity(), color), statusBarAlpha);
  78 + }
  79 +
  80 + /**
  81 + * 设置状态栏字体颜色
  82 + */
  83 + protected void setStatusBarTextColor(int type) {
  84 + if (type == StatusBarConfig.BLACK) {
  85 + StatusBarUtil.setStatusBarWrite(getActivity());
  86 + } else if (type == StatusBarConfig.WHITE) {
  87 + StatusBarUtil.setStatusBarDarkMode(getActivity());
  88 + }
  89 + }
  90 +
  91 +
62 92 }
... ...
core/base/src/main/java/com/cnlive/libs/base/media/picker/ui/EditVideoActivity.java
... ... @@ -36,7 +36,7 @@ import com.cnlive.libs.base.media.picker.view.RangeSeekBar;
36 36 import com.cnlive.libs.base.util.AlertUtil;
37 37 import com.cnlive.libs.base.util.ExtractVideoInfoUtil;
38 38 import com.cnlive.libs.base.util.PictureUtils;
39   -import com.cnlive.libs.base.util.StatusBarUtil2;
  39 +import com.cnlive.libs.base.util.StatusBarUtil;
40 40 import com.cnlive.libs.video.video.VideoView;
41 41 import com.cnlive.strike.base.R;
42 42 import com.qiniu.pili.droid.shortvideo.PLShortVideoTrimmer;
... ... @@ -105,7 +105,7 @@ public class EditVideoActivity extends AppCompatActivity implements View.OnClick
105 105 super.onCreate(savedInstanceState);
106 106 setContentView(R.layout.activity_edit_video);
107 107 QMUIStatusBarHelper.setStatusBarDarkMode(this);
108   - StatusBarUtil2.setColor(this, Color.BLACK, 0);
  108 + StatusBarUtil.setColor(this, Color.BLACK, 0);
109 109 initData();
110 110 initView();
111 111 initEditVideo();
... ...
core/base/src/main/java/com/cnlive/libs/base/util/StatusBarConfig.java 0 → 100644
  1 +package com.cnlive.libs.base.util;
  2 +
  3 +public class StatusBarConfig {
  4 + public static final int WHITE = 0;//白色
  5 + public static final int BLACK = 1;//黑色
  6 +}
... ...
core/base/src/main/java/com/cnlive/libs/base/util/StatusBarUtil.java
... ... @@ -2,17 +2,734 @@ package com.cnlive.libs.base.util;
2 2  
3 3 import android.annotation.TargetApi;
4 4 import android.app.Activity;
  5 +import android.content.Context;
  6 +import android.graphics.Color;
  7 +import android.os.Build;
  8 +import android.view.View;
  9 +import android.view.ViewGroup;
  10 +import android.view.Window;
  11 +import android.view.WindowManager;
  12 +import android.widget.LinearLayout;
5 13  
  14 +import androidx.annotation.ColorInt;
  15 +import androidx.annotation.IntRange;
  16 +import androidx.annotation.NonNull;
  17 +import androidx.coordinatorlayout.widget.CoordinatorLayout;
  18 +import androidx.drawerlayout.widget.DrawerLayout;
  19 +
  20 +import com.cnlive.strike.base.R;
6 21 import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
7 22  
  23 +import java.lang.reflect.Field;
  24 +import java.lang.reflect.Method;
  25 +
8 26 /**
9 27 * 修改状态栏颜色 和字体颜色的方法
10 28 */
11 29  
12 30 @SuppressWarnings("unused")
13 31 public class StatusBarUtil {
  32 + public static final int DEFAULT_STATUS_BAR_ALPHA = 112;
  33 + private static final int FAKE_STATUS_BAR_VIEW_ID = R.id.statusbarutil_fake_status_bar_view;
  34 + private static final int FAKE_TRANSLUCENT_VIEW_ID = R.id.statusbarutil_translucent_view;
  35 + private static final int TAG_KEY_HAVE_SET_OFFSET = -123;
  36 +
  37 + /**
  38 + * 设置状态栏颜色
  39 + *
  40 + * @param activity 需要设置的 activity
  41 + * @param color 状态栏颜色值
  42 + */
  43 + public static void setColor(Activity activity, @ColorInt int color) {
  44 + setColor(activity, color, DEFAULT_STATUS_BAR_ALPHA);
  45 + }
  46 +
  47 + /**
  48 + * 设置状态栏颜色
  49 + *
  50 + * @param activity 需要设置的activity
  51 + * @param color 状态栏颜色值
  52 + * @param statusBarAlpha 状态栏透明度
  53 + */
  54 +
  55 + public static void setColor(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) {
  56 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  57 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  58 + activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  59 + activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha));
  60 + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  61 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  62 + ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
  63 + View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
  64 + if (fakeStatusBarView != null) {
  65 + if (fakeStatusBarView.getVisibility() == View.GONE) {
  66 + fakeStatusBarView.setVisibility(View.VISIBLE);
  67 + }
  68 + fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
  69 + } else {
  70 + decorView.addView(createStatusBarView(activity, color, statusBarAlpha));
  71 + }
  72 + setRootView(activity);
  73 + }
  74 + }
  75 +
  76 + /**
  77 + * 为滑动返回界面设置状态栏颜色
  78 + *
  79 + * @param activity 需要设置的activity
  80 + * @param color 状态栏颜色值
  81 + */
  82 + public static void setColorForSwipeBack(Activity activity, int color) {
  83 + setColorForSwipeBack(activity, color, DEFAULT_STATUS_BAR_ALPHA);
  84 + }
  85 +
  86 + /**
  87 + * 为滑动返回界面设置状态栏颜色
  88 + *
  89 + * @param activity 需要设置的activity
  90 + * @param color 状态栏颜色值
  91 + * @param statusBarAlpha 状态栏透明度
  92 + */
  93 + public static void setColorForSwipeBack(Activity activity, @ColorInt int color,
  94 + @IntRange(from = 0, to = 255) int statusBarAlpha) {
  95 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  96 +
  97 + ViewGroup contentView = ((ViewGroup) activity.findViewById(android.R.id.content));
  98 + View rootView = contentView.getChildAt(0);
  99 + int statusBarHeight = getStatusBarHeight(activity);
  100 + if (rootView != null && rootView instanceof CoordinatorLayout) {
  101 + final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) rootView;
  102 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  103 + coordinatorLayout.setFitsSystemWindows(false);
  104 + contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
  105 + boolean isNeedRequestLayout = contentView.getPaddingTop() < statusBarHeight;
  106 + if (isNeedRequestLayout) {
  107 + contentView.setPadding(0, statusBarHeight, 0, 0);
  108 + coordinatorLayout.post(new Runnable() {
  109 + @Override
  110 + public void run() {
  111 + coordinatorLayout.requestLayout();
  112 + }
  113 + });
  114 + }
  115 + } else {
  116 + coordinatorLayout.setStatusBarBackgroundColor(calculateStatusColor(color, statusBarAlpha));
  117 + }
  118 + } else {
  119 + contentView.setPadding(0, statusBarHeight, 0, 0);
  120 + contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
  121 + }
  122 + setTransparentForWindow(activity);
  123 + }
  124 + }
  125 +
  126 + /**
  127 + * 设置状态栏纯色 不加半透明效果
  128 + *
  129 + * @param activity 需要设置的 activity
  130 + * @param color 状态栏颜色值
  131 + */
  132 + public static void setColorNoTranslucent(Activity activity, @ColorInt int color) {
  133 + setColor(activity, color, 0);
  134 + }
  135 +
  136 + /**
  137 + * 设置状态栏颜色(5.0以下无半透明效果,不建议使用)
  138 + *
  139 + * @param activity 需要设置的 activity
  140 + * @param color 状态栏颜色值
  141 + */
  142 + @Deprecated
  143 + public static void setColorDiff(Activity activity, @ColorInt int color) {
  144 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  145 + return;
  146 + }
  147 + transparentStatusBar(activity);
  148 + ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
  149 + // 移除半透明矩形,以免叠加
  150 + View fakeStatusBarView = contentView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
  151 + if (fakeStatusBarView != null) {
  152 + if (fakeStatusBarView.getVisibility() == View.GONE) {
  153 + fakeStatusBarView.setVisibility(View.VISIBLE);
  154 + }
  155 + fakeStatusBarView.setBackgroundColor(color);
  156 + } else {
  157 + contentView.addView(createStatusBarView(activity, color));
  158 + }
  159 + setRootView(activity);
  160 + }
  161 +
  162 + /**
  163 + * 使状态栏半透明
  164 + * <presenter>
  165 + * 适用于图片作为背景的界面,此时需要图片填充到状态栏
  166 + *
  167 + * @param activity 需要设置的activity
  168 + */
  169 + public static void setTranslucent(Activity activity) {
  170 + setTranslucent(activity, DEFAULT_STATUS_BAR_ALPHA);
  171 + }
  172 +
  173 + /**
  174 + * 使状态栏半透明
  175 + * <presenter>
  176 + * 适用于图片作为背景的界面,此时需要图片填充到状态栏
  177 + *
  178 + * @param activity 需要设置的activity
  179 + * @param statusBarAlpha 状态栏透明度
  180 + */
  181 + public static void setTranslucent(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
  182 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  183 + return;
  184 + }
  185 + setTransparent(activity);
  186 + addTranslucentView(activity, statusBarAlpha);
  187 + }
  188 +
  189 + /**
  190 + * 针对根布局是 CoordinatorLayout, 使状态栏半透明
  191 + * <presenter>
  192 + * 适用于图片作为背景的界面,此时需要图片填充到状态栏
  193 + *
  194 + * @param activity 需要设置的activity
  195 + * @param statusBarAlpha 状态栏透明度
  196 + */
  197 + public static void setTranslucentForCoordinatorLayout(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
  198 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  199 + return;
  200 + }
  201 + transparentStatusBar(activity);
  202 + addTranslucentView(activity, statusBarAlpha);
  203 + }
  204 +
  205 + /**
  206 + * 设置状态栏全透明
  207 + *
  208 + * @param activity 需要设置的activity
  209 + */
  210 + public static void setTransparent(Activity activity) {
  211 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  212 + return;
  213 + }
  214 + transparentStatusBar(activity);
  215 + setRootView(activity);
  216 + }
  217 +
  218 + /**
  219 + * 使状态栏透明(5.0以上半透明效果,不建议使用)
  220 + * <presenter>
  221 + * 适用于图片作为背景的界面,此时需要图片填充到状态栏
  222 + *
  223 + * @param activity 需要设置的activity
  224 + */
  225 + @Deprecated
  226 + public static void setTranslucentDiff(Activity activity) {
  227 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  228 + // 设置状态栏透明
  229 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  230 + setRootView(activity);
  231 + }
  232 + }
  233 +
  234 + /**
  235 + * 为DrawerLayout 布局设置状态栏变色
  236 + *
  237 + * @param activity 需要设置的activity
  238 + * @param drawerLayout DrawerLayout
  239 + * @param color 状态栏颜色值
  240 + */
  241 + public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
  242 + setColorForDrawerLayout(activity, drawerLayout, color, DEFAULT_STATUS_BAR_ALPHA);
  243 + }
  244 +
  245 + /**
  246 + * 为DrawerLayout 布局设置状态栏颜色,纯色
  247 + *
  248 + * @param activity 需要设置的activity
  249 + * @param drawerLayout DrawerLayout
  250 + * @param color 状态栏颜色值
  251 + */
  252 + public static void setColorNoTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
  253 + setColorForDrawerLayout(activity, drawerLayout, color, 0);
  254 + }
  255 +
  256 + /**
  257 + * 为DrawerLayout 布局设置状态栏变色
  258 + *
  259 + * @param activity 需要设置的activity
  260 + * @param drawerLayout DrawerLayout
  261 + * @param color 状态栏颜色值
  262 + * @param statusBarAlpha 状态栏透明度
  263 + */
  264 + public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color,
  265 + @IntRange(from = 0, to = 255) int statusBarAlpha) {
  266 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  267 + return;
  268 + }
  269 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  270 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  271 + activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  272 + activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
  273 + } else {
  274 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  275 + }
  276 + // 生成一个状态栏大小的矩形
  277 + // 添加 statusBarView 到布局中
  278 + ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
  279 + View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID);
  280 + if (fakeStatusBarView != null) {
  281 + if (fakeStatusBarView.getVisibility() == View.GONE) {
  282 + fakeStatusBarView.setVisibility(View.VISIBLE);
  283 + }
  284 + fakeStatusBarView.setBackgroundColor(color);
  285 + } else {
  286 + contentLayout.addView(createStatusBarView(activity, color), 0);
  287 + }
  288 + // 内容布局不是 LinearLayout 时,设置padding top
  289 + if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
  290 + contentLayout.getChildAt(1)
  291 + .setPadding(contentLayout.getPaddingLeft(), getStatusBarHeight(activity) + contentLayout.getPaddingTop(),
  292 + contentLayout.getPaddingRight(), contentLayout.getPaddingBottom());
  293 + }
  294 + // 设置属性
  295 + setDrawerLayoutProperty(drawerLayout, contentLayout);
  296 + addTranslucentView(activity, statusBarAlpha);
  297 + }
  298 +
  299 + /**
  300 + * 设置 DrawerLayout 属性
  301 + *
  302 + * @param drawerLayout DrawerLayout
  303 + * @param drawerLayoutContentLayout DrawerLayout 的内容布局
  304 + */
  305 + private static void setDrawerLayoutProperty(DrawerLayout drawerLayout, ViewGroup drawerLayoutContentLayout) {
  306 + ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);
  307 + drawerLayout.setFitsSystemWindows(false);
  308 + drawerLayoutContentLayout.setFitsSystemWindows(false);
  309 + drawerLayoutContentLayout.setClipToPadding(true);
  310 + drawer.setFitsSystemWindows(false);
  311 + }
  312 +
  313 + /**
  314 + * 为DrawerLayout 布局设置状态栏变色(5.0以下无半透明效果,不建议使用)
  315 + *
  316 + * @param activity 需要设置的activity
  317 + * @param drawerLayout DrawerLayout
  318 + * @param color 状态栏颜色值
  319 + */
  320 + @Deprecated
  321 + public static void setColorForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
  322 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  323 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  324 + // 生成一个状态栏大小的矩形
  325 + ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
  326 + View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID);
  327 + if (fakeStatusBarView != null) {
  328 + if (fakeStatusBarView.getVisibility() == View.GONE) {
  329 + fakeStatusBarView.setVisibility(View.VISIBLE);
  330 + }
  331 + fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, DEFAULT_STATUS_BAR_ALPHA));
  332 + } else {
  333 + // 添加 statusBarView 到布局中
  334 + contentLayout.addView(createStatusBarView(activity, color), 0);
  335 + }
  336 + // 内容布局不是 LinearLayout 时,设置padding top
  337 + if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
  338 + contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
  339 + }
  340 + // 设置属性
  341 + setDrawerLayoutProperty(drawerLayout, contentLayout);
  342 + }
  343 + }
  344 +
  345 + /**
  346 + * 为 DrawerLayout 布局设置状态栏透明
  347 + *
  348 + * @param activity 需要设置的activity
  349 + * @param drawerLayout DrawerLayout
  350 + */
  351 + public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
  352 + setTranslucentForDrawerLayout(activity, drawerLayout, DEFAULT_STATUS_BAR_ALPHA);
  353 + }
  354 +
  355 + /**
  356 + * 为 DrawerLayout 布局设置状态栏透明
  357 + *
  358 + * @param activity 需要设置的activity
  359 + * @param drawerLayout DrawerLayout
  360 + */
  361 + public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout,
  362 + @IntRange(from = 0, to = 255) int statusBarAlpha) {
  363 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  364 + return;
  365 + }
  366 + setTransparentForDrawerLayout(activity, drawerLayout);
  367 + addTranslucentView(activity, statusBarAlpha);
  368 + }
  369 +
  370 + /**
  371 + * 为 DrawerLayout 布局设置状态栏透明
  372 + *
  373 + * @param activity 需要设置的activity
  374 + * @param drawerLayout DrawerLayout
  375 + */
  376 + public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
  377 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  378 + return;
  379 + }
  380 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  381 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  382 + activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  383 + activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
  384 + } else {
  385 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  386 + }
  387 +
  388 + ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
  389 + // 内容布局不是 LinearLayout 时,设置padding top
  390 + if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
  391 + contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
  392 + }
  393 +
  394 + // 设置属性
  395 + setDrawerLayoutProperty(drawerLayout, contentLayout);
  396 + }
14 397  
15 398 /**
  399 + * 为 DrawerLayout 布局设置状态栏透明(5.0以上半透明效果,不建议使用)
  400 + *
  401 + * @param activity 需要设置的activity
  402 + * @param drawerLayout DrawerLayout
  403 + */
  404 + @Deprecated
  405 + public static void setTranslucentForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout) {
  406 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  407 + // 设置状态栏透明
  408 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  409 + // 设置内容布局属性
  410 + ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
  411 + contentLayout.setFitsSystemWindows(true);
  412 + contentLayout.setClipToPadding(true);
  413 + // 设置抽屉布局属性
  414 + ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1);
  415 + vg.setFitsSystemWindows(false);
  416 + // 设置 DrawerLayout 属性
  417 + drawerLayout.setFitsSystemWindows(false);
  418 + }
  419 + }
  420 +
  421 + /**
  422 + * 为头部是 ImageView 的界面设置状态栏全透明
  423 + *
  424 + * @param activity 需要设置的activity
  425 + * @param needOffsetView 需要向下偏移的 View
  426 + */
  427 + public static void setTransparentForImageView(Activity activity, View needOffsetView) {
  428 + setTranslucentForImageView(activity, 0, needOffsetView);
  429 + }
  430 +
  431 + /**
  432 + * 为头部是 ImageView 的界面设置状态栏透明(使用默认透明度)
  433 + *
  434 + * @param activity 需要设置的activity
  435 + * @param needOffsetView 需要向下偏移的 View
  436 + */
  437 + public static void setTranslucentForImageView(Activity activity, View needOffsetView) {
  438 + setTranslucentForImageView(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
  439 + }
  440 +
  441 + /**
  442 + * 为头部是 ImageView 的界面设置状态栏透明
  443 + *
  444 + * @param activity 需要设置的activity
  445 + * @param statusBarAlpha 状态栏透明度
  446 + * @param needOffsetView 需要向下偏移的 View
  447 + */
  448 + public static void setTranslucentForImageView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
  449 + View needOffsetView) {
  450 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  451 + return;
  452 + }
  453 + setTransparentForWindow(activity);
  454 + addTranslucentView(activity, statusBarAlpha);
  455 + if (needOffsetView != null) {
  456 + Object haveSetOffset = needOffsetView.getTag(TAG_KEY_HAVE_SET_OFFSET);
  457 + if (haveSetOffset != null && (Boolean) haveSetOffset) {
  458 + return;
  459 + }
  460 + ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams();
  461 + layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(activity),
  462 + layoutParams.rightMargin, layoutParams.bottomMargin);
  463 + needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true);
  464 + }
  465 + }
  466 +
  467 + /**
  468 + * 为 fragment 头部是 ImageView 的设置状态栏透明
  469 + *
  470 + * @param activity fragment 对应的 activity
  471 + * @param needOffsetView 需要向下偏移的 View
  472 + */
  473 + public static void setTranslucentForImageViewInFragment(Activity activity, View needOffsetView) {
  474 + setTranslucentForImageViewInFragment(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
  475 + }
  476 +
  477 + /**
  478 + * 为 fragment 头部是 ImageView 的设置状态栏透明
  479 + *
  480 + * @param activity fragment 对应的 activity
  481 + * @param needOffsetView 需要向下偏移的 View
  482 + */
  483 + public static void setTransparentForImageViewInFragment(Activity activity, View needOffsetView) {
  484 + setTranslucentForImageViewInFragment(activity, 0, needOffsetView);
  485 + }
  486 +
  487 + /**
  488 + * 为 fragment 头部是 ImageView 的设置状态栏透明
  489 + *
  490 + * @param activity fragment 对应的 activity
  491 + * @param statusBarAlpha 状态栏透明度
  492 + * @param needOffsetView 需要向下偏移的 View
  493 + */
  494 + public static void setTranslucentForImageViewInFragment(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
  495 + View needOffsetView) {
  496 + setTranslucentForImageView(activity, statusBarAlpha, needOffsetView);
  497 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  498 + clearPreviousSetting(activity);
  499 + }
  500 + }
  501 +
  502 + /**
  503 + * 隐藏伪状态栏 View
  504 + *
  505 + * @param activity 调用的 Activity
  506 + */
  507 + public static void hideFakeStatusBarView(Activity activity) {
  508 + ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
  509 + View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
  510 + if (fakeStatusBarView != null) {
  511 + fakeStatusBarView.setVisibility(View.GONE);
  512 + }
  513 + View fakeTranslucentView = decorView.findViewById(FAKE_TRANSLUCENT_VIEW_ID);
  514 + if (fakeTranslucentView != null) {
  515 + fakeTranslucentView.setVisibility(View.GONE);
  516 + }
  517 + }
  518 +
  519 + @TargetApi(Build.VERSION_CODES.M)
  520 + public static void setLightMode(Activity activity) {
  521 + setMIUIStatusBarDarkIcon(activity, true);
  522 + setMeizuStatusBarDarkIcon(activity, true);
  523 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  524 + activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
  525 + }
  526 + }
  527 +
  528 + @TargetApi(Build.VERSION_CODES.M)
  529 + public static void setDarkMode(Activity activity) {
  530 + setMIUIStatusBarDarkIcon(activity, false);
  531 + setMeizuStatusBarDarkIcon(activity, false);
  532 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  533 + activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
  534 + }
  535 + }
  536 +
  537 + /**
  538 + * 修改 MIUI V6 以上状态栏颜色
  539 + */
  540 + private static void setMIUIStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) {
  541 + Class<? extends Window> clazz = activity.getWindow().getClass();
  542 + try {
  543 + Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
  544 + Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
  545 + int darkModeFlag = field.getInt(layoutParams);
  546 + Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
  547 + extraFlagField.invoke(activity.getWindow(), darkIcon ? darkModeFlag : 0, darkModeFlag);
  548 + } catch (Exception e) {
  549 + //e.printStackTrace();
  550 + }
  551 + }
  552 +
  553 + /**
  554 + * 修改魅族状态栏字体颜色 Flyme 4.0
  555 + */
  556 + private static void setMeizuStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) {
  557 + try {
  558 + WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
  559 + Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
  560 + Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
  561 + darkFlag.setAccessible(true);
  562 + meizuFlags.setAccessible(true);
  563 + int bit = darkFlag.getInt(null);
  564 + int value = meizuFlags.getInt(lp);
  565 + if (darkIcon) {
  566 + value |= bit;
  567 + } else {
  568 + value &= ~bit;
  569 + }
  570 + meizuFlags.setInt(lp, value);
  571 + activity.getWindow().setAttributes(lp);
  572 + } catch (Exception e) {
  573 + //e.printStackTrace();
  574 + }
  575 + }
  576 +
  577 + ///////////////////////////////////////////////////////////////////////////////////
  578 +
  579 + @TargetApi(Build.VERSION_CODES.KITKAT)
  580 + private static void clearPreviousSetting(Activity activity) {
  581 + ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
  582 + View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
  583 + if (fakeStatusBarView != null) {
  584 + decorView.removeView(fakeStatusBarView);
  585 + ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
  586 + rootView.setPadding(0, 0, 0, 0);
  587 + }
  588 + }
  589 +
  590 + /**
  591 + * 添加半透明矩形条
  592 + *
  593 + * @param activity 需要设置的 activity
  594 + * @param statusBarAlpha 透明值
  595 + */
  596 + public static void addTranslucentView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
  597 + ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
  598 + View fakeTranslucentView = contentView.findViewById(FAKE_TRANSLUCENT_VIEW_ID);
  599 + if (fakeTranslucentView != null) {
  600 + if (fakeTranslucentView.getVisibility() == View.GONE) {
  601 + fakeTranslucentView.setVisibility(View.VISIBLE);
  602 + }
  603 + fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0));
  604 + } else {
  605 + contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha));
  606 + }
  607 + }
  608 +
  609 + /**
  610 + * 生成一个和状态栏大小相同的彩色矩形条
  611 + *
  612 + * @param activity 需要设置的 activity
  613 + * @param color 状态栏颜色值
  614 + * @return 状态栏矩形条
  615 + */
  616 + private static View createStatusBarView(Activity activity, @ColorInt int color) {
  617 + return createStatusBarView(activity, color, 0);
  618 + }
  619 +
  620 + /**
  621 + * 生成一个和状态栏大小相同的半透明矩形条
  622 + *
  623 + * @param activity 需要设置的activity
  624 + * @param color 状态栏颜色值
  625 + * @param alpha 透明值
  626 + * @return 状态栏矩形条
  627 + */
  628 + private static View createStatusBarView(Activity activity, @ColorInt int color, int alpha) {
  629 + // 绘制一个和状态栏一样高的矩形
  630 + View statusBarView = new View(activity);
  631 + LinearLayout.LayoutParams params =
  632 + new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
  633 + statusBarView.setLayoutParams(params);
  634 + statusBarView.setBackgroundColor(calculateStatusColor(color, alpha));
  635 + statusBarView.setId(FAKE_STATUS_BAR_VIEW_ID);
  636 + return statusBarView;
  637 + }
  638 +
  639 + /**
  640 + * 设置根布局参数
  641 + */
  642 + private static void setRootView(Activity activity) {
  643 + ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content);
  644 + for (int i = 0, count = parent.getChildCount(); i < count; i++) {
  645 + View childView = parent.getChildAt(i);
  646 + if (childView instanceof ViewGroup) {
  647 + childView.setFitsSystemWindows(true);
  648 + ((ViewGroup) childView).setClipToPadding(true);
  649 + }
  650 + }
  651 + }
  652 +
  653 + /**
  654 + * 设置透明
  655 + */
  656 + public static void setTransparentForWindow(Activity activity) {
  657 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  658 + activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
  659 + activity.getWindow()
  660 + .getDecorView()
  661 + .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
  662 + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  663 + activity.getWindow()
  664 + .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  665 + }
  666 + }
  667 +
  668 + /**
  669 + * 使状态栏透明
  670 + */
  671 + @TargetApi(Build.VERSION_CODES.KITKAT)
  672 + private static void transparentStatusBar(Activity activity) {
  673 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  674 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  675 + activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  676 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  677 + activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
  678 + } else {
  679 + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  680 + }
  681 + }
  682 +
  683 + /**
  684 + * 创建半透明矩形 View
  685 + *
  686 + * @param alpha 透明值
  687 + * @return 半透明 View
  688 + */
  689 + private static View createTranslucentStatusBarView(Activity activity, int alpha) {
  690 + // 绘制一个和状态栏一样高的矩形
  691 + View statusBarView = new View(activity);
  692 + LinearLayout.LayoutParams params =
  693 + new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
  694 + statusBarView.setLayoutParams(params);
  695 + statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0));
  696 + statusBarView.setId(FAKE_TRANSLUCENT_VIEW_ID);
  697 + return statusBarView;
  698 + }
  699 +
  700 + /**
  701 + * 获取状态栏高度
  702 + *
  703 + * @param context context
  704 + * @return 状态栏高度
  705 + */
  706 + public static int getStatusBarHeight(Context context) {
  707 + // 获得状态栏高度
  708 + int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
  709 + return context.getResources().getDimensionPixelSize(resourceId);
  710 + }
  711 +
  712 + /**
  713 + * 计算状态栏颜色
  714 + *
  715 + * @param color color值
  716 + * @param alpha alpha值
  717 + * @return 最终的状态栏颜色
  718 + */
  719 + private static int calculateStatusColor(@ColorInt int color, int alpha) {
  720 + if (alpha == 0) {
  721 + return color;
  722 + }
  723 + float a = 1 - alpha / 255f;
  724 + int red = color >> 16 & 0xff;
  725 + int green = color >> 8 & 0xff;
  726 + int blue = color & 0xff;
  727 + red = (int) (red * a + 0.5);
  728 + green = (int) (green * a + 0.5);
  729 + blue = (int) (blue * a + 0.5);
  730 + return 0xff << 24 | red << 16 | green << 8 | blue;
  731 + }
  732 + /**
16 733 * 设置状态栏黑色色字体图标
17 734 * 支持 4.4 以上版本 MIUI 和 Flyme,以及 6.0 以上版本的其他 Android
18 735 */
... ...
core/base/src/main/java/com/cnlive/libs/base/util/StatusBarUtil2.java deleted 100644 → 0
1   -package com.cnlive.libs.base.util;
2   -
3   -import android.annotation.TargetApi;
4   -import android.app.Activity;
5   -import android.content.Context;
6   -import android.graphics.Color;
7   -import android.os.Build;
8   -import android.view.View;
9   -import android.view.ViewGroup;
10   -import android.view.Window;
11   -import android.view.WindowManager;
12   -import android.widget.LinearLayout;
13   -
14   -
15   -import androidx.annotation.ColorInt;
16   -import androidx.annotation.IntRange;
17   -import androidx.annotation.NonNull;
18   -import androidx.coordinatorlayout.widget.CoordinatorLayout;
19   -import androidx.drawerlayout.widget.DrawerLayout;
20   -
21   -import com.cnlive.strike.base.R;
22   -
23   -import java.lang.reflect.Field;
24   -import java.lang.reflect.Method;
25   -
26   -/**
27   - * Created by Jaeger on 16/2/14.
28   - * <presenter>
29   - * Email: chjie.jaeger@gmail.com
30   - * GitHub: https://github.com/laobie
31   - */
32   -public class StatusBarUtil2 {
33   -
34   - public static final int DEFAULT_STATUS_BAR_ALPHA = 112;
35   - private static final int FAKE_STATUS_BAR_VIEW_ID = R.id.statusbarutil_fake_status_bar_view;
36   - private static final int FAKE_TRANSLUCENT_VIEW_ID = R.id.statusbarutil_translucent_view;
37   - private static final int TAG_KEY_HAVE_SET_OFFSET = -123;
38   -
39   - /**
40   - * 设置状态栏颜色
41   - *
42   - * @param activity 需要设置的 activity
43   - * @param color 状态栏颜色值
44   - */
45   - public static void setColor(Activity activity, @ColorInt int color) {
46   - setColor(activity, color, DEFAULT_STATUS_BAR_ALPHA);
47   - }
48   -
49   - /**
50   - * 设置状态栏颜色
51   - *
52   - * @param activity 需要设置的activity
53   - * @param color 状态栏颜色值
54   - * @param statusBarAlpha 状态栏透明度
55   - */
56   -
57   - public static void setColor(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) {
58   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
59   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
60   - activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
61   - activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha));
62   - } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
63   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
64   - ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
65   - View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
66   - if (fakeStatusBarView != null) {
67   - if (fakeStatusBarView.getVisibility() == View.GONE) {
68   - fakeStatusBarView.setVisibility(View.VISIBLE);
69   - }
70   - fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
71   - } else {
72   - decorView.addView(createStatusBarView(activity, color, statusBarAlpha));
73   - }
74   - setRootView(activity);
75   - }
76   - }
77   -
78   - /**
79   - * 为滑动返回界面设置状态栏颜色
80   - *
81   - * @param activity 需要设置的activity
82   - * @param color 状态栏颜色值
83   - */
84   - public static void setColorForSwipeBack(Activity activity, int color) {
85   - setColorForSwipeBack(activity, color, DEFAULT_STATUS_BAR_ALPHA);
86   - }
87   -
88   - /**
89   - * 为滑动返回界面设置状态栏颜色
90   - *
91   - * @param activity 需要设置的activity
92   - * @param color 状态栏颜色值
93   - * @param statusBarAlpha 状态栏透明度
94   - */
95   - public static void setColorForSwipeBack(Activity activity, @ColorInt int color,
96   - @IntRange(from = 0, to = 255) int statusBarAlpha) {
97   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
98   -
99   - ViewGroup contentView = ((ViewGroup) activity.findViewById(android.R.id.content));
100   - View rootView = contentView.getChildAt(0);
101   - int statusBarHeight = getStatusBarHeight(activity);
102   - if (rootView != null && rootView instanceof CoordinatorLayout) {
103   - final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) rootView;
104   - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
105   - coordinatorLayout.setFitsSystemWindows(false);
106   - contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
107   - boolean isNeedRequestLayout = contentView.getPaddingTop() < statusBarHeight;
108   - if (isNeedRequestLayout) {
109   - contentView.setPadding(0, statusBarHeight, 0, 0);
110   - coordinatorLayout.post(new Runnable() {
111   - @Override
112   - public void run() {
113   - coordinatorLayout.requestLayout();
114   - }
115   - });
116   - }
117   - } else {
118   - coordinatorLayout.setStatusBarBackgroundColor(calculateStatusColor(color, statusBarAlpha));
119   - }
120   - } else {
121   - contentView.setPadding(0, statusBarHeight, 0, 0);
122   - contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
123   - }
124   - setTransparentForWindow(activity);
125   - }
126   - }
127   -
128   - /**
129   - * 设置状态栏纯色 不加半透明效果
130   - *
131   - * @param activity 需要设置的 activity
132   - * @param color 状态栏颜色值
133   - */
134   - public static void setColorNoTranslucent(Activity activity, @ColorInt int color) {
135   - setColor(activity, color, 0);
136   - }
137   -
138   - /**
139   - * 设置状态栏颜色(5.0以下无半透明效果,不建议使用)
140   - *
141   - * @param activity 需要设置的 activity
142   - * @param color 状态栏颜色值
143   - */
144   - @Deprecated
145   - public static void setColorDiff(Activity activity, @ColorInt int color) {
146   - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
147   - return;
148   - }
149   - transparentStatusBar(activity);
150   - ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
151   - // 移除半透明矩形,以免叠加
152   - View fakeStatusBarView = contentView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
153   - if (fakeStatusBarView != null) {
154   - if (fakeStatusBarView.getVisibility() == View.GONE) {
155   - fakeStatusBarView.setVisibility(View.VISIBLE);
156   - }
157   - fakeStatusBarView.setBackgroundColor(color);
158   - } else {
159   - contentView.addView(createStatusBarView(activity, color));
160   - }
161   - setRootView(activity);
162   - }
163   -
164   - /**
165   - * 使状态栏半透明
166   - * <presenter>
167   - * 适用于图片作为背景的界面,此时需要图片填充到状态栏
168   - *
169   - * @param activity 需要设置的activity
170   - */
171   - public static void setTranslucent(Activity activity) {
172   - setTranslucent(activity, DEFAULT_STATUS_BAR_ALPHA);
173   - }
174   -
175   - /**
176   - * 使状态栏半透明
177   - * <presenter>
178   - * 适用于图片作为背景的界面,此时需要图片填充到状态栏
179   - *
180   - * @param activity 需要设置的activity
181   - * @param statusBarAlpha 状态栏透明度
182   - */
183   - public static void setTranslucent(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
184   - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
185   - return;
186   - }
187   - setTransparent(activity);
188   - addTranslucentView(activity, statusBarAlpha);
189   - }
190   -
191   - /**
192   - * 针对根布局是 CoordinatorLayout, 使状态栏半透明
193   - * <presenter>
194   - * 适用于图片作为背景的界面,此时需要图片填充到状态栏
195   - *
196   - * @param activity 需要设置的activity
197   - * @param statusBarAlpha 状态栏透明度
198   - */
199   - public static void setTranslucentForCoordinatorLayout(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
200   - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
201   - return;
202   - }
203   - transparentStatusBar(activity);
204   - addTranslucentView(activity, statusBarAlpha);
205   - }
206   -
207   - /**
208   - * 设置状态栏全透明
209   - *
210   - * @param activity 需要设置的activity
211   - */
212   - public static void setTransparent(Activity activity) {
213   - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
214   - return;
215   - }
216   - transparentStatusBar(activity);
217   - setRootView(activity);
218   - }
219   -
220   - /**
221   - * 使状态栏透明(5.0以上半透明效果,不建议使用)
222   - * <presenter>
223   - * 适用于图片作为背景的界面,此时需要图片填充到状态栏
224   - *
225   - * @param activity 需要设置的activity
226   - */
227   - @Deprecated
228   - public static void setTranslucentDiff(Activity activity) {
229   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
230   - // 设置状态栏透明
231   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
232   - setRootView(activity);
233   - }
234   - }
235   -
236   - /**
237   - * 为DrawerLayout 布局设置状态栏变色
238   - *
239   - * @param activity 需要设置的activity
240   - * @param drawerLayout DrawerLayout
241   - * @param color 状态栏颜色值
242   - */
243   - public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
244   - setColorForDrawerLayout(activity, drawerLayout, color, DEFAULT_STATUS_BAR_ALPHA);
245   - }
246   -
247   - /**
248   - * 为DrawerLayout 布局设置状态栏颜色,纯色
249   - *
250   - * @param activity 需要设置的activity
251   - * @param drawerLayout DrawerLayout
252   - * @param color 状态栏颜色值
253   - */
254   - public static void setColorNoTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
255   - setColorForDrawerLayout(activity, drawerLayout, color, 0);
256   - }
257   -
258   - /**
259   - * 为DrawerLayout 布局设置状态栏变色
260   - *
261   - * @param activity 需要设置的activity
262   - * @param drawerLayout DrawerLayout
263   - * @param color 状态栏颜色值
264   - * @param statusBarAlpha 状态栏透明度
265   - */
266   - public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color,
267   - @IntRange(from = 0, to = 255) int statusBarAlpha) {
268   - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
269   - return;
270   - }
271   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
272   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
273   - activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
274   - activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
275   - } else {
276   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
277   - }
278   - // 生成一个状态栏大小的矩形
279   - // 添加 statusBarView 到布局中
280   - ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
281   - View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID);
282   - if (fakeStatusBarView != null) {
283   - if (fakeStatusBarView.getVisibility() == View.GONE) {
284   - fakeStatusBarView.setVisibility(View.VISIBLE);
285   - }
286   - fakeStatusBarView.setBackgroundColor(color);
287   - } else {
288   - contentLayout.addView(createStatusBarView(activity, color), 0);
289   - }
290   - // 内容布局不是 LinearLayout 时,设置padding top
291   - if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
292   - contentLayout.getChildAt(1)
293   - .setPadding(contentLayout.getPaddingLeft(), getStatusBarHeight(activity) + contentLayout.getPaddingTop(),
294   - contentLayout.getPaddingRight(), contentLayout.getPaddingBottom());
295   - }
296   - // 设置属性
297   - setDrawerLayoutProperty(drawerLayout, contentLayout);
298   - addTranslucentView(activity, statusBarAlpha);
299   - }
300   -
301   - /**
302   - * 设置 DrawerLayout 属性
303   - *
304   - * @param drawerLayout DrawerLayout
305   - * @param drawerLayoutContentLayout DrawerLayout 的内容布局
306   - */
307   - private static void setDrawerLayoutProperty(DrawerLayout drawerLayout, ViewGroup drawerLayoutContentLayout) {
308   - ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);
309   - drawerLayout.setFitsSystemWindows(false);
310   - drawerLayoutContentLayout.setFitsSystemWindows(false);
311   - drawerLayoutContentLayout.setClipToPadding(true);
312   - drawer.setFitsSystemWindows(false);
313   - }
314   -
315   - /**
316   - * 为DrawerLayout 布局设置状态栏变色(5.0以下无半透明效果,不建议使用)
317   - *
318   - * @param activity 需要设置的activity
319   - * @param drawerLayout DrawerLayout
320   - * @param color 状态栏颜色值
321   - */
322   - @Deprecated
323   - public static void setColorForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
324   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
325   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
326   - // 生成一个状态栏大小的矩形
327   - ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
328   - View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID);
329   - if (fakeStatusBarView != null) {
330   - if (fakeStatusBarView.getVisibility() == View.GONE) {
331   - fakeStatusBarView.setVisibility(View.VISIBLE);
332   - }
333   - fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, DEFAULT_STATUS_BAR_ALPHA));
334   - } else {
335   - // 添加 statusBarView 到布局中
336   - contentLayout.addView(createStatusBarView(activity, color), 0);
337   - }
338   - // 内容布局不是 LinearLayout 时,设置padding top
339   - if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
340   - contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
341   - }
342   - // 设置属性
343   - setDrawerLayoutProperty(drawerLayout, contentLayout);
344   - }
345   - }
346   -
347   - /**
348   - * 为 DrawerLayout 布局设置状态栏透明
349   - *
350   - * @param activity 需要设置的activity
351   - * @param drawerLayout DrawerLayout
352   - */
353   - public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
354   - setTranslucentForDrawerLayout(activity, drawerLayout, DEFAULT_STATUS_BAR_ALPHA);
355   - }
356   -
357   - /**
358   - * 为 DrawerLayout 布局设置状态栏透明
359   - *
360   - * @param activity 需要设置的activity
361   - * @param drawerLayout DrawerLayout
362   - */
363   - public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout,
364   - @IntRange(from = 0, to = 255) int statusBarAlpha) {
365   - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
366   - return;
367   - }
368   - setTransparentForDrawerLayout(activity, drawerLayout);
369   - addTranslucentView(activity, statusBarAlpha);
370   - }
371   -
372   - /**
373   - * 为 DrawerLayout 布局设置状态栏透明
374   - *
375   - * @param activity 需要设置的activity
376   - * @param drawerLayout DrawerLayout
377   - */
378   - public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
379   - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
380   - return;
381   - }
382   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
383   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
384   - activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
385   - activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
386   - } else {
387   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
388   - }
389   -
390   - ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
391   - // 内容布局不是 LinearLayout 时,设置padding top
392   - if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
393   - contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
394   - }
395   -
396   - // 设置属性
397   - setDrawerLayoutProperty(drawerLayout, contentLayout);
398   - }
399   -
400   - /**
401   - * 为 DrawerLayout 布局设置状态栏透明(5.0以上半透明效果,不建议使用)
402   - *
403   - * @param activity 需要设置的activity
404   - * @param drawerLayout DrawerLayout
405   - */
406   - @Deprecated
407   - public static void setTranslucentForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout) {
408   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
409   - // 设置状态栏透明
410   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
411   - // 设置内容布局属性
412   - ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
413   - contentLayout.setFitsSystemWindows(true);
414   - contentLayout.setClipToPadding(true);
415   - // 设置抽屉布局属性
416   - ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1);
417   - vg.setFitsSystemWindows(false);
418   - // 设置 DrawerLayout 属性
419   - drawerLayout.setFitsSystemWindows(false);
420   - }
421   - }
422   -
423   - /**
424   - * 为头部是 ImageView 的界面设置状态栏全透明
425   - *
426   - * @param activity 需要设置的activity
427   - * @param needOffsetView 需要向下偏移的 View
428   - */
429   - public static void setTransparentForImageView(Activity activity, View needOffsetView) {
430   - setTranslucentForImageView(activity, 0, needOffsetView);
431   - }
432   -
433   - /**
434   - * 为头部是 ImageView 的界面设置状态栏透明(使用默认透明度)
435   - *
436   - * @param activity 需要设置的activity
437   - * @param needOffsetView 需要向下偏移的 View
438   - */
439   - public static void setTranslucentForImageView(Activity activity, View needOffsetView) {
440   - setTranslucentForImageView(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
441   - }
442   -
443   - /**
444   - * 为头部是 ImageView 的界面设置状态栏透明
445   - *
446   - * @param activity 需要设置的activity
447   - * @param statusBarAlpha 状态栏透明度
448   - * @param needOffsetView 需要向下偏移的 View
449   - */
450   - public static void setTranslucentForImageView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
451   - View needOffsetView) {
452   - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
453   - return;
454   - }
455   - setTransparentForWindow(activity);
456   - addTranslucentView(activity, statusBarAlpha);
457   - if (needOffsetView != null) {
458   - Object haveSetOffset = needOffsetView.getTag(TAG_KEY_HAVE_SET_OFFSET);
459   - if (haveSetOffset != null && (Boolean) haveSetOffset) {
460   - return;
461   - }
462   - ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams();
463   - layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(activity),
464   - layoutParams.rightMargin, layoutParams.bottomMargin);
465   - needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true);
466   - }
467   - }
468   -
469   - /**
470   - * 为 fragment 头部是 ImageView 的设置状态栏透明
471   - *
472   - * @param activity fragment 对应的 activity
473   - * @param needOffsetView 需要向下偏移的 View
474   - */
475   - public static void setTranslucentForImageViewInFragment(Activity activity, View needOffsetView) {
476   - setTranslucentForImageViewInFragment(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
477   - }
478   -
479   - /**
480   - * 为 fragment 头部是 ImageView 的设置状态栏透明
481   - *
482   - * @param activity fragment 对应的 activity
483   - * @param needOffsetView 需要向下偏移的 View
484   - */
485   - public static void setTransparentForImageViewInFragment(Activity activity, View needOffsetView) {
486   - setTranslucentForImageViewInFragment(activity, 0, needOffsetView);
487   - }
488   -
489   - /**
490   - * 为 fragment 头部是 ImageView 的设置状态栏透明
491   - *
492   - * @param activity fragment 对应的 activity
493   - * @param statusBarAlpha 状态栏透明度
494   - * @param needOffsetView 需要向下偏移的 View
495   - */
496   - public static void setTranslucentForImageViewInFragment(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
497   - View needOffsetView) {
498   - setTranslucentForImageView(activity, statusBarAlpha, needOffsetView);
499   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
500   - clearPreviousSetting(activity);
501   - }
502   - }
503   -
504   - /**
505   - * 隐藏伪状态栏 View
506   - *
507   - * @param activity 调用的 Activity
508   - */
509   - public static void hideFakeStatusBarView(Activity activity) {
510   - ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
511   - View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
512   - if (fakeStatusBarView != null) {
513   - fakeStatusBarView.setVisibility(View.GONE);
514   - }
515   - View fakeTranslucentView = decorView.findViewById(FAKE_TRANSLUCENT_VIEW_ID);
516   - if (fakeTranslucentView != null) {
517   - fakeTranslucentView.setVisibility(View.GONE);
518   - }
519   - }
520   -
521   - @TargetApi(Build.VERSION_CODES.M)
522   - public static void setLightMode(Activity activity) {
523   - setMIUIStatusBarDarkIcon(activity, true);
524   - setMeizuStatusBarDarkIcon(activity, true);
525   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
526   - activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
527   - }
528   - }
529   -
530   - @TargetApi(Build.VERSION_CODES.M)
531   - public static void setDarkMode(Activity activity) {
532   - setMIUIStatusBarDarkIcon(activity, false);
533   - setMeizuStatusBarDarkIcon(activity, false);
534   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
535   - activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
536   - }
537   - }
538   -
539   - /**
540   - * 修改 MIUI V6 以上状态栏颜色
541   - */
542   - private static void setMIUIStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) {
543   - Class<? extends Window> clazz = activity.getWindow().getClass();
544   - try {
545   - Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
546   - Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
547   - int darkModeFlag = field.getInt(layoutParams);
548   - Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
549   - extraFlagField.invoke(activity.getWindow(), darkIcon ? darkModeFlag : 0, darkModeFlag);
550   - } catch (Exception e) {
551   - //e.printStackTrace();
552   - }
553   - }
554   -
555   - /**
556   - * 修改魅族状态栏字体颜色 Flyme 4.0
557   - */
558   - private static void setMeizuStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) {
559   - try {
560   - WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
561   - Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
562   - Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
563   - darkFlag.setAccessible(true);
564   - meizuFlags.setAccessible(true);
565   - int bit = darkFlag.getInt(null);
566   - int value = meizuFlags.getInt(lp);
567   - if (darkIcon) {
568   - value |= bit;
569   - } else {
570   - value &= ~bit;
571   - }
572   - meizuFlags.setInt(lp, value);
573   - activity.getWindow().setAttributes(lp);
574   - } catch (Exception e) {
575   - //e.printStackTrace();
576   - }
577   - }
578   -
579   - ///////////////////////////////////////////////////////////////////////////////////
580   -
581   - @TargetApi(Build.VERSION_CODES.KITKAT)
582   - private static void clearPreviousSetting(Activity activity) {
583   - ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
584   - View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
585   - if (fakeStatusBarView != null) {
586   - decorView.removeView(fakeStatusBarView);
587   - ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
588   - rootView.setPadding(0, 0, 0, 0);
589   - }
590   - }
591   -
592   - /**
593   - * 添加半透明矩形条
594   - *
595   - * @param activity 需要设置的 activity
596   - * @param statusBarAlpha 透明值
597   - */
598   - public static void addTranslucentView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
599   - ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
600   - View fakeTranslucentView = contentView.findViewById(FAKE_TRANSLUCENT_VIEW_ID);
601   - if (fakeTranslucentView != null) {
602   - if (fakeTranslucentView.getVisibility() == View.GONE) {
603   - fakeTranslucentView.setVisibility(View.VISIBLE);
604   - }
605   - fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0));
606   - } else {
607   - contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha));
608   - }
609   - }
610   -
611   - /**
612   - * 生成一个和状态栏大小相同的彩色矩形条
613   - *
614   - * @param activity 需要设置的 activity
615   - * @param color 状态栏颜色值
616   - * @return 状态栏矩形条
617   - */
618   - private static View createStatusBarView(Activity activity, @ColorInt int color) {
619   - return createStatusBarView(activity, color, 0);
620   - }
621   -
622   - /**
623   - * 生成一个和状态栏大小相同的半透明矩形条
624   - *
625   - * @param activity 需要设置的activity
626   - * @param color 状态栏颜色值
627   - * @param alpha 透明值
628   - * @return 状态栏矩形条
629   - */
630   - private static View createStatusBarView(Activity activity, @ColorInt int color, int alpha) {
631   - // 绘制一个和状态栏一样高的矩形
632   - View statusBarView = new View(activity);
633   - LinearLayout.LayoutParams params =
634   - new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
635   - statusBarView.setLayoutParams(params);
636   - statusBarView.setBackgroundColor(calculateStatusColor(color, alpha));
637   - statusBarView.setId(FAKE_STATUS_BAR_VIEW_ID);
638   - return statusBarView;
639   - }
640   -
641   - /**
642   - * 设置根布局参数
643   - */
644   - private static void setRootView(Activity activity) {
645   - ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content);
646   - for (int i = 0, count = parent.getChildCount(); i < count; i++) {
647   - View childView = parent.getChildAt(i);
648   - if (childView instanceof ViewGroup) {
649   - childView.setFitsSystemWindows(true);
650   - ((ViewGroup) childView).setClipToPadding(true);
651   - }
652   - }
653   - }
654   -
655   - /**
656   - * 设置透明
657   - */
658   - public static void setTransparentForWindow(Activity activity) {
659   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
660   - activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
661   - activity.getWindow()
662   - .getDecorView()
663   - .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
664   - } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
665   - activity.getWindow()
666   - .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
667   - }
668   - }
669   -
670   - /**
671   - * 使状态栏透明
672   - */
673   - @TargetApi(Build.VERSION_CODES.KITKAT)
674   - private static void transparentStatusBar(Activity activity) {
675   - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
676   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
677   - activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
678   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
679   - activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
680   - } else {
681   - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
682   - }
683   - }
684   -
685   - /**
686   - * 创建半透明矩形 View
687   - *
688   - * @param alpha 透明值
689   - * @return 半透明 View
690   - */
691   - private static View createTranslucentStatusBarView(Activity activity, int alpha) {
692   - // 绘制一个和状态栏一样高的矩形
693   - View statusBarView = new View(activity);
694   - LinearLayout.LayoutParams params =
695   - new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
696   - statusBarView.setLayoutParams(params);
697   - statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0));
698   - statusBarView.setId(FAKE_TRANSLUCENT_VIEW_ID);
699   - return statusBarView;
700   - }
701   -
702   - /**
703   - * 获取状态栏高度
704   - *
705   - * @param context context
706   - * @return 状态栏高度
707   - */
708   - public static int getStatusBarHeight(Context context) {
709   - // 获得状态栏高度
710   - int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
711   - return context.getResources().getDimensionPixelSize(resourceId);
712   - }
713   -
714   - /**
715   - * 计算状态栏颜色
716   - *
717   - * @param color color值
718   - * @param alpha alpha值
719   - * @return 最终的状态栏颜色
720   - */
721   - private static int calculateStatusColor(@ColorInt int color, int alpha) {
722   - if (alpha == 0) {
723   - return color;
724   - }
725   - float a = 1 - alpha / 255f;
726   - int red = color >> 16 & 0xff;
727   - int green = color >> 8 & 0xff;
728   - int blue = color & 0xff;
729   - red = (int) (red * a + 0.5);
730   - green = (int) (green * a + 0.5);
731   - blue = (int) (blue * a + 0.5);
732   - return 0xff << 24 | red << 16 | green << 8 | blue;
733   - }
734   -}