0
このアプリでは、画面の向きがポートレートで固定されていて、画面全体を回転させるのではなく、アクティビティを再構築する必要があります。代わりに加速度計を使用してください。一部のデバイスでセンサータイプSensor.TYPE_MAGNETIC_FIELDを取得できませんでした
次のコードは、私がこの方向性を計算するために地磁界を得ることができなかったこの1つのデバイス(One + Running 6.0)に遭遇するまで、私がテストしたすべてのデバイスでうまく機能します。
これはハードウェアの問題ですか?私はいくつかのアクセス許可が足りないのですか 私はそれを調べましたが、私は加速度計の実行時にパーミッションを尋ねる必要があることを伝える文書は見つかりませんでした。ここで
はonSensorのコードです:
public void onSensorChanged(SensorEvent event) {
boolean isOrientationEnabled;
try {
isOrientationEnabled = Settings.System.getInt(getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION) == 1;
} catch (Settings.SettingNotFoundException e) {
isOrientationEnabled = false;
}
if (!isOrientationEnabled){
if(this.Orientation!= ORIENTATION_PORTRAIT)rotateViews(ORIENTATION_PORTRAIT);
this.Orientation = ORIENTATION_PORTRAIT;
return;
}
if(allow_rotation) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
//mGeomagnetic is null
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);
// orientation contains: azimut, pitch and roll
float pitch = (float) Math.toDegrees(orientation[1]);
if (pitch < -45 && pitch > -135) {
// if device is laid flat on a surface
if(this.Orientation!= ORIENTATION_PORTRAIT) rotateViews(ORIENTATION_PORTRAIT);
this.Orientation = ORIENTATION_PORTRAIT;
return;
}
float roll = (float) Math.abs(Math.toDegrees(orientation[2]));
if ((roll > 60 && roll < 135)) {
// The device is closer to landscape orientation. Enable fullscreen
int landscape_mode;//0 = right, 2 = left
if (Math.toDegrees(orientation[2]) > 0) landscape_mode = ORIENTATION_LANDSCAPE_RIGHT;
else landscape_mode = ORIENTATION_LANDSCAPE_LEFT;
if(this.Orientation!=landscape_mode) rotateViews(landscape_mode);
this.Orientation = landscape_mode;
} else if (roll < 45 && roll > 135) {
// The device is closer to portrait orientation. Disable fullscreen
if(this.Orientation!=1)rotateViews(ORIENTATION_PORTRAIT);
this.Orientation = ORIENTATION_PORTRAIT;
}
}
}
}
}
こんにちは、前に述べたmGravityとgravityNormの取得方法を詳しく教えてください。ありがとう。 – Minitour