About Android UI Layout adaptive

Source: Internet
Author: User

Originally thought that the last article was written before the Lunar New Year, but it seems that there is still a need to continue to see the useful records, a relatively basic, not much attention before.

Back to the point, this article mainly on the adaptation of Android a Little thing, why will suddenly think of this, mainly because of the previous look at launcher code, which saw this code is always not understand, so checked the next information. Look at the code,

Bitmapdrawable bitmapdrawable = (bitmapdrawable) icon; Bitmap Bitmap = Bitmapdrawable.getbitmap (); if (bitmap.getdensity () = = Bitmap.density_none) { Bitmapdrawable.settargetdensity (Context.getresources (). Getdisplaymetrics ());

The above code is derived from Launcher2 source Utilities.java in the static Bitmap Createiconbitmap (drawable icon, context context), So first of all, to see the meaning of this parameter bitmap.density_none, the farmer uncle's translation instructions, you can know that the description of this parameter is this: marked the bitmap is created with unknown pixel density. So it's easy to guess what the following code means, just that if you create a bitmap by an unknown pixel density, the pixel density of the bitmapdrawable is set to the pixel density of the current screen, that is, the pixel density of the bitmap that is created when it is displayed is the current screen pixel density.

But for displaymetrics I know very little, basically can say only know is to get screen resolution, so rushed to check information.

Here is the API's original description, I have directly posted

Android.util class Displaymetrics Java.lang.Object successor Android.util.DisplayMetrics public class Displaymetrics  Extends Object a structure describing general information on a display, such as its size, density, and font scaling. Field summary static intdefault_density the reference density used throughout t          The He system.          Floatdensity The logical density of the display.          Intheightpixels the absolute height of the display in pixels.          Floatscaleddensity A scaling factor for fonts displayed on the display.          Intwidthpixels the absolute width of the display in pixels.          floatxdpi the exact physical pixels per inch of the screen in the X dimension. floatydpi the exact physical pixels per inch of the screen in the Y dimension. Construction Method Summary Displaymetrics () method summary Voidsetto (DisPlaymetrics o) voidsettodefaults () The method inherited from class Java.lang.Object equals, Getclas S, Hashcode, notify, Notifyall, toString, wait, wait, wait field details default_density public static final in          T default_density the reference DENSITY used throughout the system. See also: Constant field value Widthpixels          public int Widthpixels The absolute width of the display in pixels.          Heightpixels public int Heightpixels The absolute height of the display in pixels. Density public float density 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 approximately-dpi SC Reen (for 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 106 DPI screens it would be. 75;                etc. ThisValue does not exactly follow the real screen size (as given by xdpi and ydpi, but rather was used to scale the size of the Overall UI in steps based to 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 are increased to 320x480 but the screens size remained 1.5 "x2" then the density would be I                Ncreased (probably to 1.5). See also: default_density scaleddensity public float scaleddensity A scaling factor for fonts displayed on the display. This was the same as density, except that it could be adjusted in smaller increments at runtime based on a user preference fo           R the font size.           xdpi Public float xdpi The exact physical pixels per inch of the screen in the X dimension. ydpi Public float ydpi The exact physical pixels per inch of the screen in the Y dimension. Construction method Details DisplAymetrics public Displaymetrics () method details Setto public void Setto (Displaymetrics o) Settodefaults public void Settodefaults ()
The format is not good enough to look at, because just as an understanding of the content, so just look at a probably sufficient. My understanding is that through the Displaymetrics class we can get most of the information about the screen display, the specific way should be more flexible, such as the first piece of code to paste

Directly with Context.getresource (). Getdisplaymetrics (), you can also initialize a Displaymetrics object, and then get the

Displaymetrics metrics = new Displaymetrics ();

Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics);

The general recommendation is to use the latter because the considered usage is to invoke in the application, so that the wide height obtained is the width of the screen, but if the first call method is also called in the activity's OnCreate function at the same time, and is called before Setcontentview (), It is likely that the resource files have not yet been loaded, so calling GetResource is likely to cause problems. Therefore, it is recommended that the second one, because the window object must have already been created since the need to initialize the activity, and now the call to Getwindowmanager is not a problem, the natural data obtained is correct. The first one can be recommended for acitivity after the creation of a success.

The simple code is as follows

protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);D isplaymetrics metrics = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics); metrics.tostring (); LOG.E ("Mylog----------", metrics.tostring ());
The following is the simulator demo log

You can see the information printed in log, density = 1.0 width = 1024,height = 552 Scaleddensity = 1.0 xdpi = ydpi = 160 because I set the simulator resolution to 1024*600, specifically will be 48 height value, I personally think is the height of the following navigation_bar, but also a bit strange why not counted.

The log below is the log I took on the real machine, which is normal.

E/mylog----------(29597): displaymetrics{density=1.0, width=1024, height=600, scaleddensity=1.0, xdpi=160.0, ydpi= 160.0}

Simply look at the API's description, not very understand, just about to understand density this value is a logical display density, and is a separate pixel density of the proportional number of units, its standard value is 160 pixels per square inch, that is, the 160dpi. And this value does not change exactly according to the actual size of the screen, like a screen with a resolution of 240*320, its density is 1, but its size width can be 1.8 inches or 1.3 inches. As you can see, for the pixel density ratio This parameter should only depend on the actual screen pixel density, once the value change is no longer 1, then the actual layout UI if the actual pixel values in the layout of the units will show a different layout effect at different resolutions, Unable to accommodate multiple resolutions. Then Android is also directly behind the resolution-independent units of measure to support these adaptive layouts

px: The direct description of the pixel directly corresponds to the actual resolution of the picture. That is, an actual physical pixel point on the 1px screen. Official not recommended, because the display effect at different resolutions will vary, can not be automatically adjusted to the actual screen pixel density

DP: This is the official recommended unit that can be adjusted to screen pixel density. There is an explanation on the Internet, I personally think the following explanation is the most reasonable. It's about pixel density, so let's first explain what pixel density is. Given a cell phone with a physical size of 1.5 inches x2 inches and a screen resolution of 240x320, we can calculate the number of pixels per inch on the screen of this phone as 240/1.5=160dpi (landscape) or 320/2=160dpi (portrait), 160DPI is the pixel density of this phone, and the pixel density unit dpi is the abbreviation for dots per inch, which is the number of pixels.  This value is the same for both landscape and portrait, because most phone screens use a square pixel point. Different cell phones/tablets may have different pixel densities, such as the same as 4-inch mobile phones, with 480x320 resolution also has 800x480 resolution, the former pixel density is relatively low. The Android system defines four pixel densities: low (120DPI), Medium (160DPI), High (240dpi), and Superelevation (320DPI), and their corresponding DP-to-PX coefficients are 0.75, 1, 1.5, and 2, which are multiplied by the DP length as the number of pixels. For example, the interface has a length of "80dp" picture, then it is actually displayed as 80x1.5=120px on the 240dpi mobile phone, on the 320dpi phone actually displayed as 80x2=160px. If you compare these two phones together, you'll find that the physical size of the picture is "almost", which is the effect of using DP as a unit.

Dip: Exactly the same as DP, except the name is different. The dip was used in earlier versions of Android, and the DP name was later recommended for unification with SP.

sp: abstract pixels independent of scale (scale-independent? Pixel). The SP and DP are similar but the only difference is that the Android system allows users to customize text size (small, normal, large, oversized, and so on), 1sp=1dp=0.00625 inches when the text size is "normal", and when the text size is "large" or "oversized", 1sp>1dp= 0.00625 inches. Similar to what we did after adjusting the font size in Windows-the window size is constant, only the text size changes.

There are a few smaller units of size:

mm: mm; In: That is, inches, 1 inches = 2.54 cm (approx);

PT: 1pt=1/72 inch = 0.035 cm; Best practice, the size of the text is used in SP units, non-text size are used DP units. For example textsize= "16sp", layout_width= "60DP", occasionally need to use PX units, such as the need to draw a thin line on the screen when the divider.

Through the above understanding, but also probably know the Android UI layout of the pixel units of some of the setting principle, then the official recommended for fixed length width needs to be set as far as possible with the pixel density related to the DP units to be set, and if you want to use the actual layout of the control actual length and width to do adaptive, Then for the long-width setting should be as far as possible to use wrap_content to set, instead of forcing the specified fixed length and width value even if the specified DP units, it is difficult to perfectly display the advantages of adaptive. But we can also according to the characteristics of their own application, appropriate to use PX and other units to make their own application more convenient and more beautiful layout.

Summed up the above, the actual to me is very intuitive to my attention or I do the application layout, the comparison design I made the actual effect of the total feeling that the image size is adaptive or directly assigned to the actual image resolution of the same DP value, the effect is different from the design diagram, has always been that the DP and pixels are equivalent, So I have been thinking about, until now finally understand that our DP value only in the density is 1 when the effect will be 1dp=1px, when this value changes will cause the actual design effect and actual presentation between the effect of the discrepancy, Then the solution is probably to specify the fixed pixel value px directly to meet our single design.

The above understanding is the individual understanding, hoped that has the research to be correct, the understanding is indefinitely.

About Android UI Layout adaptive

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.