java TYPE_ROTATION_VECTOR在某些设备上不起作用
我制作了一个程序,使用类型_旋转_向量来获得手机的方向。。该程序适用于某些三星galaxy s手机,但不适用于索尼xperia neo v。。所有手机都有带api的安卓版本>;=9.. 有什么问题吗
package com.example.sensortest;
import 安卓.annotation.TargetApi;
import 安卓.app.Activity;
import 安卓.hardware.Sensor;
import 安卓.hardware.SensorEvent;
import 安卓.hardware.SensorEventListener;
import 安卓.hardware.SensorManager;
import 安卓.os.Build;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener{
TextView t1,t2,t3;
private SensorManager mSensorManager;
private Sensor mRotVectSensor;
private float[] orientationVals=new float[3];
private float[] mRotationMatrix=new float[16];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView)findViewById(R.id.TextView1);
t2=(TextView)findViewById(R.id.TextView2);
t3=(TextView)findViewById(R.id.TextView3);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mRotVectSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType()==Sensor.TYPE_ROTATION_VECTOR)
{
SensorManager.getRotationMatrixFromVector(mRotationMatrix,event.values);
SensorManager.remapCoordinateSystem(mRotationMatrix,SensorManager.AXIS_X, SensorManager.AXIS_Z, mRotationMatrix);
SensorManager.getOrientation(mRotationMatrix, orientationVals);
orientationVals[0]=(float)Math.toDegrees(orientationVals[0]);
orientationVals[1]=(float)Math.toDegrees(orientationVals[1]);
orientationVals[2]=(float)Math.toDegrees(orientationVals[2]);
t1.setText(String.valueOf(orientationVals[0]));
t2.setText(String.valueOf(orientationVals[1]));
t3.setText(String.valueOf(orientationVals[2]));
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and
// accelerometer sensors
mSensorManager.registerListener(this,
mRotVectSensor,
10000);
}
@Override
protected void onPause() {
// unregister listener
super.onPause();
mSensorManager.unregisterListener(this);
}
}
# 1 楼答案
我可能无法解决您的问题,但请确保您在SensorManager中使用了不同的矩阵。remapCoordinateSystem(见Documentation)。上面写着:(参数)outR——变换后的旋转矩阵。inR和outR不应该是同一个数组