我正试图从我的Android手机上获取我的传感器的倾斜角度,滚动角度,但到目前为止没有成功,
当我点击我应该给我3个角度的按钮时,我得到"结果:0.0 -0.0 -0.0"
package com.example;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class MyActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final float[] mValuesMagnet = new float[3];
final float[] mValuesAccel = new float[3];
final float[] mValuesOrientation = new float[3];
final float[] mRotationMatrix = new float[9];
final Button btn_valider = (Button) findViewById(R.id.btn1);
final TextView txt1 …Run Code Online (Sandbox Code Playgroud) 假设您在所有3个维度(即X,Y和Z)中都有加速度读数.您如何使用读数来推断手机是向左还是向右倾斜?读数每20ms生成一次.
我实际上想要从读数推断出倾斜的逻辑.倾斜需要平稳.
我在很多地方读到过:一个屏幕值得另一个:“API 用于设备自然方向的传感器坐标系不会随着设备移动而改变,并且与 OpenGL 坐标系相同。”
现在,我得到与此图像相同的读数: 
我不明白的是:如果我旋转手机(始终使屏幕面向用户)坐标系没有改变,则重力应始终施加在 Y 轴上。只有当我将手机放在屏幕不再面向用户的位置时,它才应该改变轴,就像放在桌子上一样,重力应该施加在 Z 轴上。
我的理解有什么问题?
谢谢!吉列尔莫。
在我的项目中,我试图使用通过Android设备的加速度计获得的数据来控制汽车.(左,右,前,后).即使我设法从加速度计读取值,即使设备处于稳定位置,读数也会经常变化.有人能为我提供更好的方法吗?
以下是我使用过的代码
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class AccelerometerService {
private static SensorManager sensorManager;
private static SensorEventListener sensorEventListener;
private static boolean started = false;
private static float[] accelerometer = new float[3];
private static float[] magneticField = new float[3];
private static float[] rotationMatrix = new float[9];
private static float[] inclinationMatrix = new float[9];
private static float[] attitude = new float[3];
private final static double RAD2DEG = 180/Math.PI;
private static int initialAzimuth = 0;
private static …Run Code Online (Sandbox Code Playgroud)