这几行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 …
我需要获得更新的设备方向,但我必须将我的活动修复为Portrait(我在布局xml文件中做了),这阻止我使用它:
int rotation = getWindowManager().getDefaultDisplay().getRotation();
Run Code Online (Sandbox Code Playgroud)
因为它总是给我画像旋转,
所以,我试图依靠传感器.我发现它Sensor.TYPE_ORIENTATION已被弃用,所以我使用Sensor.TYPE_ACCELEROMETER&的组合,Sensor.TYPE_MAGNETIC_FIELD这里是事件监听器:
SensorEventListener sensorEventListener = new SensorEventListener() {
float[] mGravity;
float[] mGeomagnetic;
@Override
public void onSensorChanged(SensorEvent event) {
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 orientationData[] = new float[3];
SensorManager.getOrientation(R, orientationData);
azimuth …Run Code Online (Sandbox Code Playgroud) 我无法获得良好的方向传感器读数.传感器读数似乎不可靠,因此我针对两个免费传感器测试应用程序(传感器测试仪(Dicotomica)和传感器监测(R软件))测试了我的代码.我发现虽然我的读数经常与传感器测试应用程序一致,但偶尔方位角/偏航和滚动的值差异高达40度,尽管音高读数大多是一致的.两个免费的应用程序似乎总是彼此一致.
我将我的代码放入一个微小的Android活动中,并得到了同样的不一致.代码如下:
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private float[] AccelerometerValues;
private float[] MagneticFieldValues;
private float[] RotationMatrix;
private long nextRefreshTime; // used to ensure dump to LogCat occurs no more than 4 times a second
private DecimalFormat df; // used for dumping sensors to LogCat
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager)getSystemService(android.content.Context.SENSOR_SERVICE);
Sensor SensorAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, SensorAccelerometer, SensorManager.SENSOR_DELAY_UI);
Sensor SensorMagField = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
mSensorManager.registerListener(this, SensorMagField, SensorManager.SENSOR_DELAY_UI);
AccelerometerValues …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用TYPE_ACCELEROMETER传感器获取电话角度。我的目标是仅在手机倾斜后才能获得角度值。可以,但是问题是当我将手机正面朝上放在桌子上时,仍然说isLandscape = true;
private boolean isLandscape;
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(
Sensor.TYPE_ACCELEROMETER),1000000);
private final SensorEventListener mSensorListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent mSensorEvent) {
float X_Axis = mSensorEvent.values[0];
float Y_Axis = mSensorEvent.values[1];
double angle = Math.atan2(X_Axis, Y_Axis)/(Math.PI/180);
if(!isLandscape) {
if(angle > 80) {
Orientation = 90;
isLandscape = true;
}
}
else
{
if(Math.abs(angle) < 10) {
Orientation = 0; //portrait
isLandscape = false;
}
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
Run Code Online (Sandbox Code Playgroud)
仅在手机倾斜后才能获得手机角度的最佳方法是什么?对不起,英语不好,
谢谢。
我确信这里已经多次询问过这个问题但是,互联网上是否有关于陀螺仪Android开发的例子.因为陀螺仪引起了我的兴趣,我认为它可以带来很好的效果.我只是猜测三星Galaxy S有一个陀螺仪,对吧?希望如此.虽然,我的主要需求是一个教程,甚至可能是一个很好的android编程教程/书(不仅仅是陀螺仪相关).我只是为了Android开发而学习Java.我已经知道C和一点OBJ-C所以学习Java花了几周时间.所以,再一次有人知道Android开发的任何非常好的教程/书籍(我发现了一些关于你管的教程,但它们的质量非常糟糕,我无法遵循),也许是一个很好的陀螺仪教程,适用于Android和内置陀螺仪的Android手机列表.