DP,PX,SP Concept grooming in Android

Source: Internet
Author: User

Today started my app development, because I have been doing the SDK, so the interface UI is very few, just started to do Android applications, there is no dp,px,sp and other concepts have a deep understanding, only know their transformation, and write a conversion tool class. Android has been doing more than a year, and now to start app development, decided not to encounter a concept, you have to get thorough. OK, let's go to the following topic:


First look at their basic concepts:

PX: Is the pixel point of the screen
DP: An abstract unit based on density, if a 160dpi screen, 1dp=1px
Dip: Equivalent to DP
SP: similar to DP, but also scaled based on user's font size preference (SP is recommended as text unit, other dip)


Through the above knowledge we can see here just understand the relationship between PX and DP can be. So here's a look at the relationship between the two of them:

For the dip and PX relationship, do the following overview:

1). px (pixels) Pixels:

A pixel is generally considered to be the smallest complete sample of an image, which is used more often, especially in web development, where the page is basically a unit of pixels.

2). Dip or DP (device independent pixels):

Device independent pixels-this is related to device hardware, generally we support the phone on a variety of resolutions, such as WVGA, HVGA

And QVGA, will use dip as the unit of length

Here is a look at the phone screen type and density and resolution of the corresponding relationship

QVGA screen density=120 QVGA (240*320)
HVGA screen density=160 HVGA (320*480)
WVGA screen density=240 WVGA (480*800)
WQVGA screen density=120 WQVGA (240*400)


Note: The density value represents the number of display points per inch, and the resolution is two concepts.


Screen resolution information is different under different density, taking 480dip*800dip WVGA (density=240) as an example

1. When density=120

Conversion: Conversion factor =120/240

Screen actual resolution is 240px*400px (two points corresponds to a resolution)

Status bar and title bar height 19px or 25dip

Screen width 400px or 800dip, working area height 211px or 480dip

Screen width 240px or 480dip when vertical screen, working area height 381px or 775dip

2. When density=160

Conversion: Conversion factor =160/240

Screen actual resolution is 320px*533px (3 points corresponds to two resolutions)

Status bar and title bar tall 25px or 25dip

Screen width 533px or 800dip, working area height 295px or 480dip

Screen width 320px or 480dip when vertical screen, working area height 508px or 775dip

3. When density=240

Conversion: Conversion factor =240/240

Screen actual resolution is 480px*800px (one point for one resolution)

Status bar and title bar tall 38px or 25dip

Screen width 800px or 800dip, working area height 442px or 480dip

Screen width 480px or 480dip when vertical screen, working area height 762px or 775dip

We typically define multiple adaptation resource folders (VALUES-XXX,DRAWABLE-XXX, etc.) in a project

DRAWABLE-LDPI: Mobile device with a screen density of 120

DRAWABLE-MDPI: Mobile device with a screen density of 160 (this is baseline, others are based on this, 1DP = 1px on this device)

DRAWABLE-HDPI: Mobile device with a screen density of 240

DRAWABLE-XHDPI: Mobile device with a screen density of 320

DRAWABLE-XXHDPI: Mobile device with a screen density of 480

(values are the same, of course, there is a point to note: values and values-hdpi effect is the same, drawable and drawable-hdpi effect is the same, so generally we will store in these two folders the same value, if two have , suitable for a better)


APK in the resource bundle

Resources that use HDPI tags when the screen is density=240

Resources that use MDPI tags when the screen is density=160

Resources that use LDPI tags when the screen is density=120

Resources that use XHDPI tags when the screen is density=320

Resources that use XXHDPI tags when the screen is density=480

Resources that are not tagged are shared in a variety of resolution cases

So use the unit dip as much as possible in the layout, use less px

DP and PX Conversion formula:
Pixs =dips * (densitydpi/160).
Dips= (pixs*160)/densitydpi

But we also need to have an offset value when we do the conversion in the code: 0.5f

[Java]View Plaincopy
  1. Private Static Final float scale = mcontext.getresources (). Getdisplaymetrics (). density;
  2. Private Static Final float scaleddensity = mContext.mContext.getResources (). Getdisplaymetrics (). scaleddensity;
  3. /**
  4. * DP turn into PX
  5. * @param dipvalue
  6. * @return
  7.  */
  8. Public Static int dip2px (float dipvalue) {
  9. return (int) (Dipvalue * scale + 0. 5f);
  10. }
  11. /**
  12. * px turns into DP
  13. * @param pxvalue
  14. * @return
  15.  */
  16. Public Static int px2dip (float pxvalue) {
  17. return (int) (Pxvalue/scale + 0. 5f);
  18. }
  19. /**
  20. * sp turns into PX
  21. * @param spvalue
  22. * @param type
  23. * @return
  24.  */
  25. Public Static float sp2px (float spvalue, int type) {
  26. Switch (type) {
  27. Case Chinese:
  28. return spvalue * scaleddensity;
  29. Case Number_or_character:
  30. return spvalue * scaleddensity * 0f/0f;
  31. default:
  32. return spvalue * scaleddensity;
  33. }
  34. }

As we can see, the scale here is the global variable defined in this class displaymetrics, in fact this value is the current cell phone's density/160,scaledensity is used for PX and SP conversion between the same scale. Another thing is that there is an offset value for the conversion.

DP This unit may be unfamiliar to people who are web developers, as they are generally using px (pixels)
But now, after starting Android apps and games, it's basically switching to DP action, because it can support multiple resolutions of the phone.


We see the relationship between PX and DP, and the conversion, here is a look at the use of the scene, that is why we use the conversion between them, we in the XML is generally defined in the size of the use of DP units, but sometimes we need to set some space and position in the code:

The following code

[Java]View Plaincopy
    1. Android.view.ViewGroup.LayoutParams.height
    2. Android.view.ViewGroup.LayoutParams.width

The above two properties are in pixels, but in order to be compatible with a variety of resolutions, we need the best dip, and we can call the following code to convert it.
[Java]View Plaincopy
    1. int heightpx= displayutil.dip2px (This, );
    2. Mtabhost.gettabwidget (). Getchildat (i). Getlayoutparams (). height = heightpx;


Of Course we sometimes get the values in the Demen.xml file in the values folder in the code as follows:

[Java]View Plaincopy
    1. float height = this. getresources (). Getdimension (R.dimen.height);
    2. Txt.height = Px2dip ((int) height); //convert height to px

I don't know what he gets is the DP value defined in the Dimens.xml file, so there is also a manual conversion operation, but found that the effect is not the same as we expected, then the value is printed for a look, altogether is twice times, that is, the value obtained by Getdimension method is the value defined in the Dimen.xml file. Twice times, it's not scientific, and then just search, and found that there are three similar methods,

Getdimension

Getdimensionpixeloffset

Getdimensionpixelsize

Their functions are not the same:

Take a look at their differences through an example:

Dimen.xml:

[HTML]View Plaincopy
  1. < dimen name="activity_vertical_margin1">16dp</dimen>
  2. < dimen name="activity_vertical_margin2">16px</dimen >
  3. < dimen name="activity_vertical_margin3">16sp</dimen>

Code:

[Java]View Plaincopy
  1. float a1=getresources (). Getdimension (R.dimen.activity_vertical_margin1);
  2. int a2=getresources (). Getdimensionpixeloffset (R.dimen.activity_vertical_margin1);
  3. int a3=getresources (). Getdimensionpixelsize (R.dimen.activity_vertical_margin1);
  4. float b1=getresources (). Getdimension (r.dimen.activity_vertical_margin2);
  5. int b2=getresources (). Getdimensionpixeloffset (r.dimen.activity_vertical_margin2);
  6. int b3=getresources (). Getdimensionpixelsize (r.dimen.activity_vertical_margin3);
  7. float c1=getresources (). Getdimension (r.dimen.activity_vertical_margin3);
  8. int c2=getresources (). Getdimensionpixeloffset (r.dimen.activity_vertical_margin3);
  9. int c3=getresources (). Getdimensionpixelsize (r.dimen.activity_vertical_margin3);
  10. LOG.I ("test", "getdimension="+a1+", getdimensionpixeloffset="+a2+", Getdimensionpixelsize= "+a3);
  11. LOG.I ("test", "getdimension="+b1+", getdimensionpixeloffset="+b2+", Getdimensionpixelsize= "+b3);
  12. LOG.I ("test", "getdimension="+c1+", getdimensionpixeloffset="+c2+", Getdimensionpixelsize= "+c3);


For device 1 (1280*720,160dpi,density=1.0):

Printing results:


For device 2 (480*800,240dpi,density=1.5):

Printing results:


Visible getdimension and Getdimensionpixeloffset function almost, are to get a value of a dimen, if it is a DP or SP unit, multiply it by density, if it is PX, do not multiply; the difference between two functions is a return float , an int is returned.
Getdimensionpixelsize is multiplied by denstiy, whether it is a DP or an SP or px.

So we don't need to convert when we use the Getdimension method to get the value.


Summary: The relevant knowledge of DP and PX is introduced here, this is very easy to confuse, and in the interview and written test will be encountered, so these concepts are still clear better ~ ~

(PS: Actually finished, I still do not quite remember to live ~ ~)






From for notes (Wiz)

DP,PX,SP Concept grooming in Android

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.