Direct relationship between DPI and PX

Source: Internet
Author: User

First, the screen resolution is irrelevant to DPI and PX.
Because there are 45 inch TV screens (5.5 inch) and mobile phone screens)

How many points does 1 inch have? We use density to distinguish

Ldpi 120 DPI 0.75
Mdpi 160 DPI 1 is generally hvga
Hdpi 240 DPI 1.5 is generally WVGA
Xhdpi 320 DPI 2
Xxhdpi 480 DPI 3

On a 160dpi Device
Dip (that is, the dip/dp we use): Device Independent pixel, device independent pixel, 1 inch number of images, we do not directly use PX, but use DP, px = 1dp * density (determined by DPI.

1dip = 1px on the mdpi Device
1dip = 3px on the xxhdpi Device

Try to use DP for the width and height of widgets, and SP for the font, so that the system can adapt according to the proportion.

The reference article also mentions how to fit the size and size of some unconventional devices.
In general, you can first cut the image according to the largest device, and set the widget width and height to wrap_content. The size of the remaining small images can be cut according to the above ratio column.

For some width and height that must be defined, use
Values-mdpi/dimens. xml
Values-hdpi/dimens. xml
Values-xhdpi/dimens. xml
Values-xxhdpi/dimens. xml
Values/dimens. xml
To adapt to different sizes.

Layout of different resolutions:
Layout-640x360, layout-800x480

Conversion between PX, DP, and SP on the mobile phone

Private Static final float scale = mcontext. getresources (). getdisplaymetrics (). density;

Private Static final float scaleddensity = mcontext. mcontext. getresources (). getdisplaymetrics (). scaleddensity;

/**

  • Convert DP to PX
  • @ Param dipvalue
  • @ Return
    */
    Public static int dip2px (float dipvalue ){
    Return (INT) (dipvalue * scale + 0.5f );
    }

/**

  • Convert Px to DP
  • @ Param pxvalue
  • @ Return
    */
    Public static int px2dip (float pxvalue ){
    Return (INT) (pxvalue/scale + 0.5f );
    }

/**

  • Convert SP to 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;
    Default:
    return spValue * scaledDensity;
    }
    }

Reference
Http://www.paibaidu.com/index.php/2014/08/13/966.html

Direct relationship between DPI and PX

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.