# 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

`Private static final Float scale = mcontext.getresources (). Getdisplaymetrics (). density;private Static Final float Scaleddensity = MContext.mContext.getResources (). Getdisplaymetrics (). scaleddensity;/** * DP turn into PX * @param dipvalue * @ return */public static int dip2px (float dipvalue) {return (int) (Dipvalue * scale + 0.5f);} /** * px turns into DP * @param pxvalue * @return */public static int Px2dip (float pxvalue) {return (int) (Pxvalue/scale + 0.5f);} /** * SP turns into px * @param spvalue * @param type * @return */public static float sp2px (float spvalue, int type) {switch (type) {Case Chinese:return Spvalue * scaleddensity;case number_or_character:return spvalue * scaleddensity * 10.0F/18.0F;DEFAU Lt:return spvalue * scaleddensity;}}`

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

`Android.view.ViewGroup.LayoutParams.heightandroid.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.
`int heightpx= displayutil.dip2px (this,), 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:

`float height = this.getresources (). Getdimension (r.dimen.height); 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:

`<dimen name= "activity_vertical_margin1" >16dp</dimen>  <dimen name= "Activity_vertical_margin2" >16px</dimen>  `

Code:

`Float a1=getresources (). Getdimension (R.dimen.activity_vertical_margin1);  int a2=getresources (). Getdimensionpixeloffset (R.dimen.activity_vertical_margin1);  int a3=getresources (). Getdimensionpixelsize (R.dimen.activity_vertical_margin1);  Float b1=getresources (). Getdimension (r.dimen.activity_vertical_margin2);  int b2=getresources (). Getdimensionpixeloffset (r.dimen.activity_vertical_margin2);  int b3=getresources (). Getdimensionpixelsize (r.dimen.activity_vertical_margin3);  Float c1=getresources (). Getdimension (r.dimen.activity_vertical_margin3);  int c2=getresources (). Getdimensionpixeloffset (r.dimen.activity_vertical_margin3);  int c3=getresources (). Getdimensionpixelsize (r.dimen.activity_vertical_margin3);  LOG.I ("Test", "getdimension=" +a1+ ", getdimensionpixeloffset=" +a2+ ", getdimensionpixelsize=" +a3 ");  LOG.I ("Test", "getdimension=" +b1+ ", getdimensionpixeloffset=" +b2+ ", getdimensionpixelsize=" +b3 "); 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 ~ ~)

DP,PX,SP Concept grooming in Android

Related Keywords:
Related Article

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.