Android Multi-resolution multi-screen density UI adaptation scheme

Source: Internet
Author: User
Tags android games

Related concepts

Resolution: The number of pixels across the screen, in order to facilitate the general use of the screen pixel width (number of horizontal pixels) multiplied by the height of the pixel, the shape of 1280x720, and the resolution is 1280x720 screen, pixel width is not necessarily 1280

screen density: indicates the number of pixels in the unit area, usually in DPI, that is, how many pixels per inch.

px : unit of length, in specific pixels

DP : unit of length, independent of the specific screen density, when displayed depending on the specific platform screen density of the final conversion to the corresponding pixel length, the specific conversion rule is: 1DP = (target screen density/standard density) *px, the standard density of 160dpi, for example, 1DP length on a 160dpi platform represents the length of a pixel, while the 240dpi platform represents a length of 1.5 pixels

Screen Size: The size of the screen, usually indicated by the length of the screen diagonal

Android interface adaptation mechanism

The adaptation of the UI interface on different platforms is influenced by screen size and screen density, and the Android adaptation mechanism is to add the constraints to these two factors after the resource, differentiate different platform resources by different qualifications, and Android will choose the resources that satisfy the platform when using the resources. Then find the closest to the condition, and then find the default (that is, without qualification), by selecting the resources suitable for the current platform to complete the different platform adaptation.

Screen size is divided into: small,normal,large,xlarge, respectively, small, medium, large, large screen

Screen density is divided into: ldpi,mdpi,hdpi,xhdpi, their standard values are: 120dpi,160dpi,240dpi,320dpi

Each of these divisions represents a range:

Adding the above qualification to the resource directory allows you to specify a specific applicable platform for the resource, as shown below

Represents a large screen, medium density layout will choose the above main.xml, large screen, medium density will choose the following main.xml

In the actual development process, the screen size is not intuitive, Android converts it to a resolution representation, depending on the screen specific resolution can choose the appropriate qualifier

Summary: By adding the above limits can be achieved by an APK suitable for several mainstream screen size and screen density, this limited method is suitable for external release applications, do not know the specific parameters of the terminal, but can not be accurately adapted to the screen size and density of the two platforms are not very good distinction.

In order to solve the above problems, since Android3.2, the introduction of accurate adaptation, in theory can be adapted to any pixel width, height, screen density of the platform, you need to add a qualifier in the following ways

Where W1280DP indicates that the screen width of 1280DP,H752DP means that the screen height of 752dp,160dpi represents the screen density, where the screen width, height must be in DP units, in the case of knowing that the screen like wide height can be obtained by formula: 1DP = (target screen density/ Standard density) *px converted into DP units.

For example: A platform screen width, height of 1920px,720px, screen density of 240dpi

The qualification for this platform is:

Or

According to the formula 1dp= (240/160) px=1.5px, width, height to DP units are 1280DP and 480DP respectively.

Android Adaptive layout layouts with different resolutions or different screen sizes (horizontal screen | vertical screen )


A: Different layout


Android phone screen size is different, there are 480x320, 640x360, 800x480. How can I get the app to adapt to various screens automatically?
In fact, it is very simple to create a different layout folder in the Res directory, such as layout-640x360,layout-800x480, All layout files are written to R.java after compilation, and the system uses the appropriate layout according to the size of the screen.


II: HDPI, MDPI, ldpi


In the previous version, there was only one drawable, while the 2.1 version had drawable-mdpi, drawable-ldpi, drawable-hdpi Three, and the three were mostly to support multi-resolution.


The difference between drawable-hdpi, drawable-mdpi and drawable-ldpi:


(1) drawable-hdpi contains high-resolution images, such as WVGA (480x800), FWVGA (480x854)


(2) drawable-mdpi contains medium-resolution images, such as HVGA (320x480)


(3) drawable-ldpi contains low-resolution images, such as QVGA (240x320)


The system will be based on the resolution of the machine to each of these folders to find the corresponding picture.


Correction: should be a picture of different density


In developing the program in order to be compatible with different screens on different platforms, it is recommended that the respective folders store different versions of the images according to requirements.


[I] Note: The resolution of the three is not the same, just as you turn down the computer's resolution, the picture will become larger, in contrast to high resolution, the picture shrinks. [/I]
Screen Orientation:


Horizontal screen vertical screen automatically switch:


The Res directory can be established in the layout-port-800x600 and Layout-land two directories, which are placed in vertical screen and horizontal screen two layout files, so that the mobile phone screen direction changes when the system will automatically call the corresponding layout file, Avoid a layout file that does not meet the two screen display issues.


Different resolution horizontal screen vertical screen automatically switch:


Using 800x600 as an example
layout-port-800x600 and layout-land-800x600 Two directories can be established in the Res directory


Do not switch:


The following steps are circulated online, but I myself was through a graphical interface to achieve this configuration, is the same as the same, I will be free to paste the picture.


Also note that each activity has this attribute screenorientation, each activity needs to be set, can be set to vertical screen (portrait), can also be set to no gravity sense (nosensor).


To keep the program interface in one Direction, do not change depending on the direction of the phone:

It can be configured in Androidmanifest.xml. Join this line android:screenorientation= "Landscape".
For example (landscape is landscape, Portrait is portrait):

Java code:

<?xml version= "1.0" encoding= "Utf-8"?>
<manifestxmlns:android= "Http://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"/>&NBSP;
<category android:name= "Android.intent.category.LAUNCHER"/> &NBSP;
</intent-filter> 
</activity>&NBSP;
<activity android:name= ". GamePlay "&NBSP;
android:screenorientation=" Portrait "></activity >&NBSP;
<activity android:name= ". Optionview "&NBSP;
android:screenorientation=" Portrait "></activity >&NBSP;
</application> <uses-sdk android:minsdkversion= "3"/>&NBSP;
</ Manifest>


In addition, the switch of each screen in Android will restart activity, so you should save the current active state before the activity is destroyed, and load the configuration when the activity is re-create, so the game in progress will not be restarted automatically!


Some programs are suitable to switch from vertical screen to horizontal screen, or vice versa, how to do this time? The following configuration android:screenorientation= "Portrait" can be configured where the activity is configured. 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 when configuring activity is to configure the following: Android:configchanges= "Keyboardhidden|orientation", another need to rewrite the activity's Onconfigurationchanged method. The implementation is as follows, do not need to do too much content:

@Override
public void onconfigurationchanged (Configuration newconfig) {
Super.onconfigurationchanged (Newconfig);
if (This.getresources (). GetConfiguration (). Orientation ==configuration.orientation_landscape) {
Land does nothing is OK
} else if (This.getresources (). GetConfiguration (). Orientation ==configuration.orientation_portrait) {
Port do nothing is OK
}
}

Write a support multi-resolution program, based on 1.6 development, the establishment of three resource folder drawable-hdpi drawable-mdpidrawable-ldpi, which contains 72*72 48*48 36*36 icon file respectively. When I test on G1 (1.5 system), the icon should be adaptive to 48*48, but the actual display is 36*36. How can I make it adaptive to the icon of 48*48?

Workaround drawable-hdpi drawable-mdpi drawable-ldpi to drawable-480x320 drawable-800x480 Multi-resolution supported folder



For Android game development we have to think about compatible Android tablets like the iphone, for Apple to consider the compatibility between screens such as ipad, iphone 3GS and iphone 4, For almost all of the resolutions summed up about more than 20 of the size of the chalk and the corresponding relationship, for the development of Android games can take into account the future of 3.0 and a lot of tablet needs.

Conventional we may only consider QVGA,HVGA,WVGA,FWVGA and DVGA, but left the phone to talk about, may use the tablet like WSVGA 1024x576 and WXGA 1280x768 and so on.
QVGA = 320 * 240;
WQVGA = 320 * 480;
WQVGA2 = 400 * 240;
WQVGA3 = 432 * 240;
HVGA = 480 * 320;
VGA = 640 * 480;
WVGA = 800 * 480;
WVGA2 = 768 * 480;
FWVGA = 854 * 480;
DVGA = 960 * 640;
PAL = 576 * 520;
NTSC = 486 * 440;
SVGA = 800 * 600;
WSVGA [...]


This is a representative of the Android software Resource Pack, drawable is stored in the application of the icon file, layout is stored in the layouts, simply say how these icons are placed. Why it takes so many resource pack files and layout files on Android is the next question we need to discuss.



Android device screen size is a variety of, such as millet is 4 inches, the Xoom tablet is 10 inches, the resolution is also strange, 800x480,960x540, etc., Android version of the fragmentation of the problem is lingering in the heart, However, in the design of the application can be divided into two chunks: 3.0 before the version and the version after 3.0. What's the problem with this situation? Let's illustrate with three assumptions.



1. If you have two 4-inch devices on your hand, device A has a resolution of 800x480 and device B has a resolution of 1600x960. You've designed a 64x64 pixel icon on device A to feel it's the right size, but when you put it on device B, the icon looks like it's half the size of the previous one.

2. Assuming that you have two devices on your hands, device A is 4 inches and device B is 10 inches. A tab control is placed above device A, with three tabs. When you put it on device B, the tab control's three tabs are pulled very long, and the space that originally placed 6 pages is only three pages signed.

3. Assuming that you have two devices, device A is Android2.3, device B is Android4.0, and device B does not have a menu, style is not the same. You find it inappropriate to use the same style of skin on two devices.



Google offers a set of systems to address these issues. We go back to the picture above, drawable folder has ldpi, MDPI, hdpi, xhdpi four kinds. DPI refers to pixels per inch, while ldpi refers to 120,mdpi refers to 160,hdpi refers to 240,xhdpi 320. Xiaomi mobile phone is 4 inches, 854x480 resolution, then the Xiaomi phone dpi is 854 square plus 480 squared and open 2 square after divided by 4, the result is about 245. If the app is installed on a Xiaomi phone, then the system calls the resources in the drawable-hdpi in the diagram. In this way, you only need to do 4 sets of resources in drawable-ldpi, drawable-mdpi, drawable-hdpi and drawable-xdpi (the icon can be produced according to the proportion of 3:4:6:8 image resources), Then we can solve the problem mentioned in the above hypothesis 1.



For devices with the same DPI, but not the same size, you can control the layout of various resources through a layout file. Google divides the device into small (two to three inches), normal (4 inches or so), large (5~7 inches), and XLarge (more than 7 inches). In the above hypothesis 2, we can configure the tab bar of 3 tabs in Layout-normal, and configure the tab bar of 6 tabs in Layout-xlarge. If your app has the same layout on all devices, you don't have to consider layouts for different sizes. Those layout* folders can be seen, the application on the HDPI and XHDPI support the screen, and the layout of the screen is inconsistent, but does not consider the different sizes of devices using different layouts.



Android3.0 before the style and Android3.0 (including 3.0) after the style of the difference is very large, the image of the application used in two styles of resources and layout. Android2.3 Millet will use the files in drawable-hdpi and layout-hdpi, and Android4.0 millet will use the files in Drawable-hdpi-v11 and layout-hdpi-v11.


This is the end of the day, when you have time to say the use of 9-patch. This article can only play a role, in the actual design of the application also need to refer to other documents, especially the official Android development documents.

Android Multi-resolution multi-screen density UI adaptation scheme

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.