android density

本文详细介绍了DIP(Density Independent Pixel)的概念及其在不同屏幕密度设备上的应用原理。通过实例对比了相同物理尺寸但不同像素密度的设备上按钮大小的表现差异,强调了使用DIP确保用户界面在各种屏幕上的正确显示的重要性。

为什么要引入dip
—The reason for dip to exist is simple enough. Take for instance the T-Mobile G1. It has a pixel resolution of 320x480 pixels. Now image another device, with the same physical screen size, but more pixels, for instance 640x480. This device would have a higher pixel density than the G1.

—If you specify, in your application, a button with a width of 100 pixels, it will look at lot smaller on the 640x480 device than on the 320x480 device. Now, if you specify the width of the button to be 100 dip, the button will appear to have exactly the same size on the two devices.

—The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, the baseline density assumed by the platform (as described later in this document). At run time, the platform transparently handles any scaling of the dip units needed, based on the actual density of the screen in use. The conversion of dip units to screen pixels is simple: pixels = dips * (density / 160). For example, on 240 dpi screen, 1 dip would equal 1.5 physical pixels. Using dip units to define your application's UI is highly recommended, as a way of ensuring proper display of your UI on different screens.

Screen dimensions
 image

跨屏幕的两种方式
—通过系统兼容模式

—程序员控制

两种主要控制方法
—The platform provides a set of resource qualifiers that let you provide size- and density-specific resources, if needed. The qualifiers for size-specific resources are large, normal, and small, and those for density-specific resources are hdpi (high), mdpi (medium), and ldpi (low).

—The platform also provides a element, whose attributes android:largeScreens, android:normalScreens, and android:smallScreens let you specify what generalized screen sizes your application supports. A fourth attribute, android:anyDensity, lets you indicate whether or not your application includes built-in support for multiple densities.

系统做了什么
—加载资源时进行缩放处理(如图片)

—对基于象素的坐标和尺寸进行自动缩放。For instance, suppose a given device is using a WVGA high-denisty screen, which is 480x800 and about the same size as a traditional HVGA screen, but it's running an app that states that it does not support multiple densities. In this case, the system will "lie" to the application when it queries for screen dimensions, and report 320x533. Then, when the app does drawing operations, such as invalidating the rectangle from (10,10) to (100, 100), the system will likewise automatically transform the coordinates by scaling them the appropriate amount, and actually invalidate the region (15,15) to (150, 150).

—在屏幕上开辟一个“模拟器”

程序设置
 image

上图是2.1版的设置画面,各版本的默认参数不大一样,1.6版以上默认全部为true,1.5版默认只有Normal screens一项为真。XML表述如下:
资源限定符
 image

以指定密度启动模拟器
在命令行下运行emulator -avd youravdname -dpi-device 160

不同设置在不同密度下的结果
 image

设计指导
—尽量使用wrap_content、 fill_parent 和dip。对于文字,则应该使用sp,它除了密度外还受用户偏好设置影响。

—避免使用AbsoluteLayout。

—不要直接使用象素。可使用ViewConfiguration取得系统定义的一些常量或带getScaled 前缀的方法。必须直接指定的话,可通过dip转换,如下:

final float scale = getContext().getResources().getDisplayMetrics().density;
mGestureThreshold = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f);

—使用特定密度或特定屏幕资源

 

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/zouxueping/archive/2010/05/18/5605332.aspx

Android 中的像素密度(Density)是一个与设备物理屏幕特性密切相关的概念。它定义了屏幕上每英寸包含的像素数量(通常用 dpi 表示)。像素密度是 Android 系统用来适应不同设备屏幕的重要机制之一,确保了 UI 元素在不同设备上显示的大小和比例保持一致。 Android 系统通过将物理像素(px)抽象为与密度无关的像素(dp 或 dip)来实现这一目标。dp 是一种虚拟像素单位,其大小会根据设备的像素密度进行调整。例如,在 160dpi 的设备上,1 dp 等于 1 px;而在更高或更低的密度设备上,系统会根据比例进行缩放。这种抽象机制使得开发者可以使用 dp 作为 UI 布局的单位,而无需担心不同设备的分辨率差异[^1]。 为了更好地适配不同密度的屏幕,Android 提供了多套资源目录(如 `drawable-mdpi`、`drawable-hdpi`、`drawable-xhdpi` 等),每套资源对应不同的像素密度范围。系统会根据设备的实际密度自动选择合适的资源进行加载。这种机制简化了 UI 适配的过程,并提升了应用在不同设备上的显示效果。 在某些情况下,开发者可能需要手动配置应用的适配参数。例如,在 `AndroidManifest.xml` 文件中,可以通过 `<meta-data>` 标签指定设计稿的宽度和高度(以 dp 为单位),从而实现更精确的布局适配。以下是一个典型的配置示例: ```xml <manifest> <application> ... <meta-data android:name="design_width_in_dp" android:value="360"/> <meta-data android:name="design_height_in_dp" android:value="640"/> ... </application> </manifest> ``` 通过这种方式,可以确保应用在不同设备上按照设计稿的比例进行适配,从而避免 UI 元素因屏幕密度差异而导致的显示异常[^2]。 ### 像素密度的匹配规则 Android 系统定义了一套像素密度的匹配规则,用于选择合适的资源文件。系统会根据设备的实际密度与资源目录的密度进行匹配,优先选择最接近的资源。如果找不到完全匹配的资源,则会使用默认资源(通常位于 `drawable` 或 `values` 目录下)。 ### 总结 理解 Android 中的像素密度概念及其配置方法对于开发高质量的应用至关重要。通过合理使用 dp 单位和多套资源目录,结合 `AndroidManifest.xml` 中的配置,可以有效地实现跨设备的 UI 适配,确保应用在不同屏幕密度下的显示效果一致且美观。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值