Android screen adaptation

Source: Internet
Author: User

Android screen adaptation has always been a headache for developers, because Android devices vary in size, and screen display is not the same, how to screen adaptation to Android is a big challenge, in order to deal with different situations, need to carefully study the android to different screen definition. Read this blog, I hope you have a basic understanding of screen adaptation.

1 Android Screen Size

Android screens are large and small, in order to provide the best experience for devices of different sizes, different sizes of devices need to be designed differently, the first problem is the screen size of Android to distinguish.

Android screen problems in the official website has detailed description, as a slag, need to study, first Android to the screen into four sizes : Small,normal,large,xlarge, these four kinds of screen first is intuitive to distinguish, That is, the diagonal of the device with a ruler, in inches to distinguish between units, such as slag with the red rice is 4.7 inches, belongs to normal, the specific Category 1 shows:


Figure 1 Android Screen size distinction

If your program learns the Android device screen, depending on the size of the different layout scheme can be used, but when we write the program, layout, it is not possible to layout in inches, first assume that we use the pixel (px) layout, but here will encounter a problem, here we need to introduce the new term, Please see 2 .....

2 screen resolution and density

Here are some of the problems you need to see a few terms, Android official online after the introduction of a few terms, slag first introduce the resolution and density

Resolution (Resolution): The total number ofphysical pixels in a screen. that is, the physical pixels of a device, such as 1280*720, can be computed by themselves.

DensityScreen density):The quantity of pixels within a physical area of the "screen" usually referred to as dpi (dots per inch).Our density is generally measured in pixels per square inch , but a new density representation is recommended, which is the number of pixels per inch (dpi). The density we use later is this dpi.

Android uses the resolution and density of these two domains to record the true size of the device, the main reason for using density (the residue guessed) should be "Go", that is, in the program do not appear in the unit, Therefore, the two values of density and resolution can be used to save the true size of the device.

For a program, it can get to the screen resolution and size, and then density = resolution/size , you can calculate the size, in Android, get density size can refer to here. Here we use a small program to find the resolution and density of the two devices. The residue defines two avd sizes of 4.7 inches, the resolution of device 1 is 800*1280, the resolution of device 2 is 480*800, and the test code is as follows:

Package Com.example.testscreen;import Android.os.bundle;import Android.app.activity;import Android.util.displaymetrics;import Android.view.menu;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); Displaymetrics metrics = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics);  Get the resolution of the device, that is, the width and height of the number of pixels int height = metrics.heightpixels; int width = metrics.widthpixels;  System.out.println ("The height is" +height + "the width is" + width);  Get the density of the device int density = metrics.densitydpi; SYSTEM.OUT.PRINTLN ("The density is" + density);}

The output of device 1 is:

06-09 00:10:17.188:i/system.out (2041): The height is 1184 the width is76806-09 00:10:17.188:i/system.out (2041): the Dens Ity is 320

The output of device 2 is:

06-09 00:45:35.085:i/system.out (2088): The height is 736 the width is48006-09 00:45:35.085:i/system.out (2088): the Densi Ty is 213

The output from both devices can be known, In fact, the slag is defined two 4.7-inch AVD, but with different resolutions, (Android has a bottom bar, looks a little different), these two examples illustrate a problem: the same size of the phone, can have different resolutions, and then there are different densities, (because density = resolution/size).

3 Program Judging screen size

Each program needs to determine the size of the screen, and then choose the appropriate layout (later said), how to determine the size ( the true size of the device, can be understood as the number of inches of the device diagonal), according to the 2 mentioned two parameters to judge, one is the resolution, one is the density, with equipment 1, Knowing that height is 1184 pixels (px) and its density is 320dpi (pixels per inch), then the specific height is 1280/320=4 (inches), width is 800/320=2.5, the length of the diagonal is calculated with the Pythagorean theorem (the slag is not fine here) and the figure 1 matches, Get the screen size size of this device.

After the screen size is determined, you first need to declare the supported screen sizes in mainfest, such as:

<supports-screens android:largescreens= "true"/>

For a better user experience, the program can choose different layouts depending on the size, and the official Android document also mentions that different layouts can be set for different sizes, such as Res/layout-large is the layout used when large the screen.

There are a few words to note in official documents:Beginning with Android 3.2 (API level), the above size groups is deprecated and you should instead use thesw<N>dpconfiguration Qualifier to define the smallest available width required by your layout resources. For example, if your Multi-pane tablet layout requires at least 600DP of the screen width, you should place it inlayout-sw600dp/.that for3.2 or more, directly using DP (DP equivalent to the size of an inch in a program) to determine the width of the screen, and then choose a different layout, such as/RES/LAYOUT-SW600DP, is the width of the requirement is more than 600DP we choose this layout. But the meaning and small,large and so on, just change the size of the specific numbers.

3 Go-inch

Above we mentioned the go-to-inch, how to make a program to display the size of a good, it seems to be able to use pixels? But this is totally not possible. To give a simple example of a pixel defect, or to use the above device 1 and device 2 for testing, we define a button in device 1 and device 2, both width and height are 200px (pixels), Effect 2 is shown:


Figure 2 The same size of the phone, the same pixel size button

In fact, this result is not surprising, this is because the pixel size of the two button is the same, but the density is different, and the final true size is drawn in inches, because the density = pixels per inch, the real size (in inches) = pixel/density . Then this problem how to break, because our program must be in the same size of the screen on the same size, if the size of two space is equal to the number of inches, and the number of inches can not be directly written in the program, and then appear an inverse of the term, DP. Actually in 2 we have seen this DP, then what is DP?

DP in the official website called Density-independent Pixel (DP), that is, density-independent pixels, that can get rid of the density of the winding, is the previous I mentioned a "de-inch" function of a new unit , Let's not mention the specific calculation of DP (later on), let's assume that this DP is a new unit of measure, so that the equivalent DP in different densities of the screen has equal length , such as 1DP whether you are in device 1 or device 2, the size is equal. This is the solution to the "go-inch" problem.

4 DP Application and picture problems

&NBSP;   by 3 description, we should be able to know that Any control in layout can be guaranteed to display a consistent problem by using DP. This is also recommended by Google.

    but for a picture of this outsider, we can specify its size in layout, If we set the width of the picture 1 inches (can be converted to DP, will be later), for a picture, its true width of 15 pixels, in the density of 3dpi screen, can display 3 pixels, then need to drop 12 pixels, the picture must be extremely unclear; At a density of 15dpi The screen can display 15 pixels, which is completely displayed. This has the problem of the resolution of High density select HD picture

For screens of different densities, you can select pictures by/res/drawable-hdpi, and so on. This website is also available.

5 Calculation of DP

    In fact, we do not need to calculate the DP, but for the sake of understanding, I still have to say the DP calculation, &NBSP; PX = DP * ( dpi/160) This is the calculation formula for the official website. For a screen with a density of 160dpi, an px equals a DP ....

In addition: Android the size of the font takes the SP , the specific is not studied, but the effect is estimated to be the same as DP.

This blog spent a lunch time, for Android learning, Slag thought should take the official website as the main reference, if the contents of this article is wrong, welcome to point out, thank you!

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.