search

Home  >  Q&A  >  body text

android-ui - 设计师给的布局和字体尺寸单位为px,在写代码的时候要如何转换成Android用的dp和sp ?

问题如题描述,设计给的尺寸都是px的,而且也不告诉他们在什么尺寸和分辨率的屏幕上做的设计?作为程序员要如何转换单位?

我不是在问px与dp之间如何转换的问题。而是由设计师的px如何转换成写在xml里的dp的问题。

巴扎黑巴扎黑2773 days ago711

reply all(4)I'll reply

  • 阿神

    阿神2017-04-17 18:01:24

    非战斗人员请远离, 现在开撕!

    When you encounter a situation like this, you can only tell your artists (we can’t call them designers here): This can’t be done!!!

    Every time you design an App UI, it must be designed at a mainstream standard resolution. Then you can find the corresponding screen density, and finally you can convert px to dpi.
    If you deviate from this principle, it is a rogue, and in the end you will If the implementation is unrealistic and far from the UI design draft, that is none of your business.

    Reference:
    Device Metrics

    Calculation method:
    After knowing the resolution of the design draft and the screen density that the resolution generally corresponds to, calculate it according to the following formula:

    dpi = px / density

    p.s. Density is the value of the Density column in Device Metrics
    p.p.s. Don’t get into a passionate fight with your artist after reading this hot-blooded reply. If you have something to say, you should talk it over [face covering] [ Give in] 2333

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 18:01:24

    First of all, you need to know what the screen size is in the design drawing
    It is usually 1080x1980 (? or other), usually belongs to xxhdpi, that is, 1dp is equivalent to 3px
    If the screen width of the design drawing is only 480 (or so), then 1dp should be It’s just 1px

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 18:01:24

    1dp is defined as 1px when the screen density value is 160ppi, that is, at mdpi, 1dp = 1px. Taking mdpi as the standard, the density value ratio of these screens is: ldpi: mdpi: hdpi: xhdpi: xxhdpi = 0.75: 1: 1.5: 2: 3; that is, at the density of xhdpi, 1dp=2px; in the case of hdpi, 1dp=1.5px. Other analogies.

    1dp=(screen ppi/160)px

    Original link

    reply
    0
  • PHPz

    PHPz2017-04-17 18:01:24

    public static float px2dp(Context context, float pxVal)

    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (pxVal / scale);
    }

    Conversion method: px ---> dp

    reply
    0
  • Cancelreply