转载: http://polygeek.com/4301_air-mobile_playing-with-stage-orientation-in-mobile-air-apps
Playing with stage.orientation in mobile AIR apps
If you create a mobile app or game you may think that you want the orientation locked to either portrait or landscape all the time. You can do that by setting the appropriate properties in the application descriptor file – app.xml. If you do want to lock it down then just use:
<aspectRatio>portrait</aspectRatio> <autoOrients>false</autoOrients>
That will lock the app to portrait, always. Or of course change it to <aspectRatio>landscape</aspectRatio> to lock it to landscape. It’s that easy.
But you don’t want to do that, probably.
It’s quite possible that you will come into instances where you need to switch back and forth or at least have the option for switching. For instance, you’re game may only play in landscape mode it might be awkward for the user to see their high scores screen in landscape.
A better option is to set the orientation in code.
stage.autoOrients = true/false; stage.setOrientation( StageOrientation.DEFAULT );
If you want just create a simple Flex Mobile project and add these buttons to the HomeScreen:
<s:VGroup width="100%" height="100%" > <s:Button label="set DEFAULT" width="100%" click="{ stage.setOrientation( StageOrientation.DEFAULT ) }" /> <s:Button label="autoOrient = true" width="100%" click="{ stage.autoOrients = true }" /> <s:Button label="autoOrient = false" width="100%" click="{ stage.autoOrients = false }" /> </s:VGroup>
Set the -app.xml file with autoOrients=true and the aspectRatio to be what you want the app to look like by default – I’m using portrait in this example.
Here’s what happens if you start the app up with the phone in landscape:
- Since I set the aspectRatio to portrait it will start up that way even though physically the phone oriented in landscape.
- Now if you rotate the phone to portrait you’ll get a re-orientation flicker. It will notice that the orientation changed and try to go to landscape and then realize it’s in portrait and then re-orient appropriately. I’d say this is a minor bug with the Flex framework. It shouldn’t create a re-orientation flicker. But whatever.
- Now if you re-orient the phone back and forth between portrait and landscape the layout of the GUI will adapt accordingly. If you hit the autoOrient = false button then it will stop re-orienting and be locked into which ever orientation it was when you hit the button.
This should account for most use cases. If you have a game you can set the default to landscape and always be able to force the user back to that orientation. But then when the game is over you can let them switch the orientation to portrait to better view their high scores.
Besides StageOrientation.DEFAULT you also have the option to set the orientation as so:
- stage.setOrientation( StageOrientation.ROTATED_LEFT );
- stage.setOrientation( StageOrientation.ROTATED_RIGHT );
- stage.setOrientation( StageOrientation.UPSIDE_DOWN );
- stage.setOrientation( StageOrientation.UNKNOWN );
For instance in the RunPee app I set the default orientation to portrait and lock it because the layout works best that way. But there is a screen for viewing videos on youTube.com so I turn autoOrient back to true and do a ROTATED_LEFT to orient the screen best for viewing the video. Then when they leave the video screen I set it back to DEFAULT and lock it.