gpt4 book ai didi

android - getDisplayMetrics().density 与 dp

转载 作者:太空狗 更新时间:2023-10-29 13:26:52 32 4
gpt4 key购买 nike

这两个在数学方面做的一样吗?它们产生相同的结果吗?

Java:

pixel=10;
margin=px * getResources().getDisplayMetrics().density;
layout_param.topMargin=margin;

XML:

android:layout_marginTop="10dp"

最佳答案

是的,它们是,getDisplayMetrics() 中的密度是用于 documentation 之后的 dip 单元的密度:

public float density

Added in API level 1 The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.

This value does not exactly follow the real screen size (as given by xdpi and ydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the density would be increased (probably to 1.5).

关于android - getDisplayMetrics().density 与 dp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20546931/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com