为什么要引入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

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

—程序员控制

两种主要控制方法
—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).

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

程序设置

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

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

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

设计指导
—尽量使用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);

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

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zouxueping/archive/2010/05/18/5605332.aspx

更多相关文章

  1. 在android 中使用og4j
  2. ❤️烦恼?头疼?不知所措?Android的ANR问题,一剂药到病除❤️【建议收藏】
  3. 每日一道Android(安卓)面试题,面试途中不败题
  4. Android使用XML全攻略(2)
  5. 使用android快速开发框架afinal 开发android应用程序
  6. Android(安卓)页面惯性回弹效果,Nested接口接口简单实现。
  7. 想抢先体验Android操作系统的魅力吗?那就使用Android(安卓)LiveCD
  8. Android使用libjpeg实现图片压缩
  9. Android自定义属性与自定义属性的获取

随机推荐

  1. 关于android使用已有id,自己的id和系统id
  2. Android 开发学习小结(七)
  3. 在 Android 模拟器中安装 busybox
  4. 模拟Android多点触控
  5. 我的Android进阶之旅------>Android利用
  6. Android studio获取sha1(调试版和发布版)
  7. Android的TextView组件相关属性
  8. 一个有关于android设置的小问题
  9. Android 模拟器的认识
  10. Android(安卓)TextView使用HTML处理字体