下面列出了android.view.DisplayCutout#getSafeInsetRight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 获得刘海屏高度
* Notch height int.
*
* @param activity the activity
* @return the int
*/
public static int getNotchHeight(Activity activity) {
int notchHeight = 0;
int statusBarHeight = ImmersionBar.getStatusBarHeight(activity);
DisplayCutout displayCutout = getDisplayCutout(activity);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && displayCutout != null) {
if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
notchHeight = displayCutout.getSafeInsetTop();
} else {
if (displayCutout.getSafeInsetLeft() == 0) {
notchHeight = displayCutout.getSafeInsetRight();
} else {
notchHeight = displayCutout.getSafeInsetLeft();
}
}
} else {
if (hasNotchAtXiaoMi(activity)) {
notchHeight = getXiaoMiNotchHeight(activity);
}
if (hasNotchAtHuaWei(activity)) {
notchHeight = getHuaWeiNotchSize(activity)[1];
}
if (hasNotchAtVIVO(activity)) {
notchHeight = dp2px(activity, 32);
if (notchHeight < statusBarHeight) {
notchHeight = statusBarHeight;
}
}
if (hasNotchAtOPPO(activity)) {
notchHeight = 80;
if (notchHeight < statusBarHeight) {
notchHeight = statusBarHeight;
}
}
}
return notchHeight;
}
/**
* 获得刘海屏高度
* Notch height int.
*
* @param activity the activity
* @return the int
*/
public static int getNotchHeight(Activity activity) {
int notchHeight = 0;
int statusBarHeight = ImmersionBar.getStatusBarHeight(activity);
DisplayCutout displayCutout = getDisplayCutout(activity);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && displayCutout != null) {
if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
notchHeight = displayCutout.getSafeInsetTop();
} else {
if (displayCutout.getSafeInsetLeft() == 0) {
notchHeight = displayCutout.getSafeInsetRight();
} else {
notchHeight = displayCutout.getSafeInsetLeft();
}
}
} else {
if (hasNotchAtXiaoMi(activity)) {
notchHeight = getXiaoMiNotchHeight(activity);
}
if (hasNotchAtHuaWei(activity)) {
notchHeight = getHuaWeiNotchSize(activity)[1];
}
if (hasNotchAtVIVO(activity)) {
notchHeight = dp2px(activity, 32);
if (notchHeight < statusBarHeight) {
notchHeight = statusBarHeight;
}
}
if (hasNotchAtOPPO(activity)) {
notchHeight = 80;
if (notchHeight < statusBarHeight) {
notchHeight = statusBarHeight;
}
}
}
return notchHeight;
}
public void onBeginLayout() {
switch (mRotation) {
case ROTATION_90:
mRotatedDisplayInfoOverscan.left = mDisplayInfoOverscan.top;
mRotatedDisplayInfoOverscan.top = mDisplayInfoOverscan.right;
mRotatedDisplayInfoOverscan.right = mDisplayInfoOverscan.bottom;
mRotatedDisplayInfoOverscan.bottom = mDisplayInfoOverscan.left;
break;
case ROTATION_180:
mRotatedDisplayInfoOverscan.left = mDisplayInfoOverscan.right;
mRotatedDisplayInfoOverscan.top = mDisplayInfoOverscan.bottom;
mRotatedDisplayInfoOverscan.right = mDisplayInfoOverscan.left;
mRotatedDisplayInfoOverscan.bottom = mDisplayInfoOverscan.top;
break;
case ROTATION_270:
mRotatedDisplayInfoOverscan.left = mDisplayInfoOverscan.bottom;
mRotatedDisplayInfoOverscan.top = mDisplayInfoOverscan.left;
mRotatedDisplayInfoOverscan.right = mDisplayInfoOverscan.top;
mRotatedDisplayInfoOverscan.bottom = mDisplayInfoOverscan.right;
break;
default:
mRotatedDisplayInfoOverscan.set(mDisplayInfoOverscan);
break;
}
mRestrictedOverscan.set(0, 0, mDisplayWidth, mDisplayHeight);
mOverscan.set(mRestrictedOverscan);
mSystem.set(mRestrictedOverscan);
mUnrestricted.set(mRotatedDisplayInfoOverscan);
mUnrestricted.right = mDisplayWidth - mUnrestricted.right;
mUnrestricted.bottom = mDisplayHeight - mUnrestricted.bottom;
mRestricted.set(mUnrestricted);
mDock.set(mUnrestricted);
mContent.set(mUnrestricted);
mVoiceContent.set(mUnrestricted);
mStable.set(mUnrestricted);
mStableFullscreen.set(mUnrestricted);
mCurrent.set(mUnrestricted);
mDisplayCutout = mDisplayInfoCutout;
mDisplayCutoutSafe.set(Integer.MIN_VALUE, Integer.MIN_VALUE,
Integer.MAX_VALUE, Integer.MAX_VALUE);
if (!mDisplayCutout.getDisplayCutout().isEmpty()) {
final DisplayCutout c = mDisplayCutout.getDisplayCutout();
if (c.getSafeInsetLeft() > 0) {
mDisplayCutoutSafe.left = mRestrictedOverscan.left + c.getSafeInsetLeft();
}
if (c.getSafeInsetTop() > 0) {
mDisplayCutoutSafe.top = mRestrictedOverscan.top + c.getSafeInsetTop();
}
if (c.getSafeInsetRight() > 0) {
mDisplayCutoutSafe.right = mRestrictedOverscan.right - c.getSafeInsetRight();
}
if (c.getSafeInsetBottom() > 0) {
mDisplayCutoutSafe.bottom = mRestrictedOverscan.bottom - c.getSafeInsetBottom();
}
}
}
@VisibleForTesting
void adjustGlobalMenuButtonPosition(
@Nullable DisplayCutout displayCutout, List<Rect> cutoutRects) {
LayoutParams globalMenuButtonParams = globalMenuButtonOverlay.getParams();
if (displayCutout == null) {
globalMenuButtonParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
} else {
Point screenSize = ScreenUtils.getRealScreenSize(getContext());
View menuButton = globalMenuButtonOverlay.findViewById(R.id.global_menu_button);
int menuButtonWidth = menuButton.getWidth();
int leftOfCenteredMenuButton = (screenSize.x / 2) - (menuButtonWidth / 2);
Rect centeredMenuButton =
new Rect(
leftOfCenteredMenuButton,
0,
leftOfCenteredMenuButton + menuButtonWidth,
menuButton.getHeight());
// Clear the margins.
MarginLayoutParams menuButtonMarginParams = (MarginLayoutParams) menuButton.getLayoutParams();
menuButtonMarginParams.setMargins(0, 0, 0, 0);
// Find the cutout that intersects the centered menu button. If there's space to the right of
// the cutout, move there. Otherwise if there's space to the left, move there. If neither side
// has space, move just below the cutout.
int safeInsetLeft = displayCutout.getSafeInsetLeft();
int safeInsetRight = displayCutout.getSafeInsetRight();
boolean cutoutIntersectsCenteredMenuButton = false;
for (Rect cutoutRect : cutoutRects) {
if (Rect.intersects(centeredMenuButton, cutoutRect)) {
cutoutIntersectsCenteredMenuButton = true;
if ((screenSize.x - safeInsetRight - cutoutRect.right) > menuButtonWidth) {
globalMenuButtonParams.gravity = Gravity.TOP | Gravity.END;
menuButtonMarginParams.rightMargin = screenSize.x - cutoutRect.right - menuButtonWidth;
} else if ((cutoutRect.left - safeInsetLeft) > menuButtonWidth) {
globalMenuButtonParams.gravity = Gravity.TOP | Gravity.START;
menuButtonMarginParams.leftMargin = cutoutRect.left - menuButtonWidth;
} else {
globalMenuButtonParams.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
menuButtonMarginParams.topMargin = displayCutout.getSafeInsetTop();
}
}
}
if (!cutoutIntersectsCenteredMenuButton) {
globalMenuButtonParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
}
}
globalMenuButtonOverlay.setParams(globalMenuButtonParams);
}