前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LCD的DIP是什么

LCD的DIP是什么

作者头像
233333
发布2021-03-08 22:00:44
6900
发布2021-03-08 22:00:44
举报

是Density independent pixel的缩写,指的是抽象意义上的像素。跟设备的屏幕密度有关系。

  它是Android里的一个单位,dip和dp是一样的。

Google的官方说明是这样的:

Density-independent pixel (dp)

  A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

  就是说在160dpi的屏幕上,1dip=1px。   它跟屏幕密度有关,如果屏幕密度大,1dip代表的px就多,比如在320dpi的屏幕上,1dip=2px。

为什么我们在布局的时候最好要用dip,不要用px?   是因为这个世界上存在着很多不同屏幕密度的手机,屏幕密度是什么?就是dpi,就是单位长度里的像素数量。

  想象一下,如果这些手机的尺寸一样,屏幕密度相差很大,那么是不是说一个手机水平方向上像素很少,另一个手机水平方向上像素很多?那我们画同样pix数量的时候,它显

  示的长度不就会不一样了?

  比如下面图中的两个手机,同时设置2px长度的Button,在屏幕密度较高的手机里就会显示的比较小。

  而同时设置的2dip长度的Button,在两个手机上显示的大小是一样的。

image
image

所以如果你在App布局中都用的px作为单位,那么你的App跑在各个设备上就会出现奇奇怪怪的现象了。

来看一下emulator上的效果,我定义了两个Button,分别用px和dip做单位。

布局文件里这样写

代码语言:javascript
复制
<Button android:layout_width="100px"
android:layout_height="100px"
android:text="@string/str_button1"/>

<Button android:layout_width="100dip"
android:layout_height="100dip" 
android:text="@string/str_button1"/>

显示的界面是这样的:

image
image

getResources().getDisplayMetrics().densityDpi 就是屏幕密度。

getResources().getDisplayMetrics().density 也可以理解为1dip相当于多少个px啦。

上面的dpi是240,1dip=1.5px

你看,100dip的Button是100pxButton的1.5倍长吧。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-03-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档