在Android Studio中将代码从Java转换为Kotlin

任何人都可以解释如何用Android Studio中的Kotlin替换现有的Java代码?

如果您已经在您的Android Studio项目中配置了Kotlin,则可以按照此处列出的步骤自动转换此处的说明。

来自网站:

  • 在主菜单上,指向代码菜单。
  • 选择转换Java文件到Kotlin文件。
 fun setCameraDisplayOrientation(activity: Activity, cameraId: Int, camera: android.hardware.Camera) { val info = android.hardware.Camera.CameraInfo() android.hardware.Camera.getCameraInfo(cameraId, info) val rotation = activity.windowManager.defaultDisplay .rotation var degrees = 0 when (rotation) { Surface.ROTATION_0 -> degrees = 0 Surface.ROTATION_90 -> degrees = 90 Surface.ROTATION_180 -> degrees = 180 Surface.ROTATION_270 -> degrees = 270 } var result: Int if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { result = (info.orientation + degrees) % 360 result = (360 - result) % 360 // compensate the mirror } else { // back-facing result = (info.orientation - degrees + 360) % 360 } camera.setDisplayOrientation(result) }