Android 使用 getOrientation() 函数获取设备角度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20339942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get device angle by using getOrientation() function
提问by fobus
I was using Sensor.TYPE_ORIENTATION
to determine current angle of device but TYPE_ORIENTATION
is deprecated on API version 8. In SensorManager manual it refers to getOrientation()
function in order to use TYPE_ORIENTATION
.
我Sensor.TYPE_ORIENTATION
用于确定设备的当前角度,但TYPE_ORIENTATION
在API 版本 8上已弃用。在 SensorManager 手册中,它指的是getOrientation()
功能以便使用TYPE_ORIENTATION
.
Here is my old code :
这是我的旧代码:
public void onSensorChanged(SensorEvent event) {
Log.d("debug","Sensor Changed");
if (event.sensor.getType()==Sensor.TYPE_ORIENTATION) {
Log.d("debug",Float.toString(event.values[0]));
float mAzimuth = event.values[0];
float mPitch = event.values[1];
float mRoll = event.values[2];
Log.d("debug","mAzimuth :"+Float.toString(mAzimuth));
Log.d("debug","mPitch :"+Float.toString(mPitch));
Log.d("debug","mRoll :"+Float.toString(mRoll));
}
}
I'm really confused about using getOrientation()
function, can anyone please show me an example how to get the angles?
我对使用getOrientation()
函数真的很困惑,谁能给我看一个如何获得角度的例子?
回答by JRomero
You now use two sensors (ACCELEROMETER and MAGNETIC_FIELD) to get that information. See blog post for more detail.
您现在使用两个传感器(ACCELEROMETER 和 MAGNETIC_FIELD)来获取该信息。有关更多详细信息,请参阅博客文章。
public class CompassActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
Sensor accelerometer;
Sensor magnetometer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(mCustomDrawableView); // Register the sensor listeners
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
float[] mGravity;
float[] mGeomagnetic;
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 orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
azimut = orientation[0]; // orientation contains: azimut, pitch and roll
}
}
}
}
Permissions:
权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
回答by Shaheed
Regarding your second question. When you are registering your sensor listeners, change your code to read:
关于你的第二个问题。当您注册传感器侦听器时,请将代码更改为:
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
}
回答by JohnnyLambada
Google has a great demo app for orientation in their google-developer-training series called TiltSpot. Because it has an Apache license, I've taken the liberty of turning it into a small library called johnnylambada-orientationthat makes getting orientation as simple adding this to your activity:
谷歌在其 google-developer-training 系列中有一个很棒的演示应用程序,称为TiltSpot。因为它有一个 Apache 许可证,我冒昧地把它变成了一个名为johnnylambada-orientation的小库,它使获取方向变得简单,将它添加到您的活动中:
getLifecycle().addObserver(new OrientationReporter(this, (a, p, r) -> {
Log.i("orientation","a="+a+" p="+p+" r="+r);
}));