我已经实现了一个只能在纵向模式下工作的应用程序。该应用程序有一个指南针,我们知道samsungs平板电脑中的指南针(例如:三星galaxy tab 2)和移动设备的工作方式不同,其中有一些是在景观中的NORTH(0º),另一些在纵向中。
我们需要全部处于肖像模式,换句话说,我们想要肖像中的NORTH(0º)。有人有解决方案吗?
答案 0 :(得分:0)
我在Galaxy Note上遇到了同样的问题,并遵循了如何确定平板电脑或手机的建议。这是另一个帖子,但不记得链接。代码如下所示:
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
double inches = Math.sqrt((metrics.widthPixels * metrics.widthPixels)
+ (metrics.heightPixels * metrics.heightPixels))
/ metrics.densityDpi;
// tablets typically larger than 6"" diagonally
// this is attempt to orient both player and magnetic sensors
if (inches > 6) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
执行此操作后,我在sensorlistener代码中翻译了方位角设置:
public void onSensorChanged(SensorEvent event) {
// Note: Not all sensor events will supply both accelerometer and
// mag-field
int s = event.sensor.getType();
switch (s) {
case Sensor.TYPE_ACCELEROMETER:
aValues = lowPass(event.values.clone(), aValues);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
mValues = lowPass(event.values.clone(), mValues);
break;
}
float[] R = new float[16]; // Rotation Matrix result goes here
float[] I = new float[9]; // Inclination Matrix result goes here
float[] oValues = new float[3]; // used for new orientation
boolean success = SensorManager.getRotationMatrix(R, I, aValues,
mValues);
// if both aValues and mValues are not null success will be true
// Also returns the Inclination Matrix
if (success) {
// Now get the device's orientation from the Rotation and
// Inclination Matrices
SensorManager.getOrientation(R, oValues);
// Change Radians to Degrees
oValues[0] = (360 + (float) Math.toDegrees(oValues[0])) % 360;
oValues[1] = (float) Math.toDegrees(oValues[1]);
oValues[2] = (float) Math.toDegrees(oValues[2]);
azimuth = oValues[0];//
y_az = oValues[1];// orientationValues[1];
z_az = oValues[2];// =
// test portrait or landscape (different on Tablets)
int test = getResources().getConfiguration().orientation;
// if necessary, azimuth = azimuth+270f or + 90f %360
heading.setText("X:" + Math.round(azimuth) + " Y:"
+ Math.round(y_az) + " Z: " + Math.round(z_az)
+ " degrees");
}
}
我最终让平板电脑保持横向模式,因为大多数用户都会按照这种方式定位,而North则处于景观的顶端。我希望我的菜鸟代码(在其他人的帮助下)可以帮到你。