mirror of
https://github.com/jwinterm/grim.git
synced 2026-10-25 07:58:33 +01:00
gui: optimize paddings
This commit is contained in:
+2
-11
@@ -8,12 +8,10 @@ android {
|
||||
|
||||
defaultConfig {
|
||||
applicationId "mw.gri.android"
|
||||
minSdk 28
|
||||
minSdk 27
|
||||
targetSdk 33
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
@@ -23,10 +21,7 @@ android {
|
||||
}
|
||||
debug {
|
||||
minifyEnabled false
|
||||
//packagingOptions {
|
||||
// doNotStrip '**/*.so'
|
||||
//}
|
||||
//debuggable true
|
||||
debuggable true
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
@@ -40,10 +35,6 @@ dependencies {
|
||||
|
||||
implementation 'androidx.appcompat:appcompat:1.4.1'
|
||||
implementation 'com.google.android.material:material:1.5.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||
|
||||
// To use the Android Frame Pacing library
|
||||
//implementation "androidx.games:games-frame-pacing:1.9.1"
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="yellow">#FFFEF102</color>
|
||||
<color name="yellow_light">#FFFEF102</color>
|
||||
<color name="black">#FF000000</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
<color name="yellow">#FFFEF102</color>
|
||||
<color name="black">#FF000000</color>
|
||||
</resources>
|
||||
@@ -1,13 +1,17 @@
|
||||
<resources xmlns:tools="http://schemas.android.com/tools">
|
||||
<!-- Base application theme. -->
|
||||
<style name="Theme.Main" parent="Theme.AppCompat.Light.NoActionBar">
|
||||
<style name="Theme.Main" parent="Theme.AppCompat.NoActionBar">
|
||||
<item name="colorPrimary">@color/yellow</item>
|
||||
<item name="colorOnPrimary">@color/black</item>
|
||||
|
||||
<item name="android:statusBarColor">?attr/colorPrimary</item>
|
||||
<item name="android:windowLightStatusBar">true</item>
|
||||
<!-- <item name="android:statusBarColor">?attr/colorPrimary</item>-->
|
||||
<!-- <item name="android:windowLightStatusBar">true</item>-->
|
||||
<item name="android:windowTranslucentStatus">true</item>
|
||||
|
||||
<item name="android:navigationBarColor">?attr/colorPrimary</item>
|
||||
<item name="android:windowLightNavigationBar">true</item>
|
||||
<!-- <item name="android:navigationBarColor">?attr/colorPrimary</item>-->
|
||||
<!-- <item name="android:windowLightNavigationBar">true</item>-->
|
||||
<item name="android:windowTranslucentNavigation">true</item>
|
||||
|
||||
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
|
||||
</style>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user