Android screen Adaptation principle

Source: Internet
Author: User

Several concepts:1) screen density (dpi): Dot per inch, which is the number of pixels each inch. LDPI (MDPI), hdpi (+), XHDPI (320) Calculation method: Take the 480x854,4.0inch phone as an example, its diagonal is 4.0inch, the diagonal pixel number is: (480^2 + 854^2) Open root = 979. so its dpi = 979/4 = 245, about 2402) screen size: diagonal length. With Small,normal,large,extra large 3) Direction: Horizontal screen and vertical screen 4) Resolution: A total number of pixel points on a physical screen, such as 480x800. The concept of resolution is not used in our application, mainly DPI and size 5) DP (density-independent pixel) independent pixel units.   An abstract concept that is used to locate the UI layout, including size and position. 1, you can have four folders in the Android Engineering directory res, mainly to support the multi-resolution picture drawable-hdpidrawable-mdpidrawable-ldpidrawable-xhdpi when the design gives transduction, The first thing we need to make clear is that the design gives the transduction in what size. If it is a 480x800 transduction, it should be placed in the drawable-hdpi directory, if it is 320x480 transduction, it should be placed in the MDPI directory. If it is a 720x1280 transduction, it should be placed in the XHDPI directory. When you use the picture, the system looks for the picture according to the machine's resolution to the appropriate folder. 1: How does the system convert the resolution of a picture if only one set of pictures is placed? The Android system loads the image resources according to the following rules: First determine the phone's screen density, and then in the corresponding density folder to find the picture resources. If found, it is displayed. If it is not found, it will be looked up from drawable or other drawable-* folders. When found, the density represented by the folder is compared to the actual density of the phone, thereby scaling the picture. For example, find the resources under drawable (equivalent to from drawable-mdpi), and the phone is hdpi, so that when the image is displayed, Android will enlarge the image to meet the hdpi phone display needs. It is not difficult to imagine that the picture will become blurred. The image is scaled before the system loads the picture, so the size you get through getwidth is already scaled. The ratio of scaling to density remains the same. 2: What's wrong with transduction misplaced folders? If the folder is misplaced, the system will scale accordingly, so that the image can be scaled incorrectly. For example, hdpi pictures in the MDPI, in the hdpi phone display, will be larger than the original picture

question 3:720p picture related questions?  Designed for Transduction, best based on that DPI? Is it all right? Generally should give 480*800 's hdpi of transduction. This will automatically shrink for mdpi. Image display quality is unaffected. For xdpi, the magnification is not large and the quality of image display is small. So why not xhdpi, so it is not better to shrink the image? There are two reasons, one is to increase the size of the APK, the other is easy to cause the deformation of the cut graph, affecting the display. mainly for. 9 of the picture, there is a certain direction of compression, if set, the stretch area is too small, and then compression may deform. This is not absolute, of course, and can be achieved by increasing the stretch area of the. 9.png file. There may also be a reason why the XHPI model is not a majority. 2. DP Working principle   the most important thing to remember is a formula:Android calculates the pixel value first to determine the density of the screen. If we set MDPI to 1, then hdpi equals 1.5. If we use DP as the unit, Android will be converted to pixel according to the following formula: PX = DP * density; DP represents the independent pixel density, so-called independent, is not dependent on the density of the screen. In essence, DP actually represents the same physical length, note: Not in pixels, but in inch units. For example, MDPI, a screen with a length of M inch, has a number of pixels n1,dp to N2. For hdpi, the length is M inch and contains the number of pixels N1 * 1.5. According to the formula can be calculated, its DP value is n1 * 1.5/1.5 = N1. For the xhdpi is also N1. that is, the same physical length has the same DP value.     from the surface, DP can represent an equal proportionThat is, in the screen densities of ldpi, MDPI, hdpi, and XDPI, the same DP value can be used to represent the same proportions. Please note: It is proportional rather than size. Because the UI design, we are most concerned about the proportion of a view relative to the entire screen, for example: in MDPI, the length of a textview is 1/2 of the screen width, assuming that the width of the screen pixel is 320pixel, then TextView is 160pixel. And in hdpi, we need the TextView also accounted for 1/2 of the screen to be called to adapt to different screen density.      To achieve this goal, Android provides the concept of DP. We match the dip-mdpi screen to the DP one by one, which means that 1 pixel in the mdpi screen equals 1 dp. Typically, the screen width of the mdpi is 320pixel, so its screen width is 320DP. The above 4 golden principles of http://blog.csdn.net/tanqiantot/article/details/11174011 for forgetting multi-screen matching

1) fill_parent, Wrap_content, Match_parent, and DP should be used to set the control dimensions in the layout file;

Specifically, when setting the values of the view's properties Android:layout_width and Android:layout_height, thewrap_content,match_parent or DP is better than PX, The text size should be defined using the SP .

2) do not appear in the code of the program specific pixel values, defined in the Dimens.xml;

To make the code simple, Android internally uses the PIX to represent the size of the control, but this is based on the current screen. To accommodate multiple screens, Android recommends that developers not use specific pixels to represent the size of the control.

3) do not use Absolutelayout (android1.5 is obsolete), you can use relativelayout substitution;

4) provide a suitable size picture for different screens.

Different size of the screen with different size of the picture, Low:medium:high:extra-high picture size ratio of 3:4:6:8; For example, for medium density (medium) screen your picture pixel size is 48x48, then low density The picture size of the screen should be 36x36, and the height (high) is 72x72,extra-high for 96x96.

Using 9-patch PNG pictures

When using the picture resource, if there is a stretch, because the picture processing reason, will deform, cause the interface shape. The 9-patch PNG image is also a standard PGN image, vacated by a pixel interval around the native PNG image, to identify which parts of the PNG picture can be stretched, which cannot be stretched, the border position on the background, and so on.

"Top, left" defines an extruded area

"Right, bottom" defines the display area, and if you use a full-filled background map, it is recommended that you do not set the margin by android:padding, but by 9-patch way.

The Android SDK provides tools to edit 9-patch images, Draw9patch.bat in the tools directory to instantly see the edited stretch, or edit directly with other image editing tools, but no effect is visible.

Android screen Adaptation principle

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.