Horizontal and vertical screen switching in android game development

Source: Internet
Author: User

During Game Development, some games can only be played on horizontal screens, so when mobile phones are erected,
Make sure that the game screen remains landscape. It is actually very easy to achieve this requirement,
Configure androidmanifest. xml. Add this line to Android: screenorientation = "Landscape ".

But the problem is that this will cause the activity to restart. Therefore, you need to save the status of the current activity before the activity is destroyed,
Load the configuration when the activity creates again, so that the ongoing game will not automatically restart!

However, a better solution is to add the Android: configchanges = "keyboardhidden | orientation" attribute to the corresponding activity, so that the activity will not be restarted. instead, call onconfigurationchanged (configuration newconfig ). in this way, you can adjust the display mode in this method.

For example

JavaCode 
If (newconfig. Orientation = configuration. orientation_landscape ){
// Horizontal
Setcontentview (R. layout. file_list_landscape );
} Else {
// Vertical
Setcontentview (R. layout. file_list );
}

Android: configchanges = "keyboardhidden | orientation" can be added directly without additional code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.