The screen sizes of Android devices are various, but they can be divided into two major parts during design: versions earlier than 3.0 and Versions later than 3.0. What problems will this cause? Let's use three assumptions to explain.
1. Assume that you have two 4 inch devices on your hand. The resolution of device A is 800 × 480, and that of Device B is 1600 × 960. You have designed A 64*64 pixel icon on device A, and it seems to be in the right size, but when it is placed on Device B, it looks like it is only half the size. 2. Assume that device A is 4 inch and Device B is 10 inch. A tab control is placed on top of device A, which has three tabs. When I put it on Device B, the three tabs of the tab control were pulled a long time. The space that originally put the six tabs was only put on three tabs. 3. Assume that device A is installed with Android2.3 and Device B is installed with Android4.0, but Device B is not created with menu, and the style is different. You find it inappropriate for two devices to use the same style of skin.
Google provides a system to solve these problems. Let's go back to the figure above. The drawable folder contains ldpi, mdpi, hdpi, and xhdpi. Dpi indicates pixels/inches, ldpi indicates 120, mdpi indicates 160, hdpi indicates 240, and xhdpi indicates 320. The cell phone resolution is 4 inch, 854 × 480, so the dpi of the cell phone is 854 square plus 480 square sum of squares after the power of 2 divided by 4, the result is about 245. If the application is installed on a Xiaomi mobile phone, the system will call the resources in the drawable-hdpi in the figure. In this way, you only need to set four sets of resources under drawable-ldpi, drawable-mdpi, drawable-hdpi, and drawable-xdpi respectively (the icons can be 3: 4: 6: 8), then we can solve the problem mentioned in hypothesis 1 above.
For devices with the same dpi but different sizes, you can use the layout file to control the layout of various resources. Google divides devices into small (2 ~ 3 inch), normal (about 4 inch), large (5 ~ 7 inch), xlarge (more than 7 inch ). In the preceding example, we can configure the tab columns of three tabs in layout-normal and configure the tab columns of six tabs in layout-xlarge. If the application layout is the same on all devices, layout of different sizes is not considered. The layout * folder shows that the application supports horizontal and vertical screens on hdpi and xhdpi, and the layout of the horizontal and vertical screens is inconsistent, but the layout of devices of different sizes is not considered.
The style before Android3.0 differs greatly from that after Android3.0 (including 3.0). In the figure, the app uses two styles of resources and layout. Android2.3 millet will use drawable-hdpi and layout-hdpi files, and Android4.0 millet will use drawable-hdpi-v11 and layout-hdpi-v11 files.I. Different layout
The screen sizes of Android mobile phones vary by 480x320,640x360,800 x. How can an App automatically adapt to different screens?
In fact, it is very simple, just need to create different layout folder under the res directory, such as layout-640x360, layout-800x480, all the layout files will be written to R after compilation. java, and the system selects the appropriate layout based on the screen size.
Ii. hdpi, mdpi, and ldpi
In earlier versions, there was only one drawable, while in version 2.1, there were three types: drawable-mdpi, drawable-ldpi, and drawable-hdpi, which were mainly used to support multi-resolution.
Differences between drawable-hdpi, drawable-mdpi, and drawable-ldpi:
(1) The drawable-hdpi contains high-resolution images, such as WVGA (480x800) and FWVGA (480x854)
(2) drawable-mdpi stores medium-resolution images, such as HVGA (320x480)
(3) drawable-ldpi stores low-resolution images, such as QVGA (240x320)
The system will find the corresponding images in these folders according to the machine resolution.
Correction: it should be an image corresponding to different density
To be compatible with different screens on different platforms, we recommend that you store images of different versions in different folders as needed.
[I] Note: the resolution of the three items is different. Just as you lower the resolution of your computer, the image will become larger. On the contrary, the resolution will be high and the image will be reduced. [/I]
Screen direction:
Automatic switching of landscape and portrait screens:
You can create two directories: layout-port-800x600 and layout-land under the res directory, which are placed in the vertical and horizontal layout files, in this way, the system automatically calls the corresponding layout file when the screen direction of the mobile phone changes, to avoid the problem that one layout file cannot meet the two screen display conditions.
Automatic switching of landscape and portrait screens with different resolutions:
Take 800x600 as an Example
Two directories of layout-port-800x600 and layout-land-800x600 can be created under the res directory
Do not switch:
Note: Each activity has this screenOrientation attribute. You must set this attribute for each activity. You can set this attribute to a portrait screen (portrait) or a non-gravity sensor (nosensor ).
Java code:
Package = "com. ray. linkit"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
Android: label = "@ string/app_name"
Android: screenOrientation = "portrait">
Android: screenOrientation = "portrait">
Android: screenOrientation = "portrait">
In addition, the Activity is restarted every time the screen is switched on in android. Therefore, you should save the status of the current Activity before the Activity is destroyed and load the configuration when the Activity is created again, in progress, the game will not automatically restart!
Some programs are suitable for switching from a portrait screen to a landscape screen, or vice versa. What should we do at this time? You can configure android: screenOrientation = "portrait" in the Activity configuration area ". In this way, you can ensure that the portrait screen is always portrait or landscape.
Some programs are suitable for switching between landscape and landscape screens. What should we do? First, you must configure the Activity as follows: android: configChanges = "keyboardHidden | orientation". In addition, you must override 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 nothing is OK
}
}
Write a program that supports multi-resolution, developed based on 1.6, and creates three resource folders drawable-hdpi drawable-mdpi drawable-ldpi, it stores 72*72 48*48 36*36 icon files. When I test on G1 (1.5 of the system), the icon should be adaptive to 48*48, but the actual display is 36*36. How can we make it adaptive to the icon of 48*48?
Workdraund drawable-hdpi drawable-mdpi drawable-ldpi change to the multi-resolution supported folder for the drawable-480X320 drawable-800X480
For Android game development, we have to think about compatibility with Android tablets like the iPhone. for Apple, we need to consider the compatibility between iPad, iPhone 3GS, and iPhone 4 screens, almost all resolutions summarize the size and relationship of over 20 chalk girls. For Android games, we can consider the needs of 3.0 and many tablets in the future.
In general, we may only consider QVGA, HVGA, WVGA, FWVGA, and DVGA. However, if we leave the phone blank, we may use a tablet like WSVGA's 1024x576 and WXGA's 1280x768.
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 Android software resource package. drawable stores the application icon files, layout stores the layout, simply put, how to place these icons. Why do we need so many resource package files and layout files on Android.