Skip to content

Commit ed7b37c

Browse files
committed
docs: document four-tier wide-foldable strategy, MagicWindow sub-windows, and getNativeDensity
1 parent c6c0835 commit ed7b37c

4 files changed

Lines changed: 105 additions & 57 deletions

File tree

‎README.en.md‎

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,48 +92,70 @@ class ComposePageActivity : ComponentActivity(), NoAdapt {
9292
}
9393
```
9494

95-
### 4. Multi-Form-Factor Configuration (Foldables / Wide-Bar Phones / Tablets / Landscape)
95+
### 4. Multi-Form-Factor Configuration (Foldables / Wide Foldables / Tablets / MagicWindow / Landscape)
9696

97-
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:
9898

9999
```kotlin
100100
ScreenAdapt.init(this) {
101-
designSize(360f, 640f)
101+
designSize(375f, 667f)
102102
excludeFontScale(true)
103103
strategy { _, activity ->
104104
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+
105121
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 else 0f
122+
val nativeDensity = ScreenAdapt.getNativeDensity(activity)
123+
val isTabletDevice = swDp >= 600 && (realLong.toFloat() / realShort.coerceAtLeast(1)) > 1.2f
109124

110125
when {
111-
// 1. Split-screen / freeform window: adapt to current window width (or ShortSide for window short side)
112-
info.inMultiWindow -> AdaptDirective.Width(360f)
113-
114-
// 2. Unfolded foldable (near-square inner display): widen design width to avoid over-scaling 360dp
115-
aspectRatio >= 0.8f -> AdaptDirective.Width(720f)
116-
117-
// 3. Tablets: use a tablet design width or return AdaptDirective.Native for responsive layouts
118-
swDp >= 600 -> AdaptDirective.Native
119-
120-
// 4. Wide-aspect phones (e.g. 16:10 / wide-bar devices): widen design width to show more content
121-
swDp >= 450 || aspectRatio >= 0.6f -> AdaptDirective.Width(420f)
122-
123-
// 5. Standard phone landscape: scale by real display short side to match portrait proportions
124-
info.configurationOrientation == Configuration.ORIENTATION_LANDSCAPE ->
125-
AdaptDirective.ShortSide(360f, useRealDisplay = true)
126-
127-
// 6. Standard phone portrait: fall back to global designSize(360f, 640f)
128-
else -> null
126+
// 1. Near-square unfolded foldable (e.g., Mate X5/X6 inner display)
127+
!isSubWindow && w > 1500 && ratio in 0.85f..1.15f ->
128+
AdaptDirective.Width(750f)
129+
130+
// 2. Fullscreen tablets / tri-foldables (excluding constrained MagicWindow/freeform sub-windows):
131+
// scale to 750dp or return AdaptDirective.Native for responsive layouts.
132+
!isSubWindow && (
133+
(isTabletDevice && shortPx >= 1500) ||
134+
(shortPx >= 1500 && nativeDensity in 0.1f..2.5f) ||
135+
shortPx >= 2000
136+
) -> AdaptDirective.ShortSide(750f, useRealDisplay = false)
137+
138+
// 3. High-PPI wide-foldable inner display (e.g., 7-8" wide foldables): 600dp short side
139+
// to avoid sudden font shrinkage from falling into the tablet 750dp bucket.
140+
!isSubWindow && shortPx >= 1500 ->
141+
AdaptDirective.ShortSide(600f, useRealDisplay = false)
142+
143+
// 4. Phones (portrait/landscape), short/square cover screens, MagicWindow, and split/freeform sub-windows:
144+
// 375dp base with short-screen protection relaxing short side up to 420dp.
145+
else -> {
146+
val effectiveShortDp = ((shortPx.toFloat() / longPx.toFloat()) * 667f)
147+
.coerceIn(375f, 420f)
148+
AdaptDirective.ShortSide(effectiveShortDp, useRealDisplay = !isSubWindow)
149+
}
129150
}
130151
}
131152
}
132153
```
133154

134155
> **Notes**:
135-
> - 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.
137159
138160
See [API Documentation](docs/API.en.md) for full signatures, configuration options, directives, and `ScreenMetrics` utilities.
139161

‎README.md‎

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,48 +92,66 @@ class ComposePageActivity : ComponentActivity(), NoAdapt {
9292
}
9393
```
9494

95-
### 4. 多形态设备配置(折叠屏 / 阔直板 / 平板 / 横屏)
95+
### 4. 多形态设备配置(折叠屏 / 阔折叠 / 平板 / 平行视界 / 横屏)
9696

97-
底层已按当前 `Window` 实时计算并自动监听窗口变化(折叠展开、分屏拖拽、横竖屏切换),但大屏与异形比例的**设计稿宽度或是否退回原生**需要结合业务需求在 `strategy` 中指定。引擎在回调 `strategy` 前已刷新当前窗口信息,可直接安全读取 `ScreenAdapt.getWindowInfo(activity)` 与 `activity.resources.configuration`:
97+
底层已按当前 `Window` 实时计算并自动监听窗口变化(折叠展开、平行视界、分屏拖拽、横竖屏切换),但大屏与异形比例的**设计稿分档或原生回退**需要结合业务需求在 `strategy` 中指定。引擎在回调 `strategy` 前已刷新当前窗口信息,可直接安全读取 `ScreenAdapt.getWindowInfo(activity)`、`ScreenAdapt.getNativeDensity(activity)` 与 `activity.resources.configuration`:
9898

9999
```kotlin
100100
ScreenAdapt.init(this) {
101-
designSize(360f, 640f)
101+
designSize(375f, 667f)
102102
excludeFontScale(true)
103103
strategy { _, activity ->
104104
val info = ScreenAdapt.getWindowInfo(activity) ?: return@strategy null
105-
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 else 0f
109-
110-
when {
111-
// 1. 分屏 / 自由窗口:按当前窗口宽度适配(或返回 ShortSide 自动取窗口短边)
112-
info.inMultiWindow -> AdaptDirective.Width(360f)
113-
114-
// 2. 折叠屏展开态(接近方形内屏):扩大设计宽度,避免 360dp 等比过度放大
115-
aspectRatio >= 0.8f -> AdaptDirective.Width(720f)
105+
val w = info.widthPx
106+
val h = info.heightPx
107+
if (w <= 0 || h <= 0) return@strategy null
116108

117-
// 3. 平板:按大屏设计稿适配,或返回 AdaptDirective.Native 走原生响应式布局
118-
swDp >= 600 -> AdaptDirective.Native
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()
119114

120-
// 4. 阔直板等宽屏手机(如 16:10 / 宽比例直板机):适当放宽设计宽度以展示更多内容
121-
swDp >= 450 || aspectRatio >= 0.6f -> AdaptDirective.Width(420f)
115+
// 识别分屏、自由小窗及华为平行视界(windowMode=103 下 isInMultiWindowMode 可能为 false)
116+
val isSubWindow = info.inMultiWindow ||
117+
(realShort > 0 && shortPx < realShort * 0.85f) ||
118+
(realLong > 0 && longPx < realLong * 0.85f)
122119

123-
// 5. 普通手机横屏:按真实短边适配,保持与竖屏一致的控件/字号尺度
124-
info.configurationOrientation == Configuration.ORIENTATION_LANDSCAPE ->
125-
AdaptDirective.ShortSide(360f, useRealDisplay = true)
120+
val swDp = activity.resources.configuration.smallestScreenWidthDp
121+
val nativeDensity = ScreenAdapt.getNativeDensity(activity)
122+
val isTabletDevice = swDp >= 600 && (realLong.toFloat() / realShort.coerceAtLeast(1)) > 1.2f
126123

127-
// 6. 其余普通手机竖屏:回退全局 designSize(360f, 640f)
128-
else -> null
124+
when {
125+
// 1. 近似方形折叠屏展开态(如 Mate X5/X6 内屏):扩大设计宽度,避免 375dp 过度放大
126+
!isSubWindow && w > 1500 && ratio in 0.85f..1.15f ->
127+
AdaptDirective.Width(750f)
128+
129+
// 2. 平板 / 三折叠全屏(排除平行视界与小窗):走 750dp 或直接返回 AdaptDirective.Native
130+
!isSubWindow && (
131+
(isTabletDevice && shortPx >= 1500) ||
132+
(shortPx >= 1500 && nativeDensity in 0.1f..2.5f) ||
133+
shortPx >= 2000
134+
) -> AdaptDirective.ShortSide(750f, useRealDisplay = false)
135+
136+
// 3. 高密阔折叠内屏展开态(如 7~8 英寸高 PPI 内屏):按短边 600dp,避免误落入平板档导致字号骤缩
137+
!isSubWindow && shortPx >= 1500 ->
138+
AdaptDirective.ShortSide(600f, useRealDisplay = false)
139+
140+
// 4. 直板手机(竖/横屏)、阔折叠小外屏、平行视界与分屏小窗:375dp 基准 + 短屏保护(最高放宽至 420dp)
141+
else -> {
142+
val effectiveShortDp = ((shortPx.toFloat() / longPx.toFloat()) * 667f)
143+
.coerceIn(375f, 420f)
144+
AdaptDirective.ShortSide(effectiveShortDp, useRealDisplay = !isSubWindow)
145+
}
129146
}
130147
}
131148
}
132149
```
133150

134151
> **提示**:
135-
> - 以上阈值(`0.8f`、`600dp`、`450dp`、`720f`/`420f`)仅为参考,请结合自身 UI 设计稿调整;若希望平板整机完全不启用适配,也可在 `Application#onCreate` 判断条件后直接跳过 `ScreenAdapt.init(...)`(`ScreenMetrics` 仍可安全调用)。
136-
> - 若希望折叠屏展开/折叠或横竖屏切换时 **不重建 `Activity`**,在 `AndroidManifest.xml` 中为对应页面配置 `android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout"`,引擎会在配置与布局变更时自动按新窗口重算密度。
152+
> - 以上分档阈值(`375f / 420f / 600f / 750f`、`667f`、`1500px`、`2.5f`)仅为参考,请结合自身 UI 设计稿调整;如果希望平板(含平行视界)统一走原生响应式布局,直接对平板分支返回 `AdaptDirective.Native`,或在 `Application#onCreate` 跳过 `ScreenAdapt.init(...)`(`ScreenMetrics` 仍可安全调用)。
153+
> - **取消/退出适配请务必返回 `AdaptDirective.Native`,不要返回 `AdaptDirective.Skip`**:`Skip` 会保留上一次已写入的密度;在不重建 `Activity` 旋转进入平行视界或横屏时,返回 `Skip` 会把竖屏密度残留到新窗口中造成裁切。
154+
> - 若希望折叠屏展开/折叠、平行视界切换或横竖屏旋转时 **不重建 `Activity`**,在 `AndroidManifest.xml` 中为对应页面配置 `android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout"`,引擎会在配置与布局变更时自动按新窗口重算密度。
137155
138156
完整接口签名、配置项、指令模型与 `ScreenMetrics` 工具说明见 [API 文档](docs/API.md)。
139157

‎docs/API.en.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ object ScreenAdapt {
6060
@JvmStatic
6161
fun getWindowInfo(activity: Activity): WindowInfo?
6262

63+
/** Returns the unadapted native display density for a Context (useful in AdaptStrategy to distinguish tablets from high-PPI foldables). */
64+
@JvmStatic
65+
fun getNativeDensity(context: Context): Float
66+
6367
@JvmStatic
6468
fun addListener(activity: Activity, listener: AdaptEventListener)
6569

@@ -145,13 +149,13 @@ Semantics and common use cases for each `AdaptDirective`:
145149
| :--- | :--- | :--- |
146150
| `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 |
147151
| `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 or when 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 or when either window short side or long side is `< 85%` of the physical display (e.g., OEM MagicWindow / freeform)** | Landscape pages (keeps visual scale consistent with portrait), large screens, MagicWindow, and split-screen compatibility |
149153
| `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 |
151155
152156
> **`AdaptStrategy` & Multi-Form-Factor Dynamic Switching**
153157
>
154-
> - 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).
155159
> - 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`.
156160
> - Returning `null` from `AdaptStrategy` falls back to the global `designWidthDp` / `designHeightDp` and `basis`.
157161

0 commit comments

Comments
 (0)