确定应用是否跨屏

跨两个屏幕显示单个应用时,将发生跨屏布局。 默认情况下,如果应用未针对此场景进行自定义,系统将通知应用,它现在占据了更大的屏幕宽度和高度,然后应用的 UI 将调整自身大小以适应新的屏幕尺寸。

有关详细信息,请参阅“双屏设备简介” 中的如何处理接缝

使用此代码确定应用是否跨越两个屏幕。

重要

Surface Duo SDK 中的 API 与 Surface Duo 设备交互,当应用在其他设备上运行时不应调用这些 API。 在调用这些 API 之前,应检查应用是否在 Surface Duo 设备上运行。 使用 isDeviceSurfaceDuo 代码片段执行此检查。

此方法集合可用于创建 isAppSpanned 检查:

fun getCurrentRotation(activity: Activity): Int {
    return try {
        val wm = activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        wm.defaultDisplay.rotation
    } catch (e: IllegalStateException) { Surface.ROTATION_0 }
}
fun getHinge(activity: Activity): Rect? {
    // Hinge's coordinates of its 4 edges in different mode
    // Double Landscape Rect(0, 1350 - 1800, 1434)
    // Double Portrait  Rect(1350, 0 - 1434, 1800)
    return if (isDeviceSurfaceDuo()) {
        val displayMask = DisplayMask.fromResourcesRectApproximation(activity)
        if (displayMask != null) {
            val screensBounding = displayMask.getBoundingRectsForRotation(
                    getCurrentRotation(activity)
            )
            if (screensBounding.size == 0) {
                Rect(0, 0, 0, 0)
            } else {
                screensBounding[0]
            }
        } else { null }
    } else { null }
}
fun getWindowRect(activity: Activity): Rect {
    val windowRect = Rect()
    activity.windowManager.defaultDisplay.getRectSize(windowRect)
    return windowRect
}
fun isAppSpanned(activity: Activity): Boolean {
    val hinge = getHinge(activity)
    val windowRect = getWindowRect(activity)

    return if (hinge != null && windowRect.width() > 0 && windowRect.height() > 0) {
        // The windowRect doesn't intersect hinge
        hinge.intersect(windowRect)
    } else {
        false
    }
}

使用 getHinge 方法可检索铰链掩码位置的界限。

另请参阅