Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 陀螺仪:旋转差分_Java_Android_Gyroscope - Fatal编程技术网

Java 陀螺仪:旋转差分

Java 陀螺仪:旋转差分,java,android,gyroscope,Java,Android,Gyroscope,我怎样才能知道设备旋转了多少? 我的意思是,如果我从一个初始位置开始,然后旋转90度,我怎么能让这个代码告诉我我已经旋转了90度?很抱歉这个问题太草率了。这是我的密码: import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardwar

我怎样才能知道设备旋转了多少? 我的意思是,如果我从一个初始位置开始,然后旋转90度,我怎么能让这个代码告诉我我已经旋转了90度?很抱歉这个问题太草率了。这是我的密码:

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;

public class AccessGyroscope extends Activity implements SensorEventListener
{
//a TextView
private TextView tv;
//the Sensor Manager
private SensorManager sManager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //get the TextView from the layout file
    tv = (TextView) findViewById(R.id.tv);

    //get a hook to the sensor service
    sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}

//when this Activity starts
@Override
protected void onResume() 
{
    super.onResume();
    /*register the sensor listener to listen to the gyroscope sensor, use the 
     * callbacks defined in this class, and gather the sensor information as  
     * quick as possible*/
    sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);
}

//When this Activity isn't visible anymore
@Override
protected void onStop() 
{
    //unregister the sensor listener
    sManager.unregisterListener(this);
    super.onStop();
}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) 
{
    //Do nothing
}

@Override
public void onSensorChanged(SensorEvent event) 
{
    //if sensor is unreliable, return void
    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
    {
        return;
    }

    //else it will output the Roll, Pitch and Yawn values
    tv.setText("Orientation Z (Yaw) :"+ Float.toString(event.values[0]));
}
}

类型_方向传感器已弃用,请勿使用,因为它工作不好且计算量大

使用[getRotationMatrix][1]传入加速度计和磁场读数,然后在矩阵上使用getRotation()。 这几乎是开发人员参考中的逐字记录:


[1] :,float[],float[],float[])“getRotationMatrix()”

最简单的解决方案有时很难找到


我添加了3个按钮:1个用于捕捉初始位置,1个用于捕捉最终位置,1个用于减去两者

谢谢你花时间回答。我对在安卓系统中使用传感器还比较陌生,所以如果你能给我一个示例代码来指导我正确的方向,那就可以帮我解决问题了!谢谢