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

Android替换废弃windowManager.defaultDisplay.orientation时getDisplay返回值疑问

问题根源

你对官方的废弃接口替换方案存在误解:

  • 官方要求你调用系统android.hardware.display.DisplayManager类自带的getDisplay(int displayId)方法获取Display实例,不需要你自行实现返回Display类型的方法
  • 你当前自定义的getDisplay方法和官方要求的实现逻辑完全不符,把「获取系统旋转参数」和「旋转Bitmap」的逻辑混写在了一起
正确实现方案

你已经实现的rotateBitmap方法无需修改,直接调用系统DisplayManager的相关接口获取旋转参数即可,示例代码如下:

// 示例:在Activity中获取旋转角度后处理图片
private fun handleRotatedPhoto() {
    // 获取系统DisplayManager服务
    val displayManager = getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
    // 调用系统getDisplay方法获取主屏幕Display实例
    val defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY)
    // 从Display实例获取当前旋转角度
    val rotation = defaultDisplay.rotation
    // 按角度旋转图片
    val targetBitmap = when(rotation) {
        Surface.ROTATION_0 -> rotateBitmap(BitmapFactory.decodeFile(photoFile.absolutePath), 0f)
        Surface.ROTATION_90 -> rotateBitmap(BitmapFactory.decodeFile(photoFile.absolutePath), 90f)
        Surface.ROTATION_180 -> rotateBitmap(BitmapFactory.decodeFile(photoFile.absolutePath), 180f)
        Surface.ROTATION_270 -> rotateBitmap(BitmapFactory.decodeFile(photoFile.absolutePath), 270f)
        else -> rotateBitmap(BitmapFactory.decodeFile(photoFile.absolutePath), 0f)
    }
    // 后续直接使用targetBitmap即可
}

如果是在非Activity的类中使用,传入对应Context实例获取服务即可:

val displayManager = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 18:36:02