android - getDisplay() 返回显示
问题描述
我知道这是一个非常愚蠢的问题或初学者问题,但我试图替换它,windowManager.defaultDisplay.orientation
因为它已被弃用,它建议我这样做。我遇到的问题是我看到了这个return
值Display
,这让我很困惑,我不知道或者期望我将什么作为返回值。有人可以帮忙吗,我知道这并不多,但希望你能理解我花了 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!
}
解决方案
尝试这个 :
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!
}
希望这可以帮助!
推荐阅读
- sql-server - 简化 SQL 删除查询
- android - Android Lombok 1.18.0 插件在发布构建期间导致 IllegalArgumentException 错误
- python - 来自pyinstaller可执行文件的.so上的Python unicode错误
- machine-learning - 如何减少大型随机森林模型 (sklearn) 的内存占用?
- spring-boot - void方法中的Spring Reactive
- xamarin.forms - Prism 版本 7 发行说明 (Xamarin.Forms)
- python - 如何使用 Django-Rest-Framework 返回单个对象
- android - 如果返回的 JSON 变量有 @ 符号,如何在 Android 中定义变量?
- java - 如何从字符串中获取类类型
- php - 如何按旧日期获取多个表的行(逐行)