You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ed7b37c
Browse filesBrowse the repository at this point in the historyBrowse files
The engine computes metrics from the current `Window` and automatically tracks window changes (fold/unfold, split-screen resizing, orientation changes), while design widths or native fallbacks for large/unusual screens should be defined in `strategy` to match your product design. `ScreenAdapt.getWindowInfo(activity)`and `activity.resources.configuration` are already refreshed before `strategy`is invoked and can be read safely inside it:
97
+
The engine computes metrics from the current `Window` and automatically tracks window changes (fold/unfold, OEM MagicWindow/letterboxing, split-screen resizing, orientation changes), while tier thresholds or native fallbacks for large/unusual screens should be defined in `strategy` to match your product design. `ScreenAdapt.getWindowInfo(activity)`, `ScreenAdapt.getNativeDensity(activity)`, and `activity.resources.configuration` are refreshed before `strategy`runs and can be read safely inside it:
98
98
99
99
```kotlin
100
100
ScreenAdapt.init(this) {
101
-
designSize(360f, 640f)
101
+
designSize(375f, 667f)
102
102
excludeFontScale(true)
103
103
strategy { _, activity ->
104
104
val info =ScreenAdapt.getWindowInfo(activity) ?:return@strategy null
105
+
val w = info.widthPx
106
+
val h = info.heightPx
107
+
if (w <=0|| h <=0) return@strategy null
108
+
109
+
val shortPx = minOf(w, h)
110
+
val longPx = maxOf(w, h)
111
+
val realShort = minOf(info.realWidthPx, info.realHeightPx)
112
+
val realLong = maxOf(info.realWidthPx, info.realHeightPx)
113
+
val ratio = w.toFloat() / h.toFloat()
114
+
115
+
// Detect split-screen, freeform, and OEM MagicWindow/letterboxed windows
116
+
// (where activity.isInMultiWindowMode may still report false).
117
+
val isSubWindow = info.inMultiWindow ||
118
+
(realShort >0&& shortPx < realShort *0.85f) ||
119
+
(realLong >0&& longPx < realLong *0.85f)
120
+
105
121
val swDp = activity.resources.configuration.smallestScreenWidthDp
106
-
val shortPx = minOf(info.widthPx, info.heightPx)
107
-
val longPx = maxOf(info.widthPx, info.heightPx)
108
-
val aspectRatio =if (longPx >0) shortPx.toFloat() / longPx else0f
122
+
val nativeDensity =ScreenAdapt.getNativeDensity(activity)
123
+
val isTabletDevice = swDp >=600&& (realLong.toFloat() / realShort.coerceAtLeast(1)) >1.2f
109
124
110
125
when {
111
-
// 1. Split-screen / freeform window: adapt to current window width (or ShortSide for window short side)
> - Thresholds above (`0.8f`, `600dp`, `450dp`, `720f`/`420f`) are reference examples—adjust them to match your design specs. If you want tablets to disable adaptation app-wide, you can skip calling `ScreenAdapt.init(...)` in `Application#onCreate` (`ScreenMetrics` remains safe to call).
136
-
> - To handle fold/unfold or rotation **without recreating the `Activity`**, declare `android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout"` in `AndroidManifest.xml`; the engine will automatically recalculate density on configuration and layout changes.
156
+
> - Thresholds above (`375f / 420f / 600f / 750f`, `667f`, `1500px`, `2.5f`) are reference examples—adjust them to match your design specs. If tablets (including MagicWindow) should use native responsive layouts, return `AdaptDirective.Native` for tablets or skip calling `ScreenAdapt.init(...)` in `Application#onCreate` (`ScreenMetrics` remains safe to call).
157
+
> -**Always return `AdaptDirective.Native` (never `AdaptDirective.Skip`) to opt out of scaling**: `Skip` leaves the previous `DisplayMetrics` untouched, so rotating without Activity recreation (for instance, entering a centered MagicWindow on a tablet) would retain the portrait density and clip the UI.
158
+
> - To handle fold/unfold, MagicWindow transitions, or rotation **without recreating the `Activity`**, declare `android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout"` in `AndroidManifest.xml`; the engine will automatically recalculate density on configuration and layout changes.
137
159
138
160
See [API Documentation](docs/API.en.md) for full signatures, configuration options, directives, and `ScreenMetrics` utilities.
@@ -145,13 +149,13 @@ Semantics and common use cases for each `AdaptDirective`:
145
149
| :--- | :--- | :--- |
146
150
| `AdaptDirective.Width(sizeDp)` | Computes `density = widthPx / sizeDp` using current available window width | Standard portrait pages, widened design specs on unfolded foldables (e.g., `720f`) or wide-bar phones |
147
151
| `AdaptDirective.Height(sizeDp)` | Computes `density = heightPx / sizeDp` using current available window height | Full-height layout pages sensitive to vertical proportions |
148
-
| `AdaptDirective.ShortSide(effectiveSizeDp, useRealDisplay =true)` | Scales by short side; uses physical display short side when `useRealDisplay =true` and fullscreen, **automatically falling back to current window short side in multi-window/split-screen orwhen window short side is `<85%` of physical short side** | Landscape pages (keeps visual scale consistent with portrait), large screens, and split-screen compatibility |
152
+
| `AdaptDirective.ShortSide(effectiveSizeDp, useRealDisplay =true)` | Scales by short side; uses physical display short side when `useRealDisplay =true` and fullscreen, **automatically falling back to current window short side in multi-window/split-screen orwheneither window short side or long side is `<85%` of the physical display (e.g., OEMMagicWindow/ freeform)** | Landscape pages (keeps visual scale consistent with portrait), large screens, MagicWindow, and split-screen compatibility |
149
153
| `AdaptDirective.Native` | Restores the target `Activity`'s own native snapshot without writing to `Application` | Tablet/large-screen responsive pages, login/media/Compose pages that should not be scaled |
150
-
| `AdaptDirective.Skip` | Skips this adaptation pass and keeps current metrics untouched | Transitional pages or screens managed manually |
154
+
| `AdaptDirective.Skip` | Skips this adaptation pass and keeps current `DisplayMetrics` untouched (**does not restore native density**; use `Native` to opt out) | Transitional pages or screens managed manually |
> - The engine refreshes the current `WindowInfo` before invoking `AdaptStrategy.resolve(target, activity)`, so strategies can safely call `ScreenAdapt.getWindowInfo(activity)` and read `activity.resources.configuration` (`smallestScreenWidthDp` is never mutated by this library and always reflects the system native value).
158
+
> - The engine refreshes the current `WindowInfo` before invoking `AdaptStrategy.resolve(target, activity)`, so strategies can safely call `ScreenAdapt.getWindowInfo(activity)`, `ScreenAdapt.getNativeDensity(activity)`, and read `activity.resources.configuration` (`smallestScreenWidthDp` is never mutated by this library and always reflects the system native value).
155
159
> - On foldable fold/unfold, wide-bar rotation, or split-screen/freeform resizing, the engine automatically refreshes window bounds and re-runs `AdaptStrategy` via `ComponentCallbacks.onConfigurationChanged` (API 29+) and `OnGlobalLayout`. Pages that should not recreate on resize must declare `android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout"` in `AndroidManifest.xml`.
156
160
> - Returning `null` from `AdaptStrategy` falls back to the global `designWidthDp` / `designHeightDp` and `basis`.
0 commit comments