How to detect landscape left (normal) vs landscape right (reverse) with support for naturally landscape devices?

北城以北 提交于 2019-11-29 09:26:02
AaronM

Right landscape wasn't supported till 2.2. You'll need to have <2.2 devices choose in the options or something.

The only way I managed to overcome this (Without going to API 8) is by registering to the SensorManager (Sensor.TYPE_ACCELEROMETER) and get onSensorChanged events.

I noticed that when your screen is in landscape mode, you will get close values to -9 (g) or +9 (g) on the latheral (x) axis. you can just check for positive or negative, but you need to save the state in the case the device is put back on the table with face upwards

Your code (snippet) should roughly look like that:

private float mShiftAngle = 0;

public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        aValues = event.values; 
        int deviceRot = getResources().getConfiguration().orientation; 
        if (deviceRot == Configuration.ORIENTATION_LANDSCAPE) {
            if (Math.abs(aValues[0])>4)
                mShiftAngle = (aValues[0]>0 ? 90 : -90);
        }
        else 
            mShiftAngle = 0;       
    }
}

it helped me, hope this will help you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!