mirror of
https://code.gri.mw/GUI/grim.git
synced 2026-07-17 12:19:09 +00:00
gui: optimize paddings
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package mw.gri.android;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.system.ErrnoException;
|
||||
import android.system.Os;
|
||||
@@ -18,14 +19,11 @@ public class MainActivity extends GameActivity {
|
||||
} catch (ErrnoException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// getDisplayCutouts();
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
int navBarHeight = Utils.getNavigationBarHeight(getApplicationContext());
|
||||
findViewById(android.R.id.content).setPadding(0, 0, 0, navBarHeight);
|
||||
}
|
||||
|
||||
public Integer getStatusBarHeight() {
|
||||
return Utils.getStatusBarHeight(getApplicationContext());
|
||||
public int[] getDisplayCutouts() {
|
||||
return Utils.getDisplayCutouts(this);
|
||||
}
|
||||
}
|
||||
@@ -1,75 +1,52 @@
|
||||
package mw.gri.android;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Point;
|
||||
import android.os.Build;
|
||||
import android.view.Display;
|
||||
import android.view.DisplayCutout;
|
||||
import android.view.WindowInsets;
|
||||
import android.view.WindowManager;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.DisplayCutoutCompat;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
public class Utils {
|
||||
|
||||
public static int getStatusBarHeight(Context context) {
|
||||
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
public static int[] getDisplayCutouts(Activity context) {
|
||||
int[] cutouts = new int[]{0, 0, 0, 0};
|
||||
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
|
||||
return windowManager
|
||||
.getCurrentWindowMetrics()
|
||||
.getWindowInsets()
|
||||
.getInsets(WindowInsets.Type.navigationBars())
|
||||
.bottom;
|
||||
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
WindowInsets insets = windowManager.getCurrentWindowMetrics().getWindowInsets();
|
||||
android.graphics.Insets barsInsets = insets.getInsets(WindowInsets.Type.systemBars());
|
||||
android.graphics.Insets cutoutsInsets = insets.getInsets(WindowInsets.Type.displayCutout());
|
||||
cutouts[0] = pxToDp(Integer.max(barsInsets.top, cutoutsInsets.top), context);
|
||||
cutouts[1] = pxToDp(Integer.max(barsInsets.right, cutoutsInsets.right), context);
|
||||
cutouts[2] = pxToDp(Integer.max(barsInsets.bottom, cutoutsInsets.bottom), context);
|
||||
cutouts[3] = pxToDp(Integer.max(barsInsets.left, cutoutsInsets.left), context);
|
||||
} else if (Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.Q) {
|
||||
DisplayCutout displayCutout = context.getWindowManager().getDefaultDisplay().getCutout();
|
||||
cutouts[0] = displayCutout.getSafeInsetBottom();
|
||||
cutouts[1] = displayCutout.getSafeInsetRight();
|
||||
cutouts[2] = displayCutout.getSafeInsetBottom();
|
||||
cutouts[3] = displayCutout.getSafeInsetLeft();
|
||||
} else {
|
||||
Resources res = context.getResources();
|
||||
int statusBarHeight = 24;
|
||||
@SuppressLint({"DiscouragedApi", "InternalInsetResource"})
|
||||
int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
|
||||
if (resourceId > 0) {
|
||||
statusBarHeight = res.getDimensionPixelSize(resourceId);
|
||||
WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(context.getWindow().getDecorView());
|
||||
if (insets != null) {
|
||||
DisplayCutoutCompat displayCutout = insets.getDisplayCutout();
|
||||
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
|
||||
if (displayCutout != null) {
|
||||
cutouts[0] = pxToDp(Integer.max(displayCutout.getSafeInsetTop(), systemBars.top), context);
|
||||
cutouts[1] = pxToDp(Integer.max(displayCutout.getSafeInsetRight(), systemBars.right), context);
|
||||
cutouts[2] = pxToDp(Integer.max(displayCutout.getSafeInsetBottom(), systemBars.bottom), context);
|
||||
cutouts[3] = pxToDp(Integer.max(displayCutout.getSafeInsetLeft(), systemBars.left), context);
|
||||
}
|
||||
}
|
||||
return statusBarHeight;
|
||||
}
|
||||
return cutouts;
|
||||
}
|
||||
|
||||
public static int getNavigationBarHeight(Context context) {
|
||||
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
|
||||
return windowManager
|
||||
.getCurrentWindowMetrics()
|
||||
.getWindowInsets()
|
||||
.getInsets(WindowInsets.Type.navigationBars())
|
||||
.bottom;
|
||||
} else {
|
||||
Point appUsableSize = getAppUsableScreenSize(context);
|
||||
Point realScreenSize = getRealScreenSize(context);
|
||||
|
||||
// navigation bar on the side
|
||||
if (appUsableSize.x < realScreenSize.x) {
|
||||
return appUsableSize.y;
|
||||
}
|
||||
|
||||
// navigation bar at the bottom
|
||||
if (appUsableSize.y < realScreenSize.y) {
|
||||
return realScreenSize.y - appUsableSize.y;
|
||||
}
|
||||
|
||||
// navigation bar is not present
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static Point getAppUsableScreenSize(Context context) {
|
||||
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
Point size = new Point();
|
||||
windowManager.getDefaultDisplay().getSize(size);
|
||||
return size;
|
||||
}
|
||||
|
||||
private static Point getRealScreenSize(Context context) {
|
||||
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
Display display = windowManager.getDefaultDisplay();
|
||||
Point size = new Point();
|
||||
display.getRealSize(size);
|
||||
return size;
|
||||
private static int pxToDp(int px, Context context) {
|
||||
return (int) (px / context.getResources().getDisplayMetrics().density);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user