我正在试验一款相机应用程序,它可以向您显示手机的倾斜角度,以帮助您调平手机。
我想做的是类似于谷歌的相机应用程序,它会显示倾斜角度和倾角:
我有以下使用Sensor.TYPE_ROTATION_VECTOR的实现,但我遇到的问题是角度会根据手机的起始位置/角度而变化。我想要一个一致的角度测量值,当手机直立时显示 90%。
class MainActivity : Activity(), SensorEventListener {
private var mSensorManager: SensorManager? = null
private var mRotationVectorSensor: Sensor? = null
private val rotationMatrix = FloatArray(9)
private val orientationAngles = FloatArray(3)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Get an instance of the SensorManager
mSensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
mRotationVectorSensor = mSensorManager!!.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)
mSensorManager!!.registerListener(
this, mRotationVectorSensor, SensorManager.SENSOR_DELAY_NORMAL
)
}
override fun onSensorChanged(event: SensorEvent) {
if (event.sensor.type == Sensor.TYPE_ROTATION_VECTOR) {
// Convert the rotation-vector to a 3x3 matrix.
SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values)
SensorManager.getOrientation(rotationMatrix, orientationAngles)
val inclination = SensorManager.getInclination(rotationMatrix)
val degree = Math.toDegrees(inclination.toDouble())
Log.d("++tiltAngleDegrees,", degree.roundToInt().toString())
}
}
}
我也尝试过使用 remapCoordinateSystem,但这并没有解决问题:
SensorManager.remapCoordinateSystem(
rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, rotationMatrix
)
Android SensorManager strange how to remapCoordinateSystem 是一个类似的 StackOverflow 帖子,但它是关于倾斜或方向角