PX,DP,SP,PT analysis and conversion of common unit of measurement in New ui-

Source: Internet
Author: User

PX,DP,SP,PT analysis and conversion of common unit of measurement in New ui-

--Reprint Please indicate source: Coder-pig, welcome reprint, do not use for commercial use!


Piglet Android Development Exchange Group has been established, welcome everyone to join, both novice, rookie, Big God can, piglet a person's

Strength is limited after all, the writing out of things will certainly have a lot of flaws, welcome to point out, brainstorm, let the pig's blog

More detailed, help more people, O (∩_∩) o Thank you!

Piglet Android Development Exchange Group: Piglet Android Development Exchange Group Group number: 421858269

new Android UI Instance Daquan directory: http://blog.csdn.net/coder_pig/article/details/42145907



The text of this section:

for units of measurement, we are habitually divided into: relative units and absolute units , from the word meaning we can

guess, the relative can be based on different scenarios to show different sizes, commonly used are: DP,PX,SP ;

and absolute units is that we have developed a standard, such as mm (mm), write dead, 1mm so much,

under what circumstances are so many, and commonly used to have , in (Inch, 2.54cm), pt (LB) printing industry commonly used

Unit, 1 pt = 1/72 in! So, what we are going to discuss today is relative unit: PX,DP,SP;

I believe you are not unfamiliar with them, such as writing to death a imageview of the width of the high; margin how many DP

Set the offset, draw a 1px wide view to form a straight line, and so on. Then this section will come

1. Study the concept of PX,DP,SP

2. Units to convert each other, write a simple tool class

3. Why do I use DP instead of PX for layout?

All right, let's start with this section!



s) The concept of PX,DP,SP:





2) units to convert each other, write a simple tool class

/** * This tool class is used for: PX,DP,SP conversion * @author Coder-pig * */public class Displayutil {//1.px to dppublic static int Px2dip (Context con         Text, float pxvalue) {final float scale = context.getresources (). Getdisplaymetrics (). density;     return (int) (Pxvalue/scale + 0.5f); }//2.DP to px public static int dip2px (context context, float Dipvalue) {final float scale = Context.getr         Esources (). Getdisplaymetrics (). density;     return (int) (Dipvalue * scale + 0.5f); }//3.px to SP public static int PX2SP (context context, float Pxvalue) {final float Fontscale = Context.get         Resources (). Getdisplaymetrics (). scaleddensity;     return (int) (Pxvalue/fontscale + 0.5f); }//4.SP to px public static int sp2px (context context, float Spvalue) {final float Fontscale = context.ge         Tresources (). Getdisplaymetrics (). scaleddensity;     return (int) (Spvalue * fontscale + 0.5f); } }


3. Why do I use DP instead of PX for layout?

Answer: in the previous section we already know the screen density dpi this concept, although the same size of the phone, but their

DPI can be different, DPI is the number of pixels inside the unit length, then the dpi of the smaller phones, pixels will be

Larger than the DPI of the cell phone pixel, then if we use PX, the large DPI phone may appear to be normal,

However, the smaller DPI phone may appear misaligned, the interface is far from the expected effect, and may exceed the boundary!px can

is understood as physical pixels, and DP is a virtual pixel, using DP can solve these problems, so we are still accustomed to

Use DP!!! When layout







Reference documents:

Http://www.cnblogs.com/yaozhongxiao/archive/2014/07/14/3842908.html






PX,DP,SP,PT analysis and conversion of common unit of measurement in New ui-

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.