The truth about conversion between DP and SP in typedvalue.applydimension

Source: Internet
Author: User

Recently, I've seen a lot of blog posts about conversion between DP-PX,PX-DP,SP-PX,PX-SP, and the way I used to do it was:

1//Conversion dip for PX  2 public static int Convertdiporpx (context context, int dip) {  3     float scale = CONTEXT.GETRESOURC Es (). Getdisplaymetrics (). density;  4     return (int) (Dip*scale + 0.5f* (dip>=0?1:-1));  5}  6   7//convert PX to dip  8 public static int Convertpxordip (context context, int px) {  9     float scale = con Text.getresources (). Getdisplaymetrics (). density; 10     

Then we see a new way of transforming the code as follows:

1 public static int dp2sp (float dpval) {2     return (int) (Typedvalue.applydimension (Typedvalue.complex_unit_dip, dpval,3             myappliction.getinstance (). Getapplicationcontext (). Getresources (). Getdisplaymetrics ()); 4}5  // ????? 6 public static int SP2DP (float spval) {7     return (int) typedvalue.applydimension (typedvalue.complex_unit_sp, Spval, 8             myappliction.getinstance (). Getapplicationcontext (). Getresources (). Getdisplaymetrics ())); 9}

Yards of farmers to typedvalue full of curiosity, through the search official website to understand this kind

Typedvalue

---android.util.TypedValue
Container for a dynamically typed data value. Primarily used with for Resources holding resource values.

This class is a tool class, and as a dynamic container, it holds some data values, which are mainly values in resource.

Let's take a look: what are the values in resource? Layout, drawable, string, style, Anim, Dimens, menu, colors, IDs some of these values have a direct relationship with the screen fit.

Some methods must be able to read these resource file information, such as:

Getdimension (Displaymetrics metrics)

Then look at the specific method:

applydimension (int unit, float value,displaymetrics metrics)
The first parameter is the unit, the second parameter is the corresponding value, and the third one you know, encapsulates the various property values of the display area.

For the code in applydimension (int unit, float value,displaymetrics metrics) Let's see

1 public  static float applydimension (int unit, float value, 2                                        displaymetrics metrics) 3     {4         switch (unit) {5 case         Complex_unit_px:6             return value, 7 case         Complex_unit_dip:8             return value * metrics.density; 9
   case complex_unit_sp:10             return value * metrics.scaleddensity;11 case         complex_unit_pt:12             return value * metrics.xdpi * (1.0F/72), Case         complex_unit_in:14             return value * metrics.xdpi;15 case         Complex_unit_ Mm:16             return value * metrics.xdpi * (1.0f/25.4f);         }18         return 0;19     }

Where the unit is dip, it is converted to a density * value, which is the pixel value, and the unit SP also converts it to PX value, so this method can be

Dip-->px

sp-->px

So above

Typedvalue.applydimension (typedvalue.complex_unit_sp, value, displaymetrics);
This method must not convert the SP to DP, we judge

DP2SP (50) = 150

SP2DP (50) = 150

CONVERTDIPORPX (50) = 150

Convertpxordip (50) = 17
The actual result of running the code is consistent with the result of the judgment.

Next we continue to analyze

Typedvalue.applydimension (typedvalue.complex_unit_sp, value, displaymetrics);
What is the system intended to do?

View Official notes:

Converts an unpacked complex data value holding a dimension to it final floating point value.

This turns the corresponding value into the point value on the actual screen, which is the pixel value.
If it is typedvalue.complex_unit_dip, multiply the display density density.

In the case of typedvalue.complex_unit_sp, multiply the pixel density by scaleddensity.

We continue to root planing the bottom.

The difference between density and scaleddensity is that

Density:the logical density of the display. Show Density density = dpi/160

Scaleddensity:a scaling factor for fonts displayed on the display. Show font zoom factor = density

Actually the value of both, in order to verify this conclusion we randomly find two machines Xiaomi 2S and Huawei P7, remove density and scaleddensity is consistent, P7 for 3.0, Xiaomi 2S = 2.0

So this paper concludes that the transformation DP-PX,PX-DP,SP-PX,PX-SP

Use the following method:

1//Conversion dip for PX  2 public   static int Convertdiporpx (context context, int dip) {  3       float scale = Context.getre Sources (). Getdisplaymetrics (). density;  4       return (int) (Dip*scale + 0.5f* (dip>=0?1:-1));  5   }  6     7   //convert PX to dip  8 public   static int Convertpxordip (context context, int px) {  9       Float scale = context.getresources (). Getdisplaymetrics (). Density;      return (int) (Px/scale + 0.5f* (px>= 0?1:-1)); One  }  of public   static int sp2px (context context, float Spvalue) {         Fontscale = Context.getr Esources (). Getdisplaymetrics (). scaleddensity;15         return (int) (Spvalue * fontscale + 0.5f);     }17     public static int PX2SP (context context, float Pxvalue) {         float Fontscale = context.getresources (). Getdisplaymetrics (). scaleddensity;20         return (int) (Pxvalue/fontscale + 0.5f);     

If there is any mistake, please correct me.

The truth about conversion between DP and SP in typedvalue.applydimension

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.