10

これは簡単に別の質問の複製になる可能性があります。Imは何を検索するかを理解するのに苦労しています。

私のカメラアプリは、次のように(マニフェストで)横向きモードでロックされています。

android:screenOrientation="landscape"

ただし、デバイスを縦向きに回転させたときに、いくつかのUI要素を回転させたいと思います(Androidは横向きであると見なしますが、それは意図的なものです)。

だから私はこれを試して向きを確認しました

int rotation = this.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0:
            Log.d("Rotation", "0");
            break;
        case Surface.ROTATION_90:
            Log.d("Rotation", "90");
            break;
        case Surface.ROTATION_180:
            Log.d("Rotation", "180");
            break;
        case Surface.ROTATION_270:
            Log.d("Rotation", "270");
            break;
    }

残念ながら、電話の向きに関係なく、常に90が返されます。Androidがオリエンテーションを「考えている」かどうかに関係なく、オリエンテーションを取得するためのより堅牢な方法はありますか?

4

5 に答える 5

4

向きを検出するために、これをセンサーマネージャーへの登録に使用します。

mSensorOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);      
mSensorManager.registerListener(this, mSensorOrientation, SensorManager.SENSOR_DELAY_NORMAL);

そして、これは向きの変化を検出するために、コメントに独自のメソッド実装を入れることができます。

定数:

public static final int LYING = 0;
public static final int LANDSCAPE_RIGHT = 1;
public static final int PORTRAIT = 2;
public static final int LANDSCAPE_LEFT = 3;



public void onSensorChanged(SensorEvent event) {

Sensor sensorEvent = event.sensor;

if ((sensorEvent.getType() == Sensor.TYPE_ORIENTATION)) {

    float [] eventValues = event.values;

    // current orientation of the phone
    float xAxis = eventValues[1];
    float yAxis = eventValues[2];

    if ((yAxis <= 25) && (yAxis >= -25) && (xAxis >= -160)) {

        if (previousOrientation != PORTRAIT){
            previousOrientation = PORTRAIT;

            // CHANGED TO PORTRAIT
        }

    } else if ((yAxis < -25) && (xAxis >= -20)) {

        if (previousOrientation != LANDSCAPE_RIGHT){
            previousOrientation = LANDSCAPE_RIGHT;

            // CHANGED TO LANDSCAPE RIGHT
        }

    } else if ((yAxis > 25) && (xAxis >= -20)){

        if (previousOrientation != LANDSCAPE_LEFT){
            previousOrientation = LANDSCAPE_LEFT;

            // CHANGED TO LANDSCAPE LEFT
        }
    }
}

}

于 2013-02-12T15:40:34.133 に答える