【转帖】【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.

阿西bug2

其实不是上一篇的续集……

胜似上一篇的续集……

好吧鉴于写的太技术流了观众表示看不懂,我就不扯技术了

今天加了个新功能,导致我大规模的改了代码以及数据库,波及面真广……郁闷

于是调了几个小时,顺便检查出了以前没发现的一个bug——造成原因是我脑子果然还是不够用

不过好在是熄灯前搞定了

于是我的代码完成度真的已经非常高了……小齐你的CSS进度啊…………急死我了…………

—————————————————明天开学了分割线—————————————————

啊其实我今天写作业已经找到上学的感觉了= =无压力表示

明天早晨是C++果断不去,然后是英语果断不去会死

下午复变,额复变是什么……

————————————————学生会方面分割线—————————————————

今天晚上去宣传部开会,去了十个人,以这个时间段大家的积极性+时间来说已经不错了嗯

开会效率依然很高,于是之后要准备搞搞宣传部的拿手好戏咯~手工活= =

哎,为啥到我手里宣传部就变成了这么特色的组织了呢,sigh~不过至少比枯燥的PS好得多吧嗯

明天去开部长级会议,各种打报告

阿西bug

最近bug微多~微多~但是改进很大嘛

然后就是出ui之后再看自己写的东西就是感觉不一样啊挥拳!!没有css状态下真是太提不起兴趣了!

成就感都不如有css多……

100924进度报告

其实最近一次的update是100920,但是放假的时候比较没干活所以现在才想起来记录下

100920修正了脑子不够引起的重大的弱智错误,好吧,真的脑子不够,好在及时发现。不过也说明了测试设备的必要性。于是我也在考虑之后要如何进行压力测试……以及我究竟需要个什么样的server……说实话我想试试入门级服务器而不是组装机。ECC内存什么的到底用起来给不给力嘛~

说起压力测试什么的= =这种小型的东西真的需要压力测试么……同时处理百个请求是不是属于“压力不大”的范畴啊

然后就是各种用户体验的增强,好吧体验你妹啊,在齐做出CSS的界面来体验就是个茶几,而且我觉得我的代码章法性好像还是太弱了,搞的我很没有底气,用户误操作下会不会就崩溃了啊杯具了啊之类,或者别有用心随便破解下就杯具,那不是惨了。哎没信心啊没信心,不知道是限制用户操作好呢,还是我的代码强悍点好

现在差的内容是服务器端的页面,这个我得先设计出UI示意图啊示意图,抓紧把作业赶完然后尽快做吧!!

框架出来后就是代码优化了,我在抉择要不要iframe化代码。。。抉择啊抉择。。。

没了就这么多