Detecting device orientation change using android:screenOrientation

对着背影说爱祢 提交于 2019-12-10 10:59:38

问题


Is there a way to detect device orientation events or at least to get the current device orientation while using android:screenOrientation in the manifest ? I mean device orientation not application orientation.

My problem is I really want to keep my application in portait mode (it's a web view) but I want to handle orientation change myself in my app. When android:screenOrientation is setted, orientation getted by methods is always the application orientation, not the device orientation.

Regards, Gaëtan


回答1:


Yo can use the acelerometer to detect the device orientation. I know, it's more hard but I think is the only solution.

Android | Sensor Manager

Android | Sensor Manager | getOrientation

Happy coding :)




回答2:


If you want to handle device rotation yourself, then use the following method:

@Override
public void onConfigurationChanged(Configuration config)
{
    super.onConfigurationChanged(config);
    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    if(display.getRotation() == Surface.ROTATION_90)
    {           
        mFlash.startAnimation(mRotate);
    }
    if(display.getRotation() == Surface.ROTATION_90)
    {
        parameters.setPreviewSize(width, height);                           
    }

    if(display.getRotation() == Surface.ROTATION_180)
    {
        parameters.setPreviewSize(height, width);               
    }

    if(display.getRotation() == Surface.ROTATION_270)
    {
        parameters.setPreviewSize(width, height);
        mCamera.setDisplayOrientation(180);
    }

}

The actions inside the if statements are just examples of what might be done. Obviously, handle whatever you need to there.




回答3:


I think the nice orientation (portrait or landscape) values that you get will always give you "application orientation". For device orientation you can check out SensorManager.getOrientation.

Unfortunately the method for listening to changes in this value is marked deprecated, so you may need to poll periodically.



来源:https://stackoverflow.com/questions/5464555/detecting-device-orientation-change-using-androidscreenorientation

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