關於android的單位dp,dip

來源:互聯網
上載者:User

摘要        本文介紹了android單位dp,dip的概念,並給出了它的確切含義。概述        Android中,dp是Density-independent Pixels簡寫,而dip則是Density Independent Pixels,實際上指的是一個概念,都是Android的單位。在文檔More Resource Types中有描述:dp        Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.         單位px代表的是像素,它和ds的轉換公式在文檔Supporting Multiple Screens中也有描述: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.        公式[cpp]  px = dp * (dpi / 160)           說明1dp代表了多少個px,下面我們簡單的推導一下這個公式。        首先,dpi是Dots Per Inch的縮寫(wiki),表示一英寸內有多少個點,對於UI介面而言,這個點就是像素(Pixel)。        然後,我們看一下dp的單位:[plain]  像素 = dp的單位 * 像素/英寸           可以看出dp的單位是長度單位英寸,上面dp的定義的最後一句話說明了這一點:In other words, it provides consistency for the real-world sizes of your UI elements across different devices.        最後,我們看一下1dp的值有多大。1dp對應dpi/160個像素,根據dpi的定義,每個像素的長度為1/dpi英寸,合在一起就是1dp=1/160英寸。[cpp]  1dp的長度 = 1 * dpi / 160 * 1 / dpi 英寸 = 1 / 160 英寸   結論        dp等同於dip,它是一個長度單位,1dp=1/160英寸。        dp這個單位的意義在於:你可以根據看到的實際大小,在應用中設定對應的dp值,而使得不同的裝置上看起來一樣大。 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.