Commit 3050303d520e4eee5ab94936a9e5cb4a35729e4d
基类修改
Showing
13 changed files
with
1055 additions
and
782 deletions
app/strike/src/main/java/com/cnlive/strike/MyActivity.java
| 1 | package com.cnlive.strike; | 1 | package com.cnlive.strike; |
| 2 | 2 | ||
| 3 | -import androidx.appcompat.app.AppCompatActivity; | ||
| 4 | 3 | ||
| 5 | -import android.os.Bundle; | 4 | +import com.cnlive.libs.base.frame.activity.BaseActivity; |
| 6 | 5 | ||
| 7 | 6 | ||
| 8 | -public class MyActivity extends AppCompatActivity { | 7 | +public class MyActivity extends BaseActivity { |
| 9 | 8 | ||
| 10 | @Override | 9 | @Override |
| 11 | - protected void onCreate(Bundle savedInstanceState) { | ||
| 12 | - super.onCreate(savedInstanceState); | ||
| 13 | - setContentView(R.layout.activity_my); | ||
| 14 | - getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new MyFragment()).commitAllowingStateLoss(); | 10 | + protected int initLayout() { |
| 11 | + return R.layout.activity_my; | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + @Override | ||
| 15 | + protected void initView() { | ||
| 16 | + getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MyFragment()).commitAllowingStateLoss(); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + @Override | ||
| 20 | + protected void initData() { | ||
| 15 | } | 21 | } |
| 16 | } | 22 | } |
app/strike/src/main/java/com/cnlive/strike/MyFragment.java
| @@ -2,28 +2,35 @@ package com.cnlive.strike; | @@ -2,28 +2,35 @@ package com.cnlive.strike; | ||
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | import android.os.Bundle; | 4 | import android.os.Bundle; |
| 5 | +import android.view.LayoutInflater; | ||
| 5 | import android.view.View; | 6 | import android.view.View; |
| 6 | -import android.widget.TextView; | 7 | +import android.view.ViewGroup; |
| 7 | 8 | ||
| 8 | import com.cnlive.libs.base.frame.fragment.BaseFragment; | 9 | import com.cnlive.libs.base.frame.fragment.BaseFragment; |
| 10 | +import com.cnlive.libs.base.util.StatusBarConfig; | ||
| 9 | 11 | ||
| 12 | +import androidx.annotation.NonNull; | ||
| 10 | import androidx.annotation.Nullable; | 13 | import androidx.annotation.Nullable; |
| 11 | 14 | ||
| 12 | 15 | ||
| 13 | -public class MyFragment extends BaseFragment <MyFragmentView,MyPresenter>{ | ||
| 14 | - | 16 | +public class MyFragment extends BaseFragment<MyFragmentView, MyPresenter> { |
| 17 | + @Nullable | ||
| 18 | + @Override | ||
| 19 | + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||
| 20 | + return inflater.inflate(R.layout.activity_main1, container, false); | ||
| 21 | + } | ||
| 15 | 22 | ||
| 16 | - public TextView tx; | ||
| 17 | @Override | 23 | @Override |
| 18 | - protected int getLayoutId() { | ||
| 19 | - return R.layout.activity_main1; | 24 | + protected void initView() { |
| 25 | + setStatusBarColor(R.color.green, 0); | ||
| 26 | + setStatusBarTextColor(StatusBarConfig.BLACK); | ||
| 20 | } | 27 | } |
| 21 | 28 | ||
| 22 | @Override | 29 | @Override |
| 23 | - public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | ||
| 24 | - super.onViewCreated(view, savedInstanceState); | ||
| 25 | - tx = view.findViewById(R.id.tv1); | ||
| 26 | - getPresenter().setData("hha"); | ||
| 27 | -// getMvpView().setData("sasa"); | 30 | + protected void initData() { |
| 31 | +// getPresenter().setData("hha"); | ||
| 32 | + getMvpView().setData("test"); | ||
| 28 | } | 33 | } |
| 34 | + | ||
| 35 | + | ||
| 29 | } | 36 | } |
app/strike/src/main/java/com/cnlive/strike/MyFragmentView.java
| @@ -10,6 +10,6 @@ public class MyFragmentView extends BaseView { | @@ -10,6 +10,6 @@ public class MyFragmentView extends BaseView { | ||
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | public void setData(String msg){ | 12 | public void setData(String msg){ |
| 13 | - fragment.tx.setText(msg); | 13 | +// fragment.tx.setText(msg); |
| 14 | } | 14 | } |
| 15 | } | 15 | } |
app/strike/src/main/java/com/cnlive/strike/MyPresenter.java
| 1 | package com.cnlive.strike; | 1 | package com.cnlive.strike; |
| 2 | 2 | ||
| 3 | +import com.cnlive.libs.base.frame.presenter.BasePresenter; | ||
| 3 | import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter; | 4 | import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter; |
| 4 | 5 | ||
| 5 | -public class MyPresenter extends MvpBasePresenter<MyFragmentView> { | 6 | +public class MyPresenter extends BasePresenter<MyFragmentView> { |
| 6 | public void setData(String msg) { | 7 | public void setData(String msg) { |
| 7 | - getView().setData(msg); | 8 | +// getView().setData(msg); |
| 8 | } | 9 | } |
| 9 | } | 10 | } |
core/base/src/main/java/com/cnlive/libs/base/frame/activity/BaseActivity.java
0 → 100644
| 1 | +package com.cnlive.libs.base.frame.activity; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import android.annotation.SuppressLint; | ||
| 5 | +import android.content.Context; | ||
| 6 | +import android.content.pm.ActivityInfo; | ||
| 7 | +import android.database.DatabaseUtils; | ||
| 8 | +import android.os.Bundle; | ||
| 9 | +import android.os.Looper; | ||
| 10 | +import android.view.View; | ||
| 11 | +import android.view.Window; | ||
| 12 | +import android.view.WindowManager; | ||
| 13 | +import android.view.inputmethod.InputMethodManager; | ||
| 14 | +import android.widget.Toast; | ||
| 15 | + | ||
| 16 | +import androidx.annotation.ColorRes; | ||
| 17 | +import androidx.annotation.IntRange; | ||
| 18 | +import androidx.annotation.Nullable; | ||
| 19 | +import androidx.appcompat.app.AppCompatActivity; | ||
| 20 | +import androidx.core.content.ContextCompat; | ||
| 21 | +import androidx.databinding.DataBindingUtil; | ||
| 22 | +import androidx.databinding.ViewDataBinding; | ||
| 23 | + | ||
| 24 | +import com.cnlive.libs.base.util.StatusBarConfig; | ||
| 25 | +import com.cnlive.libs.base.util.StatusBarUtil; | ||
| 26 | +import com.cnlive.libs.base.util.lib_permisshelper.PermissionsUtils; | ||
| 27 | +import com.cnlive.libs.base.util.ActivityCollector; | ||
| 28 | + | ||
| 29 | +/** | ||
| 30 | + * @author ys | ||
| 31 | + * 基类 activity | ||
| 32 | + */ | ||
| 33 | +public abstract class BaseActivity extends AppCompatActivity { | ||
| 34 | + //获取TAG的activity名称 | ||
| 35 | + protected final String TAG = this.getClass().getSimpleName(); | ||
| 36 | + //是否显示标题栏 | ||
| 37 | + private boolean isShowTitle = true; | ||
| 38 | + //是否显示状态栏 | ||
| 39 | + private boolean isShowStatusBar = true; | ||
| 40 | + //是否允许旋转屏幕 | ||
| 41 | + private boolean isAllowScreenRoate = true; | ||
| 42 | + //封装Toast对象 | ||
| 43 | + private static Toast toast; | ||
| 44 | + public Context context; | ||
| 45 | + private PermissionsUtils mPermissionsUtils;//权限申请 | ||
| 46 | + private int stateBarColor = -1; | ||
| 47 | + public ViewDataBinding binding; | ||
| 48 | + | ||
| 49 | + @Override | ||
| 50 | + protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
| 51 | + super.onCreate(savedInstanceState); | ||
| 52 | + context = this; | ||
| 53 | + //activity管理 | ||
| 54 | + ActivityCollector.addActivity(this); | ||
| 55 | + if (!isShowTitle) { | ||
| 56 | + requestWindowFeature(Window.FEATURE_NO_TITLE); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + if (!isShowStatusBar) { | ||
| 60 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | ||
| 61 | + , WindowManager.LayoutParams.FLAG_FULLSCREEN); | ||
| 62 | + } | ||
| 63 | + //设置布局 | ||
| 64 | + binding = DataBindingUtil.setContentView(this, initLayout()); | ||
| 65 | + //设置屏幕是否可旋转 | ||
| 66 | + if (!isAllowScreenRoate) { | ||
| 67 | + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); | ||
| 68 | + } else { | ||
| 69 | + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | ||
| 70 | + } | ||
| 71 | + mPermissionsUtils = new PermissionsUtils();//权限申请 | ||
| 72 | + //初始化控件 | ||
| 73 | + initView(); | ||
| 74 | + //设置数据 | ||
| 75 | + initData(); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + /** | ||
| 79 | + * 初始化布局 | ||
| 80 | + * | ||
| 81 | + * @return 布局id | ||
| 82 | + */ | ||
| 83 | + protected abstract int initLayout(); | ||
| 84 | + | ||
| 85 | + /** | ||
| 86 | + * 初始化控件 | ||
| 87 | + */ | ||
| 88 | + protected abstract void initView(); | ||
| 89 | + | ||
| 90 | + /** | ||
| 91 | + * 设置数据 | ||
| 92 | + */ | ||
| 93 | + protected abstract void initData(); | ||
| 94 | + | ||
| 95 | + /** | ||
| 96 | + * 设置是否显示标题栏 | ||
| 97 | + * | ||
| 98 | + * @param showTitle true or false | ||
| 99 | + */ | ||
| 100 | + public void setShowTitle(boolean showTitle) { | ||
| 101 | + isShowTitle = showTitle; | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + /** | ||
| 105 | + * 设置是否显示状态栏 | ||
| 106 | + * | ||
| 107 | + * @param showStatusBar true or false | ||
| 108 | + */ | ||
| 109 | + public void setShowStatusBar(boolean showStatusBar) { | ||
| 110 | + isShowStatusBar = showStatusBar; | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + /** | ||
| 114 | + * 是否允许屏幕旋转 | ||
| 115 | + * | ||
| 116 | + * @param allowScreenRoate true or false | ||
| 117 | + */ | ||
| 118 | + public void setAllowScreenRoate(boolean allowScreenRoate) { | ||
| 119 | + isAllowScreenRoate = allowScreenRoate; | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + /** | ||
| 123 | + * 保证同一按钮在1秒内只会响应一次点击事件 | ||
| 124 | + */ | ||
| 125 | + public abstract class OnSingleClickListener implements View.OnClickListener { | ||
| 126 | + //两次点击按钮之间的间隔,目前为1000ms | ||
| 127 | + private static final int MIN_CLICK_DELAY_TIME = 1000; | ||
| 128 | + private long lastClickTime; | ||
| 129 | + | ||
| 130 | + public abstract void onSingleClick(View view); | ||
| 131 | + | ||
| 132 | + @Override | ||
| 133 | + public void onClick(View view) { | ||
| 134 | + long curClickTime = System.currentTimeMillis(); | ||
| 135 | + if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) { | ||
| 136 | + lastClickTime = curClickTime; | ||
| 137 | + onSingleClick(view); | ||
| 138 | + } | ||
| 139 | + } | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + /** | ||
| 143 | + * 同一按钮在短时间内可重复响应点击事件 | ||
| 144 | + */ | ||
| 145 | + public abstract class OnMultiClickListener implements View.OnClickListener { | ||
| 146 | + public abstract void onMultiClick(View view); | ||
| 147 | + | ||
| 148 | + @Override | ||
| 149 | + public void onClick(View v) { | ||
| 150 | + onMultiClick(v); | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + /** | ||
| 155 | + * 显示提示 toast | ||
| 156 | + * | ||
| 157 | + * @param msg 提示信息 | ||
| 158 | + */ | ||
| 159 | + @SuppressLint("ShowToast") | ||
| 160 | + public void showToast(String msg) { | ||
| 161 | + try { | ||
| 162 | + if (null == toast) { | ||
| 163 | + toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT); | ||
| 164 | + } else { | ||
| 165 | + toast.setText(msg); | ||
| 166 | + } | ||
| 167 | + runOnUiThread(new Runnable() { | ||
| 168 | + @Override | ||
| 169 | + public void run() { | ||
| 170 | + toast.show(); | ||
| 171 | + } | ||
| 172 | + }); | ||
| 173 | + } catch (Exception e) { | ||
| 174 | + e.printStackTrace(); | ||
| 175 | + //解决在子线程中调用Toast的异常情况处理 | ||
| 176 | + Looper.prepare(); | ||
| 177 | + Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); | ||
| 178 | + Looper.loop(); | ||
| 179 | + } | ||
| 180 | + } | ||
| 181 | + | ||
| 182 | + | ||
| 183 | + /** | ||
| 184 | + * 隐藏软键盘 | ||
| 185 | + */ | ||
| 186 | + public void hideSoftInput() { | ||
| 187 | + InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); | ||
| 188 | + if (getCurrentFocus() != null && null != imm) { | ||
| 189 | + imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); | ||
| 190 | + } | ||
| 191 | + } | ||
| 192 | + | ||
| 193 | + /** | ||
| 194 | + * 显示软键盘 | ||
| 195 | + */ | ||
| 196 | + public void showSoftInput() { | ||
| 197 | + InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); | ||
| 198 | + if (getCurrentFocus() != null && null != imm) { | ||
| 199 | + imm.showSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0); | ||
| 200 | + } | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + /** | ||
| 204 | + * 显示软键盘 | ||
| 205 | + */ | ||
| 206 | + public void showSoftInput(View view) { | ||
| 207 | + // InputMethodManager.SHOW_FORCED); | ||
| 208 | + InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); | ||
| 209 | + imm.showSoftInput(view, InputMethodManager.SHOW_FORCED); | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + @Override | ||
| 213 | + protected void onDestroy() { | ||
| 214 | + super.onDestroy(); | ||
| 215 | + //activity管理 | ||
| 216 | + ActivityCollector.removeActivity(this); | ||
| 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 | + } | ||
| 239 | +} | ||
| 0 | \ No newline at end of file | 240 | \ No newline at end of file |
core/base/src/main/java/com/cnlive/libs/base/frame/fragment/BaseFragment.java
| @@ -6,17 +6,21 @@ import android.view.LayoutInflater; | @@ -6,17 +6,21 @@ import android.view.LayoutInflater; | ||
| 6 | import android.view.View; | 6 | import android.view.View; |
| 7 | import android.view.ViewGroup; | 7 | import android.view.ViewGroup; |
| 8 | 8 | ||
| 9 | +import androidx.annotation.ColorRes; | ||
| 10 | +import androidx.annotation.IntRange; | ||
| 9 | import androidx.annotation.NonNull; | 11 | import androidx.annotation.NonNull; |
| 10 | import androidx.annotation.Nullable; | 12 | import androidx.annotation.Nullable; |
| 13 | +import androidx.core.content.ContextCompat; | ||
| 11 | 14 | ||
| 12 | import com.cnlive.libs.base.frame.presenter.BasePresenter; | 15 | import com.cnlive.libs.base.frame.presenter.BasePresenter; |
| 13 | import com.cnlive.libs.base.frame.view.BaseView; | 16 | import com.cnlive.libs.base.frame.view.BaseView; |
| 14 | import com.cnlive.libs.base.util.ReflectUtil; | 17 | import com.cnlive.libs.base.util.ReflectUtil; |
| 18 | +import com.cnlive.libs.base.util.StatusBarConfig; | ||
| 19 | +import com.cnlive.libs.base.util.StatusBarUtil; | ||
| 15 | import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter; | 20 | import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter; |
| 16 | import com.hannesdorfmann.mosby3.mvp.MvpPresenter; | 21 | import com.hannesdorfmann.mosby3.mvp.MvpPresenter; |
| 17 | import com.hannesdorfmann.mosby3.mvp.MvpView; | 22 | import com.hannesdorfmann.mosby3.mvp.MvpView; |
| 18 | 23 | ||
| 19 | - | ||
| 20 | /** | 24 | /** |
| 21 | * MVP Databinding Base Fragment | 25 | * MVP Databinding Base Fragment |
| 22 | * | 26 | * |
| @@ -25,42 +29,53 @@ import com.hannesdorfmann.mosby3.mvp.MvpView; | @@ -25,42 +29,53 @@ import com.hannesdorfmann.mosby3.mvp.MvpView; | ||
| 25 | */ | 29 | */ |
| 26 | public abstract class BaseFragment<V extends MvpView, P extends MvpBasePresenter<V>> extends com.hannesdorfmann.mosby3.mvp.MvpFragment<V, P> { | 30 | public abstract class BaseFragment<V extends MvpView, P extends MvpBasePresenter<V>> extends com.hannesdorfmann.mosby3.mvp.MvpFragment<V, P> { |
| 27 | 31 | ||
| 28 | - public View view; | ||
| 29 | - public V views; | ||
| 30 | - | ||
| 31 | - @Nullable | ||
| 32 | - @Override | ||
| 33 | - public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||
| 34 | - return view = inflater.inflate(getLayoutId(), container, false); | ||
| 35 | - } | ||
| 36 | 32 | ||
| 37 | @Override | 33 | @Override |
| 38 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | 34 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { |
| 39 | super.onViewCreated(view, savedInstanceState); | 35 | super.onViewCreated(view, savedInstanceState); |
| 40 | - getPresenter(); | 36 | + if (null != getMvpView() && null != getPresenter()) { |
| 37 | + initView(); | ||
| 38 | + initData(); | ||
| 39 | + } | ||
| 41 | } | 40 | } |
| 42 | 41 | ||
| 43 | - @NonNull | ||
| 44 | - public V createView() { | ||
| 45 | - Log.e("sssss", "createView: "); | ||
| 46 | - return ReflectUtil.instanceView(0, this.getClass(), this, BaseView.class); | ||
| 47 | - } | ||
| 48 | 42 | ||
| 49 | @Override | 43 | @Override |
| 50 | public P createPresenter() { | 44 | public P createPresenter() { |
| 51 | - Log.e("sssss", "createPresenter: "); | ||
| 52 | return ReflectUtil.instancePresenter(1, this.getClass(), BasePresenter.class); | 45 | return ReflectUtil.instancePresenter(1, this.getClass(), BasePresenter.class); |
| 53 | } | 46 | } |
| 54 | 47 | ||
| 55 | @NonNull | 48 | @NonNull |
| 56 | @Override | 49 | @Override |
| 57 | public V getMvpView() { | 50 | public V getMvpView() { |
| 58 | - if (null == views) { | ||
| 59 | - return createView(); | ||
| 60 | - } else { | ||
| 61 | - return views; | 51 | + return ReflectUtil.instanceView(0, this.getClass(), this, BaseView.class); |
| 52 | + } | ||
| 53 | + | ||
| 54 | + | ||
| 55 | + protected abstract void initView(); | ||
| 56 | + | ||
| 57 | + protected abstract void initData(); | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * 设置状态栏颜色 | ||
| 61 | + * | ||
| 62 | + * @param color | ||
| 63 | + * @param statusBarAlpha | ||
| 64 | + */ | ||
| 65 | + protected void setStatusBarColor(@ColorRes int color, @IntRange(from = 0, to = 255) int statusBarAlpha) { | ||
| 66 | + StatusBarUtil.setColor(getActivity(), ContextCompat.getColor(getActivity(), color), statusBarAlpha); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + /** | ||
| 70 | + * 设置状态栏字体颜色 | ||
| 71 | + */ | ||
| 72 | + protected void setStatusBarTextColor(int type) { | ||
| 73 | + if (type == StatusBarConfig.BLACK) { | ||
| 74 | + StatusBarUtil.setStatusBarWrite(getActivity()); | ||
| 75 | + } else if (type == StatusBarConfig.WHITE) { | ||
| 76 | + StatusBarUtil.setStatusBarDarkMode(getActivity()); | ||
| 62 | } | 77 | } |
| 63 | } | 78 | } |
| 64 | 79 | ||
| 65 | - protected abstract int getLayoutId(); | ||
| 66 | -} | ||
| 67 | \ No newline at end of file | 80 | \ No newline at end of file |
| 81 | + | ||
| 82 | +} |
core/base/src/main/java/com/cnlive/libs/base/frame/presenter/BasePresenter.java
| 1 | package com.cnlive.libs.base.frame.presenter; | 1 | package com.cnlive.libs.base.frame.presenter; |
| 2 | 2 | ||
| 3 | - | ||
| 4 | import com.cnlive.libs.base.frame.view.BaseView; | 3 | import com.cnlive.libs.base.frame.view.BaseView; |
| 5 | import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter; | 4 | import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter; |
| 6 | 5 | ||
| 7 | -public class BasePresenter extends MvpBasePresenter<BaseView> { | 6 | +public class BasePresenter<M extends BaseView> extends MvpBasePresenter<BaseView> { |
| 8 | } | 7 | } |
core/base/src/main/java/com/cnlive/libs/base/frame/view/BaseView.java
| @@ -2,6 +2,11 @@ package com.cnlive.libs.base.frame.view; | @@ -2,6 +2,11 @@ package com.cnlive.libs.base.frame.view; | ||
| 2 | 2 | ||
| 3 | import com.hannesdorfmann.mosby3.mvp.MvpView; | 3 | import com.hannesdorfmann.mosby3.mvp.MvpView; |
| 4 | 4 | ||
| 5 | +/** | ||
| 6 | + * @author ys | ||
| 7 | + * 基类View | ||
| 8 | + */ | ||
| 5 | public class BaseView implements MvpView { | 9 | public class BaseView implements MvpView { |
| 10 | + protected final String TAG = this.getClass().getSimpleName(); | ||
| 6 | 11 | ||
| 7 | } | 12 | } |
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,7 +36,7 @@ import com.cnlive.libs.base.media.picker.view.RangeSeekBar; | ||
| 36 | import com.cnlive.libs.base.util.AlertUtil; | 36 | import com.cnlive.libs.base.util.AlertUtil; |
| 37 | import com.cnlive.libs.base.util.ExtractVideoInfoUtil; | 37 | import com.cnlive.libs.base.util.ExtractVideoInfoUtil; |
| 38 | import com.cnlive.libs.base.util.PictureUtils; | 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 | import com.cnlive.libs.video.video.VideoView; | 40 | import com.cnlive.libs.video.video.VideoView; |
| 41 | import com.cnlive.strike.base.R; | 41 | import com.cnlive.strike.base.R; |
| 42 | import com.qiniu.pili.droid.shortvideo.PLShortVideoTrimmer; | 42 | import com.qiniu.pili.droid.shortvideo.PLShortVideoTrimmer; |
| @@ -105,7 +105,7 @@ public class EditVideoActivity extends AppCompatActivity implements View.OnClick | @@ -105,7 +105,7 @@ public class EditVideoActivity extends AppCompatActivity implements View.OnClick | ||
| 105 | super.onCreate(savedInstanceState); | 105 | super.onCreate(savedInstanceState); |
| 106 | setContentView(R.layout.activity_edit_video); | 106 | setContentView(R.layout.activity_edit_video); |
| 107 | QMUIStatusBarHelper.setStatusBarDarkMode(this); | 107 | QMUIStatusBarHelper.setStatusBarDarkMode(this); |
| 108 | - StatusBarUtil2.setColor(this, Color.BLACK, 0); | 108 | + StatusBarUtil.setColor(this, Color.BLACK, 0); |
| 109 | initData(); | 109 | initData(); |
| 110 | initView(); | 110 | initView(); |
| 111 | initEditVideo(); | 111 | initEditVideo(); |
core/base/src/main/java/com/cnlive/libs/base/util/ReflectUtil.java
| @@ -78,6 +78,7 @@ public class ReflectUtil { | @@ -78,6 +78,7 @@ public class ReflectUtil { | ||
| 78 | 78 | ||
| 79 | 79 | ||
| 80 | /** | 80 | /** |
| 81 | + * ys | ||
| 81 | * 创建view的实例 | 82 | * 创建view的实例 |
| 82 | * | 83 | * |
| 83 | * @param position | 84 | * @param position |
| @@ -95,7 +96,7 @@ public class ReflectUtil { | @@ -95,7 +96,7 @@ public class ReflectUtil { | ||
| 95 | clazz = (Class<R>) pt.getActualTypeArguments()[position]; | 96 | clazz = (Class<R>) pt.getActualTypeArguments()[position]; |
| 96 | } catch (Exception e) { | 97 | } catch (Exception e) { |
| 97 | //直接使用基类 | 98 | //直接使用基类 |
| 98 | - clazz=baseView; | 99 | + clazz = baseView; |
| 99 | } | 100 | } |
| 100 | Constructor constructor = null; | 101 | Constructor constructor = null; |
| 101 | try { | 102 | try { |
| @@ -121,6 +122,16 @@ public class ReflectUtil { | @@ -121,6 +122,16 @@ public class ReflectUtil { | ||
| 121 | return result; | 122 | return result; |
| 122 | } | 123 | } |
| 123 | 124 | ||
| 125 | + /** | ||
| 126 | + * 初始化presenter | ||
| 127 | + * ys | ||
| 128 | + * | ||
| 129 | + * @param position | ||
| 130 | + * @param type | ||
| 131 | + * @param basePresenter | ||
| 132 | + * @param <R> | ||
| 133 | + * @return | ||
| 134 | + */ | ||
| 124 | public static <R> R instancePresenter(int position, Class type, Class basePresenter) { | 135 | public static <R> R instancePresenter(int position, Class type, Class basePresenter) { |
| 125 | R result = null; | 136 | R result = null; |
| 126 | try { | 137 | try { |
| @@ -128,6 +139,7 @@ public class ReflectUtil { | @@ -128,6 +139,7 @@ public class ReflectUtil { | ||
| 128 | Class<R> clazz = (Class<R>) pt.getActualTypeArguments()[position]; | 139 | Class<R> clazz = (Class<R>) pt.getActualTypeArguments()[position]; |
| 129 | result = clazz.newInstance(); | 140 | result = clazz.newInstance(); |
| 130 | } catch (Exception ignored) { | 141 | } catch (Exception ignored) { |
| 142 | + //找不到就给基类 | ||
| 131 | Class<R> clazz = basePresenter; | 143 | Class<R> clazz = basePresenter; |
| 132 | try { | 144 | try { |
| 133 | result = clazz.newInstance(); | 145 | result = clazz.newInstance(); |
core/base/src/main/java/com/cnlive/libs/base/util/StatusBarConfig.java
0 → 100644
core/base/src/main/java/com/cnlive/libs/base/util/StatusBarUtil.java
| @@ -2,17 +2,734 @@ package com.cnlive.libs.base.util; | @@ -2,17 +2,734 @@ package com.cnlive.libs.base.util; | ||
| 2 | 2 | ||
| 3 | import android.annotation.TargetApi; | 3 | import android.annotation.TargetApi; |
| 4 | import android.app.Activity; | 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 | import com.qmuiteam.qmui.util.QMUIStatusBarHelper; | 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 | @SuppressWarnings("unused") | 30 | @SuppressWarnings("unused") |
| 13 | public class StatusBarUtil { | 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 | * 支持 4.4 以上版本 MIUI 和 Flyme,以及 6.0 以上版本的其他 Android | 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 | -} |