API演示 - >图形 - > 指南针
它只能正常工作,直到您不更改设备的自然方向.在大多数手机中,肖像和大多数10英寸平板电脑是风景.如果你改变而不是需要旋转90度.我想看看该系统的3D修复.
100%肯定需要使用remapCoordinateSystem()方法.
我想看看如何(代码)如果我能看到如何计算那些轴映射(理论数学)的解释它会很好.
我试着理解,但我忘记了所有的线性代数.
这里说明为什么我们必须使用,但不告诉如何!
float R[] = new float[9];
// X (product of Y and Z) and roughly points East
// Y: points to Magnetic NORTH and tangential to ground
// Z: points to SKY and perpendicular to ground
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
Run Code Online (Sandbox Code Playgroud)
似乎那些坐标是这个位置: - 设备在表格中说(x,y轴在桌子上)

只有,如果
getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_0
Run Code Online (Sandbox Code Playgroud)
问题是如何完成此代码: - 那些案例分支
switch (mScreenRotation) {
case Surface.ROTATION_0:
Log.v("SurfaceRemap", "0 degree");
axisX …Run Code Online (Sandbox Code Playgroud) 有两种方法可以获得3个旋转值(方位角,俯仰,滚动).
一个是注册TYPE_ORIENTATION类型的监听器.这是最简单的方法,我从每次旋转中得到正确的值范围,如文档所示:azimuth:[0,359] pitch:[ - 180,180] roll:[ - 90,90]
另一个,你第一次看到它时最精确和最复杂.Android推荐它,所以我想使用它,但我得到不同的值.
方位角:[ - 180,180].-180/180是S,0 i N,90 E和-90 W.
pitch:[ - 90,90 ].90为90,-90为-90,0为0但-180/180(屏幕向下)为0.
roll:[ - 180,180].
我应该得到相同的值,但有小数,对吧?
我有以下代码:
aValues = new float[3];
mValues = new float[3];
sensorListener = new SensorEventListener (){
public void onSensorChanged (SensorEvent event){
switch (event.sensor.getType ()){
case Sensor.TYPE_ACCELEROMETER:
aValues = event.values.clone ();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
mValues = event.values.clone ();
break;
}
float[] R = new float[16];
float[] orientationValues = new float[3];
SensorManager.getRotationMatrix (R, null, aValues, mValues);
SensorManager.getOrientation (R, orientationValues);
orientationValues[0] …Run Code Online (Sandbox Code Playgroud) 我一直在努力使用getOrientation获取设备的实际方向.通过TYPE_ACCELEROMETER和TYPE_MAGNETIC_FIELD获取加速度计和磁场读数非常简单,通过TYPE_ORIENTATION获取方向也是如此.但是使用getOrientation根据世界坐标系获取方向.这里和这里有很多很好的教程,但我还没有把它们放在一起.为此,我正在开发一个应用程序,它只是从getOrientation中吐出Accelerometer,Magnetic Field,原始Orientation数据和Orientation数据.我已经附加了代码,但它在我的手机上经常出现故障.有什么建议?
package com.sensorall;
import com.sensorall.R;
import android.hardware.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class OrientationAccelerometer extends Activity {
SensorManager sensorManager;
float[] mGravs = new float[3];
float[] mGeoMags = new float[3];
float[] mRotationM = new float[16];
float[] mInclinationM = new float[16];
float[] mOrientation = new float[3];
float[] mOldOreintation = new float[3];
String[] mAccelerometer = new String[3];
String[] mMagnetic = new String[3];
String[] mRotation = new String[16];
String[] mInclination = new String[16];
String[] mOrientationString = new …Run Code Online (Sandbox Code Playgroud) 这几行C#代码使用加速度计计算手机围绕y轴的旋转:
private float GetRoll() {
/*
* Sum up the accelerometer events
*/
Vector3 accelerationVector = Vector3.zero;
for (int i=0; i < Input.accelerationEventCount; i++) {
AccelerationEvent accEvent = Input.GetAccelerationEvent(i);
accelerationVector += accEvent.acceleration * accEvent.deltaTime;
}
accelerationVector.Normalize();
int inclination = (int) Mathf.Round(Mathf.Rad2Deg * Mathf.Acos(accelerationVector.z));
float roll = 0;
if (inclination < 25 || inclination > 155) {
/*
* How to calculate the rotation here?
*/
} else {
roll = Mathf.Round(Mathf.Rad2Deg * Mathf.Atan2(accelerationVector.x, accelerationVector.y));
}
return roll;
}
Run Code Online (Sandbox Code Playgroud)
如果手机平放在桌子上,你知道数学运算吗?即如果inclination …
我目前正在尝试替换我的'Sensor.TYPE_ORIENTATION',因为它已被弃用.所以Android文档缺少大部分信息,关于如何做到这一点.虽然调试和陷在这里SO我想通了,如何计算azimuth其曾经被OrientationSensor提供.我是这样做的:
float accelerometerVals[] = new float[3];
float magneticVals[] = new float[3];
float orientationSensorVals[] = new float[3];
float rotationMatrix[] = new float[16];
float orientation[] = new float[3];
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, accelerometerVals, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, magneticVals, 0, 3);
break;
case Sensor.TYPE_ORIENTATION:
System.arraycopy(event.values, 0, orientationSensorVals, 0, 3);
break;
default:
break;
}
if (null != magneticVals && null != accelerometerVals){
SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerVals, magneticVals) …Run Code Online (Sandbox Code Playgroud)