【转帖】【Android开发学习笔记】关于AVD中LED Density


http://xy0811.spaces.live.com/Blog/cns!F8AECD2A067A6B17!1508.entry

1.          什么是density

1)         density
density
表示每英寸有多少个显示点(逻辑值),它的单位是dpidot per inch,通常屏幕大时,density就大,屏幕小时,density就小,通常

屏幕实际分辨率为240px*400px时,density=120
屏幕实际分辨率为320px*533px
density=160
屏幕实际分辨率为480px*800pxdensity=240

2)         分辨率
是整个屏是多少点,比如800×480,它是软件的显示单位

3)         Android1.6版本开始支持density(对应API Level 4)
用于解决应用在屏幕大小不同的硬件上正常显示的问题

2.          相关代码及设置

1)         AndroidManifest.xml


这个参数在
API Level 4也就是SDK 1.6以后才启用的,而且1.6版本的API Level 4的应用默认值就是True

2)         资源目录名(android 2.0以后)
res/xxx-hdpi         
density240时,使用此目录下的资源
res/xxx-mdpi        
density160时,使用此目录下的资源
res/xxx-ldpi          
density120时,使用此目录下的资源
res/xxx                 
不常后缀,为默认设置,同xxx-mdpi

3)         资源单位(xml文件中定义大小的单位)

a)        dp=dip=dx (Density independent pixel)
基于屏幕密度的抽象单位,设备无关的点,用于说明与密度无关的尺寸和位置。这些单位是相对于一个160dpi的屏幕,所有一个dp160dpi屏幕上的一个点。

b)         px
px
指软件的单位点,设备相关的点

c)          具体使用

                                       i.              布局时尽量使用单位dip,少使用px
若使用px,如果设某控件大小400x400px,在800×480上显示正常,而在533×320的屏上就超出屏幕了

若使用dp,如果设某控件大小为160x160dp,就可以通过从系统中取density来算出真正的大小,比如在800×480屏的density设为240,而533×320屏的density设为160,借以下公式计算
pixels = dips * (density / 160)
800×480在屏中显示240象素,而在533×320的屏中显示为160象素,控件在屏中显示的比例是一样的。

                                      ii.              内部的处理过程分为三步:
screen中其它元素,转为应用的值,计算应用各控件位置,然后再转成800×480以供真正显示
res-xxx不存在,则读取res有的资源,然后对其做相应缩放

3.          实现density的关键源码

1)         BitmapFactory.java(用于缩放图片)

2)         ComptibilityInfo.java(用于位置计算)

 

 

另外,在网页里可以对不同的density规定不同的CSS,好在我们设备固定,哼

Building web pages to support different screen densities

A screen’s density is based on it’s screen resolution and physical size. A screen with low density has fewer available pixels per inch, where a screen with high density has more — sometimes significantly more — pixels per inch. The density of a screen is important because, other things being equal, a UI element (such as a button) whose height and width are defined in terms of screen pixels will appear larger on the lower density screen and smaller on the higher density screen. For simplicity, Android collapses all actual screen densities into three generalized densities:high, medium, and low.

By default, WebView scales a web page so that it is drawn at a size that matches the default appearance on a medium density screen. So, it applies 1.5x scaling on a high density screen (because its pixels are smaller) and 0.75x scaling on a low density screen (because its pixels are bigger). Starting with API Level 5 (Android 2.0), WebView supports DOM, CSS, and meta tag features to help you (as a web developer) target screens with different screen densities.

Here’s a summary of the features you can use to handle different screen densities:

  • The window.devicePixelRatio DOM property. The value of this property specifies the default scaling factor used for the current device. For example, if the value ofwindow.devicePixelRatio is “1.0”, then the device is considered a medium density (mdpi) device and default scaling is not applied to the web page; if the value is “1.5”, then the device is considered a high density device (hdpi) and the page content is scaled 1.5x; if the value is “0.75”, then the device is considered a low density device (ldpi) and the content is scaled 0.75x. However, if you specify the "target-densitydpi" meta property (discussed below), then you can stop this default scaling behavior.
  • The -webkit-device-pixel-ratio CSS media query. Use this to specify the screen densities for which this style sheet is to be used. The corresponding value should be either “0.75”, “1”, or “1.5”, to indicate that the styles are for devices with low density, medium density, or high density screens, respectively. For example:
     <link rel="stylesheet" media="screen and (-webkit-device-pixel-ratio:1.5)" href="hdpi.css" />

    The hdpi.css stylesheet is only used for devices with a screen pixel ration of 1.5, which is the high density pixel ratio.

  • The target-densitydpi property for the viewport meta tag. You can use this to specify the target density for which the web page is designed, using the following values:
    • device-dpi – Use the device’s native dpi as the target dpi. Default scaling never occurs.
    • high-dpi – Use hdpi as the target dpi. Medium and low density screens scale down as appropriate.
    • medium-dpi – Use mdpi as the target dpi. High density screens scale up and low density screens scale down. This is also the default behavior.
    • low-dpi – Use ldpi as the target dpi. Medium and high density screens scale up as appropriate.
    • <value> – Specify a dpi value to use as the target dpi (accepted values are 70-400).

    Here’s an example meta tag to specify the target density:

    <meta name="viewport" content="target-densitydpi=device-dpi" />

If you want to modify your web page for different densities, by using the -webkit-device-pixel-ratio CSS media query and/or the window.devicePixelRatio DOM property, then you should set the target-densitydpi meta property to device-dpi. This stops Android from performing scaling in your web page and allows you to make the necessary adjustments for each density via CSS and JavaScript.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据