Created
July 14, 2021 08:03
-
-
Save puke3615/ceabee7d926e8c5e39407855685bbb53 to your computer and use it in GitHub Desktop.
ScreenUtil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.Context; | |
import android.content.res.Resources; | |
import android.graphics.Color; | |
import android.os.Build; | |
import android.provider.Settings; | |
import android.view.View; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import java.lang.reflect.Method; | |
/** | |
* 提供屏幕处理相关功能 | |
* | |
* @author puke | |
* @version 2018/5/21 | |
*/ | |
public class ScreenUtil { | |
private ScreenUtil() { | |
} | |
/** | |
* 通过设置全屏,设置状态栏透明 | |
*/ | |
public static void fullScreen(Activity activity) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
// 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色 | |
Window window = activity.getWindow(); | |
View decorView = window.getDecorView(); | |
// 两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间 | |
int option = 0; | |
option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | |
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE; | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
option |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; | |
} | |
decorView.setSystemUiVisibility(option); | |
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); | |
window.setStatusBarColor(Color.TRANSPARENT); | |
// 导航栏颜色也可以正常设置 | |
// window.setNavigationBarColor(Color.TRANSPARENT); | |
} else { | |
Window window = activity.getWindow(); | |
WindowManager.LayoutParams attributes = window.getAttributes(); | |
int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; | |
int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; | |
attributes.flags |= flagTranslucentStatus; | |
// attributes.flags |= flagTranslucentNavigation; | |
window.setAttributes(attributes); | |
} | |
} | |
} | |
/** | |
* 设置沉浸式状态栏属性 | |
*/ | |
public static void setImmersiveStatusBar(Activity activity) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
activity.getWindow().setStatusBarColor(Color.TRANSPARENT); | |
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | |
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); | |
} else if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) { | |
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, | |
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); | |
} | |
} | |
public static int getNavigationBarHeight() { | |
Context context = App.getApplication(); | |
Resources resources = context.getResources(); | |
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); | |
if (resourceId > 0) { | |
//判断底部导航栏是否为显示状态 | |
boolean navigationBarShowing = isNavigationBarShowing(); | |
if (navigationBarShowing) { | |
return resources.getDimensionPixelSize(resourceId); | |
} | |
} | |
return 0; | |
} | |
public static boolean isNavigationBarShowing() { | |
Context context = App.getApplication(); | |
//判断手机底部是否支持导航栏显示 | |
boolean haveNavigationBar = checkDeviceHasNavigationBar(); | |
if (haveNavigationBar) { | |
if (Build.VERSION.SDK_INT >= 17) { | |
String brand = Build.BRAND; | |
String mDeviceInfo; | |
if (brand.equalsIgnoreCase("HUAWEI")) { | |
mDeviceInfo = "navigationbar_is_min"; | |
} else if (brand.equalsIgnoreCase("XIAOMI")) { | |
mDeviceInfo = "force_fsg_nav_bar"; | |
} else if (brand.equalsIgnoreCase("VIVO")) { | |
mDeviceInfo = "navigation_gesture_on"; | |
} else if (brand.equalsIgnoreCase("OPPO")) { | |
mDeviceInfo = "navigation_gesture_on"; | |
} else { | |
mDeviceInfo = "navigationbar_is_min"; | |
} | |
if (Settings.Global.getInt(context.getContentResolver(), mDeviceInfo, 0) == 0) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
private static boolean checkDeviceHasNavigationBar() { | |
Context context = App.getApplication(); | |
boolean hasNavigationBar = false; | |
Resources rs = context.getResources(); | |
int id = rs.getIdentifier("config_showNavigationBar", "bool", "android"); | |
if (id > 0) { | |
hasNavigationBar = rs.getBoolean(id); | |
} | |
try { | |
Class systemPropertiesClass = Class.forName("android.os.SystemProperties"); | |
Method m = systemPropertiesClass.getMethod("get", String.class); | |
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys"); | |
if ("1".equals(navBarOverride)) { | |
hasNavigationBar = false; | |
} else if ("0".equals(navBarOverride)) { | |
hasNavigationBar = true; | |
} | |
} catch (Exception e) { | |
} | |
return hasNavigationBar; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment