gpt4 book ai didi

java - TYPE_ROTATION_VECTOR 不适用于某些设备

转载 作者:行者123 更新时间:2023-11-30 03:33:44 28 4
gpt4 key购买 nike

我制作了一个使用 TYPE_ROTATION_VECTOR 获取手机方向的程序。该程序适用于某些三星 galaxy s 手机,但不适用于 sony xperia neo v。所有手机都有 api >= 9 的 android 版本。可能是什么问题?

package com.example.sensortest;

import android.annotation.TargetApi;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.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);
}

最佳答案

您必须检查 mRotVectSensor 是否为空值。对于 Sensor.TYPE_ROTATION_VECTOR,设备必须有一个陀螺仪

关于java - TYPE_ROTATION_VECTOR 不适用于某些设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16978372/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com