Android screen adaptation

Source: Internet
Author: User

Android screen adaptation has always been a headache for developers. Because android devices have different sizes and different screen display effects, it is a great challenge to adapt android devices to the screen, in order to deal with different situations, we need to carefully study the definition of different screens in android. I have read this blog from the school slag and hope you will have a basic understanding of screen adaptation.

1. android screen size

Android screens are large and small. To provide the best experience for devices of different sizes, You need to design devices of different sizes, the first problem is how android distinguishes the screen size.

Android screen problems are described in detail on the official website. As a scum, you need to take a closer look. First, android divides the screen into four sizes: small, normal, large, xlarge, the four screens are intuitively differentiated, that is, the diagonal line of the device is measured with a ruler and measured in inches. For example, the Red Rice used for slag is 4.7 inch, which is normal, specific Classification 1:


Figure 1 android screen size differentiation

If your program knows the screen of the android device, different layout schemes can be used according to different sizes. However, when we write a program, the layout in inches cannot be used, let's assume that we use pixel (px) for layout, but here we will encounter a problem. Here we need to introduce the new term, please refer to 2 ..... <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Environment + INXiwO/Environment + 0vLi49sr10 + + jrGFuZHJvaWS52c34yc/Environment + Environment/A7c/environment/xy9jK/cS/Environment + zcrHw7/Environment + Environment/environment/Environment + Environment + NHUo6zL/L/weight + z37Tno6y/ydLUy + weight/ydLUss6/vNXiwO + weight + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java; "> 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 (). getdefadisplay display (). getMetrics (metrics); // get the device resolution, that is, the number of pixels in width and height 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 density 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 density is 213

As you can see from the output of the two devices, the slag actually defines two avds of 4.7 inch, but they have different resolutions (android contains the Bottom Bar, which looks a little different ), the two examples illustrate a problem: cell phones of the same size can have different resolutions, and then different density (because density = resolution/size ).

3. The program determines the screen size.

Every program needs to determine the screen size, and then select the appropriate layout (as will be said later). How can we determine the size (the actual size of the device can be understood as the number of inches on the diagonal line of the device, based on the two parameters mentioned in 2, one is resolution, and the other is density. In device 1, the height is 1184 pixels (px ), the density is 320 dpi (pixel/inch), the specific value of the height is 1280/320 = 4 (inch), the width is 800/320 = 2.5, and the diagonal length is calculated using the stock theorem, the screen size of the device is determined by matching with figure 1.

After determining the screen size, you must declare the supported screen size in mainfest, for example:

 

If you want a good user experience, you can select different layout based on different sizes as mentioned in the official android documentation, for example, res/layout-large is the layout used in the large screen.

Please note the following in the official documents: Beginning with Android 3.2 (API level 13), the above size groups are deprecated and you shoshould instead usesw dp Configuration 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 screen width, you should place it inlayout-sw600dp/. That is, for more than 3.2, directly using dp (dp is equivalent to the inch size in a program) to determine the screen width, and then select a different layout, such as/res/layout-sw600dp, that is, if the width requirement is greater than 600dp, we will choose this layout. Similar to small, large, and so on, the idea only replaces the size with a specific number.

3 de-inches

As we mentioned above, how can we make a program display the size in pixels? But this is totally impossible. Let's take a simple example to illustrate the pixel defect. We still use the above device 1 and device 2 for testing. We define a button in both device 1 and device 2, the width and height are both 200px (pixels), and effect 2 is shown below:


Figure 2 a cell phone of the same size and a button of the same pixel size

In fact, this result is not surprising at all, because the two buttons have the same pixel size but different density, and the final actual size is drawn in inches, because the density is pixel/inch, the actual size (in inches) is pixel/density. So how can this problem be solved, because our program must have the same size on the screen of the same size. If the two spaces have the same size, the number of inches is equal, while the number of inches cannot be directly written in the program, so there is a against-day term, dp. In fact, we have seen this dp in 2. What is dp?

In the official website, dp is called Density-independent pixel (dp), that is, Density-independent pixels, which can be used to get rid of the tangle of Density, this is a new unit that I mentioned earlier with the "de-Inch" function. We will not mention the specific calculation of dp (which will be discussed later ), let's assume that this dp is a brand new measurement unit, so that the equal dp has an equal length in the screen of different density, such as 1dp whether you are on device 1 or device 2, the values are equal. This solves the problem of "de-Inch.

4 dp application and image problems

Through the description in 3, we should know that any control in layout uses dp to ensure consistent display. This is also recommended by google.

However, for an image, we can specify the size of the image in layout. If we specify the image width of 1 inch (which can be converted to dp, which will be later), for an image, the actual width is 15 pixels. In a screen with a density of 3dpi, three pixels can be displayed. In this case, 12 pixels need to be discarded, and the image must be extremely unclear; the screen with a density of 15 DPI can display 15 pixels, which is completely displayed. In this case, the resolution is fixed. If the width is 1 inch, the image with all 15 pixels is displayed clearly, how can we make the pictures displayed on screens of different density clearer? We can select different images for screens of different density, that is, high-density images. For any image, we can make three, one low-resolution, one high-resolution, and one median. for high-density screens, for example, the density is 15, we choose an image with a higher resolution (that is, a large image), such as 15 inch. For an image with a density of 3, we want it to display a picture with a resolution of 3, this ensures that no information is lost and the image is clear.

For screens of different density, you can select images through/res/drawable-hdpi. This official website also has.

5 dp computing

In fact, we do not need to calculate the dp, but to make it easy to understand, let's talk about the calculation of the dp, px = dp * (dpi / 160) This is the calculation formula on the official website. For a screen with a density of DPI, a px is equal to a dp ....

In addition, the font size in android is sp, which has not been studied yet. However, the function estimation is similar to that in dp.

This blog took me a noon time to learn about android. I think it should be based on the official website. If the content in this article is incorrect, thank you!

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.