Android unit conversion, android unit

Source: Internet
Author: User

Android unit conversion, android unit

PT-> SP

According to the applyDimension method in TypedValue. java:

public static float applyDimension(int unit, float value,                                       DisplayMetrics metrics)    {        switch (unit) {        case COMPLEX_UNIT_PX:            return value;       case COMPLEX_UNIT_DIP:            return value * metrics.density;       case COMPLEX_UNIT_SP:            return value * metrics.scaledDensity;        case COMPLEX_UNIT_PT:            return value * metrics.xdpi * (1.0f/72);        case COMPLEX_UNIT_IN:            return value * metrics.xdpi;        case COMPLEX_UNIT_MM:            return value * metrics.xdpi * (1.0f/25.4f);        }        return 0;    }


Convert COMPLEX_UNIT_PT to COMPLEX_UNIT_SP:

Value * metrics. xdpi * (1.0f/72)/metrics. scaledDensity = value * metrics. xdpi/(72 * metrics. scaledDensity ))

So 1pt = metrics. xdpi/(72 * metrics. scaledDensity) sp

(Xdpi and metrics. scaledDensity can be obtained from the system. Note that the pt of the ui annotation must be converted to the system's corresponding pt. It depends on the size of the ui annotation)

The value depends on the machine, which is roughly equal to 2.22, and some machines are 2.15.


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.