You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检测Android设备屏幕圆角及圆角半径?OpenGL游戏UI适配需求

判断Android设备屏幕圆角并获取半径的方案

嗨,针对你的OpenGL全屏游戏场景,我整理了几个靠谱的方法来检测屏幕圆角并获取半径,帮你调整UI位置避免被遮挡:

1. 利用Android官方API(Android 12+)

从Android 12(API 31)开始,系统提供了直接获取屏幕圆角信息的官方API,这是最准确的方式,优先推荐:

  • 使用WindowInsets.getRoundedCorner(int position)方法,position参数可以指定四个角的位置:RoundedCorner.POSITION_TOP_LEFT、POSITION_TOP_RIGHT、POSITION_BOTTOM_LEFT、POSITION_BOTTOM_RIGHT
  • 示例代码(Kotlin):
// 在你的OpenGL视图或Activity中设置Insets监听
yourGLView.setOnApplyWindowInsetsListener { v, insets ->
    // 获取左上角圆角信息
    val topLeftCorner = insets.getRoundedCorner(RoundedCorner.POSITION_TOP_LEFT)
    // 半径单位为像素px,无圆角时返回0
    val topLeftRadius = topLeftCorner?.radius ?: 0

    // 同理可获取其他三个角的半径
    val topRightRadius = insets.getRoundedCorner(RoundedCorner.POSITION_TOP_RIGHT)?.radius ?: 0
    // ...

    // 记得返回Insets,不影响其他Insets相关逻辑
    insets
}

提示:如果设备没有圆角,getRoundedCorner()会返回null,这时候直接取0即可

2. 兼容低版本设备的自定义检测方案

如果你的游戏需要支持Android 12以下的设备,就得用自定义方法来推断圆角了,思路是通过对比屏幕真实尺寸和应用可显示区域的差值来估算:

  • 步骤示例(Kotlin):
    1. 获取屏幕真实物理分辨率(包含被圆角/刘海遮挡的区域):
      val display = context.display
      val realScreenSize = Point()
      display?.getRealSize(realScreenSize)
      val realWidth = realScreenSize.x
      val realHeight = realScreenSize.y
      
    2. 获取应用窗口的可显示区域(排除系统UI和圆角遮挡的部分):
      val window = activity.window
      val windowInsets = window.decorView.rootWindowInsets
      val usableWidth = realWidth - windowInsets.systemWindowInsetLeft - windowInsets.systemWindowInsetRight
      val usableHeight = realHeight - windowInsets.systemWindowInsetTop - windowInsets.systemWindowInsetBottom
      
    3. 估算圆角半径:假设圆角是标准的四分之一圆,用差值的最小值作为半径:
      val horizontalGap = (realWidth - usableWidth) / 2
      val verticalGap = (realHeight - usableHeight) / 2
      val estimatedCornerRadius = if (horizontalGap > 0 && verticalGap > 0) {
          Math.min(horizontalGap, verticalGap)
      } else {
          0
      }
      
    注意:这种方法是估算,精度不如官方API,但能覆盖大部分低版本设备的需求

3. OpenGL视图中的适配细节

因为你的游戏是单全屏OpenGL视图,还要注意这些适配点:

  • 将获取到的像素单位圆角半径,转换成OpenGL的归一化设备坐标(比如根据屏幕宽高比进行换算)
  • 在渲染UI元素时,给边缘元素留出至少等于圆角半径的安全距离,或者直接根据圆角区域调整UI的布局边界
  • 调试阶段可以在OpenGL中绘制一个和屏幕圆角匹配的半透明遮罩层,直观检查UI是否被遮挡

内容的提问来源于stack exchange,提问作者michalsrb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:50:36