Skip to content

Commit

Permalink
新增:StatusBarUtil增加判断是否状态栏隐藏isHideStatusBar、隐藏状态栏hideStatusBar、设置全屏set…
Browse files Browse the repository at this point in the history
…FullScreen方法

新增:NavigationBarUtil增加判断是否导航栏隐藏isHideNavigationBar、隐藏导航栏hideNavigationBar方法
  • Loading branch information
[email protected] authored and [email protected] committed Aug 1, 2019
1 parent 4d4e90b commit 70f4eea
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 77 deletions.
Binary file modified apk/sample.apk
Binary file not shown.
19 changes: 15 additions & 4 deletions app/src/main/java/com/aries/ui/widget/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import android.view.ViewTreeObserver;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;

import com.aries.ui.helper.navigation.NavigationBarUtil;
import com.aries.ui.util.NotchUtil;
import com.aries.ui.util.StatusBarUtil;
Expand All @@ -29,9 +32,6 @@
import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;

/**
* @Author: AriesHoo on 2019/4/11 13:08
* @E-Mail: [email protected]
Expand Down Expand Up @@ -104,6 +104,8 @@ private void showNotch() {
.show();
}

boolean mIsFull;

@Override
protected void initView(Bundle bundle) {
int padding = getResources().getDimensionPixelSize(R.dimen.dp_margin);
Expand All @@ -114,6 +116,13 @@ protected void initView(Bundle bundle) {
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.dp_text_size_main));
mTextView.setBackgroundColor(Color.WHITE);
mAdapter.addHeaderView(mTextView);
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StatusBarUtil.setFullScreen(mContext, !mIsFull);
mIsFull = !mIsFull;
}
});
List<WidgetEntity> list = new ArrayList<>();
list.add(new WidgetEntity("StatusViewHelper", "支持顶部自定义标题栏沉浸效果帮助类。", StatusViewHelperActivity.class));
list.add(new WidgetEntity("KeyboardHelper", "解决软键盘遮住底部输入框问题帮助类。", TitleEditActivity.class));
Expand Down Expand Up @@ -152,7 +161,9 @@ public void onGlobalLayout() {
+ "<br>屏幕纵横比:" + NavigationBarUtil.getAspectRatio(mContext)
+ "<br>是否开启全面屏手势:根据判断系统是否开启虚拟导航栏(如华为可手动开关该方法不是完全正确的):" + NavigationBarUtil.isOpenFullScreenGestures(mContext)
+ "<br>是否有导航栏:" + NavigationBarUtil.hasNavBar(mContext)
+ "<br>导航栏高度:" + NavigationBarUtil.getNavigationBarHeight(mContext)));
+ "<br>导航栏高度:" + NavigationBarUtil.getNavigationBarHeight(mContext)
+ "<br>是否隐藏状态栏:" + StatusBarUtil.isStatusBar(mContext)
+ "<br>是否隐藏导航栏:" + NavigationBarUtil.isHideNavigationBar(mContext)));
}
}
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ ext {
minLibSdkVersion = 15
targetSdkVersion = 28
supportVersion = "1.0.0"
versionCode = 48
versionName = "3.2.18"
versionCode = 49
versionName = "3.2.19"
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* 6、2019-4-15 10:30:13 新增导航栏图标颜色深浅方法{@link #setNavigationBarDarkMode(Window)}{@link #setNavigationBarDarkMode(Activity)}
* {@link #setNavigationBarLightMode(Window)} {@link #setNavigationBarLightMode(Activity)}
* 7、2019-7-31 21:20:50 增加判断是否支持导航栏文字及icon黑色变化方法{@link #isSupportNavigationBarFontChange()}
* 8、2019-8-1 11:26:10 增加判断是否隐藏导航栏方法{@link #isHideNavigationBar(Window)}{@link #isHideNavigationBar(Activity)}
*/
public class NavigationBarUtil {

Expand Down Expand Up @@ -114,6 +115,97 @@ public static boolean isOpenFullScreenGestures(Context context) {
return false;
}

public static boolean isNavigationAtBottom(Activity activity) {
if (activity == null) {
return false;
}
return isNavigationAtBottom(activity.getWindow());
}

/**
* 判断导航栏是否在底部
*
* @param window
* @return
*/
public static boolean isNavigationAtBottom(Window window) {
if (window == null) {
return false;
}
boolean mInPortrait = (Resources.getSystem().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
float mSmallestWidthDp = getSmallestWidthDp(window);
return (mSmallestWidthDp >= 600 || mInPortrait);
}

/**
* 是否隐藏状态栏
*
* @param activity Activity对象
* @return 是否隐藏
*/
public static boolean isHideNavigationBar(Activity activity) {
if (activity == null) {
return false;
}
return isHideNavigationBar(activity.getWindow());
}

/**
* 是否隐藏状态栏
*
* @param window Window对象
* @return 是否隐藏
*/
public static boolean isHideNavigationBar(Window window) {
if (window == null) {
return false;
}
return (window.getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
== View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}

/**
* 判断系统是否支持导航栏文字及图标颜色变化
*
* @return true支持导航栏文字颜色变化
*/
public static boolean isSupportNavigationBarFontChange() {
if (RomUtil.isEMUI() && RomUtil.getEMUIVersion().compareTo("EmotionUI_4.1") > 0) {
return true;
}
if (RomUtil.getMIUIVersionCode() >= 6) {
return true;
}
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
}

/**
* 判断当前手机是否为全面屏--通过纵横比
*
* @param context
* @return
*/
public static boolean isFullScreenDevice(Context context) {
if (context == null) {
return false;
}
if (mHasCheckFullScreen) {
return mIsFullScreenDevice;
}
mHasCheckFullScreen = true;
mIsFullScreenDevice = false;
// 低于 API 21的,都不会是全面屏
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return false;
}
mIsFullScreenDevice = getAspectRatio(context) >= mAspectRatio;
return mIsFullScreenDevice;
}

/**
* @param activity
* @return
*/
public static boolean hasNavBar(Activity activity) {
if (activity == null) {
return false;
Expand All @@ -135,6 +227,9 @@ public static boolean hasNavBar(Window window) {
if (isOpenFullScreenGestures(window.getContext())) {
return false;
}
if (isHideNavigationBar(window)) {
return false;
}
//其他手机根据屏幕真实高度与显示高度是否相同来判断
WindowManager windowManager = window.getWindowManager();
Display d = windowManager.getDefaultDisplay();
Expand All @@ -151,6 +246,46 @@ public static boolean hasNavBar(Window window) {
return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}

/**
* 隐藏导航栏
*
* @param activity Activity 对象
* @param isHide 是否隐藏
*/
public static void hideNavigationBar(Activity activity, boolean isHide) {
if (activity == null) {
return;
}
hideNavigationBar(activity.getWindow(), isHide);
}

/**
* 隐藏导航栏
*
* @param window Window 对象
* @param isHide 是否隐藏
*/
public static void hideNavigationBar(Window window, boolean isHide) {
if (window == null) {
return;
}
//防止系统栏隐藏时内容区域大小发生变化
int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | window.getDecorView().getSystemUiVisibility();
if (isHide) {
if (!isHideNavigationBar(window)) {
uiFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
} else {
if (isHideNavigationBar(window)) {
uiFlags ^= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
^ View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
}
uiFlags |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
window.getDecorView().setSystemUiVisibility(uiFlags);
}

public static int getFakeNavigationBarHeight(Activity activity) {
if (activity == null) {
return 0;
Expand Down Expand Up @@ -268,44 +403,6 @@ public static int getNavigationBarHeight(WindowManager windowManager) {

}

/**
* 判断系统是否支持导航栏文字及图标颜色变化
*
* @return true支持导航栏文字颜色变化
*/
public static boolean isSupportNavigationBarFontChange() {
if (RomUtil.isEMUI() && RomUtil.getEMUIVersion().compareTo("EmotionUI_4.1") > 0) {
return true;
}
if (RomUtil.getMIUIVersionCode() >= 6) {
return true;
}
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
}

/**
* 判断当前手机是否为全面屏--通过纵横比
*
* @param context
* @return
*/
public static boolean isFullScreenDevice(Context context) {
if (context == null) {
return false;
}
if (mHasCheckFullScreen) {
return mIsFullScreenDevice;
}
mHasCheckFullScreen = true;
mIsFullScreenDevice = false;
// 低于 API 21的,都不会是全面屏
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return false;
}
mIsFullScreenDevice = getAspectRatio(context) >= mAspectRatio;
return mIsFullScreenDevice;
}

/**
* 获取手机纵横比
*
Expand Down Expand Up @@ -350,28 +447,6 @@ private static float getSmallestWidthDp(Window window) {
return Math.min(widthDp, heightDp);
}

public static boolean isNavigationAtBottom(Activity activity) {
if (activity == null) {
return false;
}
return isNavigationAtBottom(activity.getWindow());
}

/**
* 判断导航栏是否在底部
*
* @param window
* @return
*/
public static boolean isNavigationAtBottom(Window window) {
if (window == null) {
return false;
}
boolean mInPortrait = (Resources.getSystem().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
float mSmallestWidthDp = getSmallestWidthDp(window);
return (mSmallestWidthDp >= 600 || mInPortrait);
}


public static final int NAVIGATION_BAR_TYPE_DEFAULT = 0;
public static final int NAVIGATION_BAR_TYPE_MI_UI = 1;
Expand Down
Loading

0 comments on commit 70f4eea

Please sign in to comment.