Android Size unit conversion and screen adaptation related

Source: Internet
Author: User

One: Android screen Adapter

As is known to all, the Android model has a variety of sizes, so screen adaptation becomes an important part of Android development. Android Screen Adapter Maybe some developers will encounter this problem, today to share the next screen adaptation, in fact, the Android screen can be very easy to match.

Basic concepts

The Android screen fits some concepts that you must understand:

    • Px

is the abbreviation for the English word pixel, which means pixels, dots on the screen. The resolution we usually refer to as 480x800 is the pixel.

In the field of design, pixels are the smallest unit used to calculate digital imagery. The images displayed in the computer are not composed of contiguous lines, but are made up of a number of invisible dots. If you enlarge the image several times, you will find that these continuous tones are actually composed of a number of small dots of similar color, which are the smallest unit "pixels" that make up the image. Since it is the smallest independent display unit, PX is an integer and does not appear to be 0.5px. Such as:

Look at this brightly coloured LED light (original size)

Can you imagine this is what he really is? (after zooming in)

    • Inch

Represents inches, which is the physical size of the screen. Equal to 2.54 centimeters per inch. For example, we often say that the size of the mobile phone screen, 5 (English), 4 (English) inches refers to this unit. These dimensions are the diagonal length of the screen. If the phone's screen is 4 inches, the diagonal length of the phone's screen (viewable area) is 4 X 2.54 = 10.16 centimeters.

    • Dpi

DPI is the abbreviation for dots per inch, in dots, which contains the number of pixels per inch. such as the 320x480 resolution of the phone, 2 inches wide, 3 inches high, the number of pixels per inch is 320/2=160dpi (landscape) or 480/3=160dpi (portrait), 160 is the dpi of the phone, landscape and portrait of this value is the same, The reason is that most phone screens use a square pixel point.

    • Density

Screen density, density and dpi are density = dpi/160

    • Dp

Also known as dip, device independent pixel, devices independent pixels abbreviation, Android Special Unit, on screen density dpi = 160 screen, 1DP = 1px.

    • Sp

and DP very similar, generally used to set the font size, and DP is the difference is that it can be based on the user's font size preference to zoom.

Android drawable

When we create a new Android project, we should be able to see many drawable folders that correspond to different dpi

    • DRAWABLE-LDPI (dpi=120, density=0.75)

    • DRAWABLE-MDPI (dpi=160, Density=1)

    • DRAWABLE-HDPI (dpi=240, density=1.5)

    • DRAWABLE-XHDPI (dpi=320, density=2)

    • DRAWABLE-XXHDPI (dpi=480, density=3)

Some of the most Android tutorials on the market are teaching for each DPI is a set of picture resources, this is a solution, but also a very stupid method, for art or design to add a lot of work not to say, but also will make your APK package is very big. So is there any good way to ensure that the screen is adaptable, but also to minimize the use of design resources, and preferably only a set of DPI image resources? Here's how to summarize the project.

First of all, it must be clear that an automatic rendering concept, the Android SDK will automatically screen size to select the corresponding resource file for rendering, such as the SDK detects that your phone DPI is 160 will take precedence to the drawable-mdpi folder to find the corresponding image resources, attention is only a priority, Suppose your phone dpi is 160, but you only have the corresponding picture resource file under the Xhpdi folder, the program can run as normal. So theoretically only need to provide a specification of the picture resources is OK, if only to provide ldpi specifications of the picture, for large-resolution phone if the picture will not be clear, so need to provide a set of the maximum DPI you need to support the picture, so that even if the user's phone resolution is very small, So the picture shrinking is still very clear.

XHDPI become the first choice

Above said that only need to provide a large set of DPI picture is OK, now the market resolution of the largest mobile phone can reach 1080x1920 resolution, such as nexus5,dpi belongs to xxhdpi, but after all, not universal, At present, the most popular high-end machine in the market resolution is more concentrated in the 720x1080 range, that is, more concentrated in the xhdpi, so at present, xhpdi rules of the picture became the first choice. Of course, with the development of technical specifications, the future may be the market xxdpi mobile phones will become more and more common, but this is something.

What about design resource constraints?

In today's app development, there will basically be iOS and Android versions, some companies to keep the different versions of the app to interact with each other, and some of the company's design resources may be more nervous, in these cases, iOS and Android version is basically a designer-led, In most cases, designers may be more likely to design on the iphone, including late-transduction and the like. At this time, as an Android developer, do you also ask the designer to cut a single set of picture resources for Android? This will break your designers down and let's tell a better way to summarize the project.

It is believed that designers will generally use the latest IPhone5 (5s and 5 size and resolution are the same) to do the prototype design, and iPhone5 screen resolution of 640x1164, screen size of 4 inches, according to the Pythagorean theorem (a^2 + b^2 = c^2) 640^2+1164^2= 1764496, and then open the square root of the screen to find the resolution of the diagonal: 1328, divided by 4 can be obtained iphone5 dpi:1328/4≈332 can be seen IPhone5 screen dpi is about 320, just belong to XHDPI, So you can be very proud like your designers say not specifically for the Android side transduction, directly to the iphone's set of cut picture resources into the drawable-xhdpi folder is OK.

Wrap_content VS DP

Wrap_content and DP are often used in Android development, and then they are related.

Assuming you read this article are unified with XHDPI resources, then you use wrap_content completely no problem, Android will automatically for other specifications of the DPI screen adaptation, such as you put a 120x120px-size picture in xhdpi, Then on the hdpi screen is only 120/1.5=80px size, but if you accidentally also put the picture into the mdpi, this time with the wrap_content display will have a problem, specifically see the following example:

For example, if you just drop the test picture in the drawable_xhdpi folder, xhdpi device will go to xhdpi folder to find the test picture and directly display, and MDPI device first will go to mdpi folder to find the test picture, but did not find, Finally found under the Xhdpi folder, and then automatically based on the density calculation and zoom display, the actual size of the display is 120/2=60px, so the overall display scale will look more normal

    • mdpi

    • xhdpi

But if you put the same picture in the MDPI folder, then the MDPI device will go directly to the MDPI folder to find the test picture, and directly display, and this time the display will not scale, the actual display size is 120x120, on the mdpi screen will look larger,

Through the whole process above, we should understand the entire process of Android loading resources, Wrap_content can also use DP to replace, take the above example, in the xhdpi folder put a 120x120 pixel test picture, The width of the high directly in addition to the density to derive the DP value, that is, in this case the following code is equivalent.

<ImageView    android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/test" />
<ImageView    android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/test" />
Summarize

I believe that through the above explanation, some basic concepts in the Android UI have a good understanding of the actual development work there are some efficient methods can be consulted, should be able to deal with most of the screen adaptation work. However, there are still some special adaptation requirements in the project that cannot be met and will be explained in the future for some special needs.

Two:

Android Size unit conversion and screen adaptation related

The meaning of units of various sizes  DP:density-independent PixelsAn abstract unit, based on the physical density of the screen. (DP and dip have the same meaning, so you don't have to treat it differently). These units are relative to160dpi (dots per inch)The screen says,On the 160dpi screen, 1DP is roughly equal to 1px. When running on a higher-density screen, the number of pixels to draw 1dp is magnified by a scale that is associated with screen density (dpi).   Similarly, on a low-density screen, the number of pixels shrinks by a percentage.  This ratio of DP to PX will vary with the density of the screen rather than the direct proportional relationship.  Using a DP unit instead of PX is a simple screen density adaptation solution. In other words, it provides a way to maintain true dimensional consistency across multiple devices.SP:scale-independent PixelsThis is a bit like a DP unit, but it also scales the dimensions based on the user's font settings (font preference). SuggestionsUse this size unit to label the font size, so they will be adjusted for screen density and user settings.PTPoints 1/72 inch (inch), depending on the physical size of the screen.px:PixelsCorresponding to the real pixel.  This unit is not recommended, because the actual expression will vary greatly depending on the device. The number of pixels per inch on each device is different (the density is different) and the total pixel count on the screen is different (the overall size is different).mm:millimetersResource type

Picture files are usually saved in multiple folders, and the suffix names of the multiple folders actually represent different screen densities. On the basis of M, the screen density (dots per inchThe ratio of the baseline to the size of the image resource required is as followsl:low Density (120dpi) 0.75    m:medium Density (160dpi) 1.0 Baseline    h:high Density (240dpi) 1.5    x:extra-high Density (320dpi) 2.0xx: Extra-extra-high density (480dpi)Dimension Unit Conversion Tool class

You can write tool classes to convert dimension units, such as:

Package Com.mengdd.dimen;import Android.content.context;public class Dimenutils {public    static int sp2px (Context Context, float spvalue) {        float Fontscale = context.getresources (). Getdisplaymetrics (). scaleddensity;        return (int) (Spvalue * fontscale + 0.5f);    }    public static int PX2SP (context context, float Pxvalue) {        float Fontscale = context.getresources (). getdisplaymetrics (). scaleddensity;        return (int) (Pxvalue/fontscale + 0.5f);    }    public static int dip2px (context context, Int. Dipvalue) {        final float scale = context.getresources (). getdisplaymetric S (). density;        return (int) (Dipvalue * scale + 0.5f);    }    public static int Px2dip (context context, float Pxvalue) {        final float scale = context.getresources (). Getdisplaymetr ICS (). density;        return (int) (Pxvalue/scale + 0.5f);}    }


The Displaymetrics in Android describes a variety of information about the display, which can be used to view the state of the device, as well as the constants of the standard for screen density as seen in this class.

The ToString () method of Displaymetrics is as follows:

    @Override public    String toString () {        return "displaymetrics{density=" + density + ", width=" + Widthpixels +            " , height= "+ Heightpixels +", scaleddensity= "+ scaleddensity +            ", xdpi= "+ xdpi +", ydpi= "+ ydpi +"} ";    }

Each of these variables is explained as follows:

    /** * The absolute width of the display in pixels.    */public int widthpixels;     /** * The absolute height of the display in pixels.    */public int heightpixels;  /** * The logical density of the display. This was a scaling factor for the * Density independent Pixel Unit, where one DIP was one Pixel on an * Approximatel Y-Example a 240x320, 1.5 "x2" screen), * Providing the baseline of the system ' s display. Thus on a 160dpi screens * This density value would be 1; On a-DPI screen it would is. 75;     etc. * * <p>this value does not exactly follow the real screen size (as given by * {@link #xdpi} and {@link #y  DPI}, but rather are used to scale the size of * The overall UI in steps based on gross changes in the display DPI. For * example, a 240x320 screens would have a density of 1 even if their width is * 1.8 ", 1.3", etc. However, if the screen resolution was increased to * 320x480 but the Screen size remained 1.5 "x2" then the density would is * increased (probably to 1.5).    * * @see #DENSITY_DEFAULT */public float DENSITY;  /** * The screen density expressed as dots-per-inch.     May is either * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.    */public int densitydpi;  /** * A scaling factor for fonts displayed on the display.  This is the same * as {@link #density}, except that it could be adjusted in smaller * increments at runtime based on     A user preference for the font size.    */public float scaleddensity;     /** * The exact physical pixels per inch of the screen in the X dimension.    */public float xdpi;     /** * The exact physical pixels per inch of the screen in the Y dimension. */public float ydpi;

Actual device Parameters  

  Xiaomi 2S, the toString () method output inDisplaymetrics is as follows:

displaymetrics{density=2.0, width=720, height=1280, scaleddensity=2.0, xdpi=345.0566, ydpi=342.23157}

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.