Detecting device orientation change using android:screenOrientation

99封情书 提交于 2019-12-06 10:51:13

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 :)

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.

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.

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