我知道之前有人问过,但我找不到我要找的答案.
我想做的就是把手机放在天空,知道我相对于地平线的角度.所以,如果我把它直接放在我上面(屏幕朝向地面)那么它应该读取90度,如果我把它放在我面前(屏幕面向我),它应该读取0度.
我已经摸索了从加速度计获取数据的一些基础知识,我相信我有X,Y和Y值.我怎样才能拿出这些点并找到我的角度?下面是我收集坐标的OnSensorChanged事件.
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
playerAngle = (float) Math.toDegrees(Math.atan2(orientation[1], orientation[0]));
}
Run Code Online (Sandbox Code Playgroud)