The method of realizing screen adaptive directional dimension and resolution by Android programming _android

Source: Internet
Author: User

An example of this article describes the method of Android programming to achieve screen adaptive direction size and resolution. Share to everyone for your reference, specific as follows:

Android screen Adaptive direction size and resolution, including screen interface layout, multiresolution support, get screen size, screen horizontal screen and vertical screen, etc., Android Simulator horizontal screen, Android virtual machine horizontal screen, Android judgment horizontal screen, Android ban horizontal screen, Android Force horizontal screen, Android horizontal screen event, Android Adaptive resolution, Android screen adaptive, Android image Adaptive, Android horizontal screen layout.

Screen interface layout

Android phone screen size 480x320, 640x360, 800x480, how to let the App automatically adapt to different screens?

We can create different layout folders in the Res directory, such as layout-640x360, layout-800x480, all layout files will be written to the R.java after compiling, and the system will choose the appropriate size of the screen according to their own layout To use.

Multi-resolution Support

Supports multiple resolutions with MDPI, LDPI, hdpi 3 files.

①HDPI contains high-resolution images, such as WVGA (480x800), FWVGA (480x854);
②MDPI containing medium-resolution images, such as HVGA (320x480);
③ldpi contain low-resolution images, such as QVGA (240x320).

The system will be based on the resolution of the machine to these folders to find the corresponding picture, in the program development, in order to be compatible with different platforms different screens, it is recommended that different versions of the picture according to the requirements of the corresponding folder.
Get screen Size

Display display = Getwindowmanager (). Getdefaultdisplay (); 
int width = display.getwidth (); 
int height = display.getheight ();
Displaymetrics dm = new Displaymetrics (); 
DM = Getresources (). Getdisplaymetrics (); 
int screenwidth = dm.widthpixels; 
int screenheight = dm.heightpixels; 
float density = dm.density; 
float xdpi = dm.xdpi; 
float ydpi = dm.ydpi;

Screen horizontal screen and vertical screen

1, Horizontal screen vertical screen automatic switching

First in the Res directory to establish layout-port-800x600 and Layout-land two directories, which are placed in vertical and horizontal screen two layout files, so that when the mobile phone screen direction changes in the system will automatically call the corresponding layout files, Avoid a problem where a layout file cannot meet two screen displays.

Some programs are suitable to switch from the vertical screen to the horizontal screen, or vice versa, this time how to do? The following configuration can be done where the activity is configured:

Copy Code code as follows:
android:screenorientation= "Portrait"

This will ensure that the vertical screen is always vertical, or landscape landscape.

And some programs are suitable for the screen switch. How to deal with it? The first thing to do is configure the activity with the following configuration:

Copy Code code as follows:
Android:configchanges= "Keyboardhidden|orientation"

It is also necessary to rewrite the Onconfigurationchanged method of the activity. The implementation method is as follows:

@Override public 
void onconfigurationchanged (Configuration newconfig) { 
super.onconfigurationchanged ( Newconfig); 
if (This.getresources (). GetConfiguration (). Orientation = Configuration.orientation_landscape) { 
//land do Nothing is OK 
} else if (This.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_portrait ) { 
//port do no is OK 
} 
}

2, different resolution horizontal screen vertical screen automatic switching

For 800x600, for example, you can create two directories of layout-port-800x600 and layout-land-800x600 in the Res directory

Description: Each activity has this attribute screenorientation, each activity needs to be set, can be set to the vertical screen (portrait), can also be set to No gravity sensor (nosensor).

3, the screen is fixed, does not change with the mobile phone direction rotation

Can be configured in Androidmanifest.xml by adding:

Copy Code code as follows:
android:screenorientation= "Landscape"

For example (landscape is transverse, portrait is longitudinal):

 <?xml version= "1.0" encoding= "Utf-8"?> <manifest "xmlns:android=" Schemas.android.com/apk/res/android "package=" Com.ray.linkit "android:versioncode=" 1 "android:versionname=" 1.0 " > <application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name= " . Main "android:label=" @string/app_name "android:screenorientation=" Portrait "> <intent-filter> <action 
Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name= ". GamePlay "android:screenorientation=" Portrait "></activity> <activity android:name=". Optionview "android:screenorientation=" Portrait "></activity> </application> <uses-sdk android: minsdkversion= "3"/> </manifest> 

Android will restart the activity each time the screen is switched on, so you should save the current active state before it is destroyed, and load the configuration when it is done again, so that the game is no longer automatically restarted.

I hope this article will help you with the Android program.

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.