android screen orientation mixing up

橙三吉。 提交于 2019-12-24 03:09:20

问题


My android application is designed to be landscape-only. I have put

        android:screenOrientation="landscape"
        android:configChanges="orientation|keyboardHidden|screenSize"

into the manifest. And it really launches in landscape orientation. And that's cool. But! I need to know device screen size to perform some calculations. And I have added following code into onCreate():

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    // utilize size to calculate something...

If I launch the app from portrait orientation, I get (Google Nexus) size.x = 800, size.y = 1205 instead of correct (for landscape) size.x = 1280, size.y = 736.

It seems for me that Android needs some time to switch from portrait to landscape (because eventually I see my app "landscaped"). But onCreate is called before this switch -- and it slays my calculations. Is my guess correct?

I tried to move calculations from onCreate() to onGlobalLayout() but I still get portrait sizes after following steps:

  1. launch app in landscape
  2. press sleep button (screen goes dark)
  3. rotate to portrait orientation
  4. press sleep button (screen fades in presenting unlock screen)
  5. unlock the device
  6. my app wakes up receiving portrait orientation size
  7. on next onGlobalLayout() I get landscape size at last, but it's too late.

Can I get rid of portrait orientation completely?


回答1:


I spent days on this same problem. As other posters revealed, when returning from sleep/lock screen, the app is handed the same portrait mode that the lock screen has. The real find was that it goes through onResume() and onWindowFocusChanged twice - once for the portrait mode, then for the landscape mode. I used the following code in both onResume() and onWindowFocusChanged() to check before executing screen initializations:

int gW = view.getMeasuredWidth();
int gH = view.getMeasuredHeight();
if (gW > gH) {// landscape!
// the height will be set at this point
    // so reinitialize your screen here
}

It helped me. Hope this helps you.




回答2:


no you cannot.

If you want to get view sizes you can override the onLayout http://developer.android.com/reference/android/view/View.html#onLayout(boolean, int, int, int, int)



来源:https://stackoverflow.com/questions/16775871/android-screen-orientation-mixing-up

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