首页 > 解决方案 > getDisplay() 返回显示

问题描述

我知道这是一个非常愚蠢的问题或初学者问题,但我试图替换它,windowManager.defaultDisplay.orientation因为它已被弃用,它建议我这样。我遇到的问题是我看到了这个returnDisplay,这让我很困惑,我不知道或者期望我将什么作为返回值。有人可以帮忙吗,我知道这并不多,但希望你能理解我花了 25 分钟试图弄清楚。如果有人不明白这个问题或有其他问题,请告诉我。先感谢您。

编码:

private fun rotateBitmap(source: Bitmap, angle: Float): Bitmap {
        val matrix = Matrix()
        matrix.postRotate(angle)
        return Bitmap.createBitmap(source, 0, 0, source.width, source.height, matrix, true)
    }

fun getDisplay(displayID: Int): Display {
        when (displayID) {
            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)
            }
        }
        
        return //The problem is right here, don't know what to return on Display!
    }

标签: androidkotlin

解决方案


尝试这个 :

fun getDisplay(displayID: Int): Display {
    when (displayID) {
        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)
        }
    }
    val displayManager: DisplayManager =
        applicationContext.getSystemService<Any>(Context.DISPLAY_SERVICE) as DisplayManager
    return displayManager.getDisplay(displayID);
    //This should work!
}

希望这可以帮助!


推荐阅读