Android screen adaptation, in addition to using DP, you can also use the scale

Source: Internet
Author: User


In Android, in order to adapt to different resolutions of the screen, the introduction of the density-independent pixel density-independent pixes, also known as dip, can be abbreviated to DP. This is not related to density because Android provides different scale values for different densities, such as mdpi with a value of 1,hdpi of 1.5,xhdpi to 2. This way, if the same DP value is used, the system uses the Dp*scale value when converting to PX. This gets the same percentage, and the same percentage, which means that the screen fits. such as 480*800,4.0inch's screen, for hdpi. Half of the screen can be represented by 160DP, and its px = 1.5 = 240px, which is exactly half of 480. And 320*640 's 3.5inch screen, for MDPI. Half of the screen is also 160DP, its px = 1 = 160px, exactly half of 320. This allows the DP to be adapted to different screens.
But now xhdpi screen is also very common, such as 720*1280 4.5inch screen, its scale value is 2, so the entire screen is 360DP, so with hdpi and MDPI is not the same, with the DP representation of some error. But the error is within acceptable limits.
All of this is based on the screen width, because the UI is designed to be as wide as possible, and if the height is the reference, the DP value represents a greater difference.
In many cases, the plate is highly standard. It's hdpi's tablet. It's a different kind of wide. For example, the height is 1200 and 1600 of the plate, are hdpi. This means that if it is half the height, the DP value is 400DP and 533, respectively, so the difference is so large, so the layout with DP is obviously no longer applicable.
Workaround: Since the introduction of DP is actually to use the same DP value to represent the same scale, then we can actually be directly proportional representation (DP is the nature of the scale, but the division of scales can not be exhaustive). Of course, the premise is that different screen scale can not be too large, such as horizontal screen and vertical screen can not be common, need to write two layout
For example, the UI gives a design drawing with a size of 2560 * 1920 with a aspect ratio of 4:3. All labels are subject to this size. In order to be able to generalize these annotations without having to calculate manually, you can define the following method of private int px_x (float px) {//To calculate the proportional return (int) (px/2560 * mscreenhight) in the x direction on a width basis;}
private int px_y (float px) {return (int) (px/1920* mscreenwidth);}
This allows you to reset the dimension-related properties in your code so that the relevant content can be adapted to different sizes of screens.
It is important to note that:One drawback of this writing is that layouts can no longer be defined directly in the layout of XML. In particular, the distance between the various controls. However, XML is not useless, it is easy to define the direct relative position of the control through XML, such as Above,below properties, so that the code does not have to repeat the definition. You can get the layout properties by using the View.getlayoutparameters () method, and then overwrite the dimension-related content. (Note: You cannot use the new parameter () method, which will completely overwrite the settings in the XML)

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.