android支持多尺寸屏幕【译】 mulit screen support

来源:互联网 发布:淘宝手机端详情超链接 编辑:程序博客网 时间:2024/06/08 07:00
1. If your application is compiled for Android 1.5 or lower, Android will assume your application was designed to look good on the classic screen size and resolution.
android执行所谓的“兼容模式”
(a)对于大屏幕的,图片进行相应的放大,大小比例一致,但图片会因此而模糊;
(b)对更小屏幕,则直接提示不能运行

2. If your application is compiled for Android 1.6 or higher, Android assumes that you are properly handling all screen sizes, android执行所谓的“非兼容模式”,以下是一些应该掌握的技巧
(1)不要以具体位置,而以“规格或规则”来放置UI元素

Don't Think About Positions, Think About Rules

eg1.   The simplest rules are the fill_parent and wrap_content values for android:layout_width and android:layout_height;
eg2.  The richest environment for easily specifying rules is to use RelativeLayout,
  • Explicitly anchor widgets to the bottom or right side of the screen, rather than hoping they will wind up there courtesy of some other layout
  • Control the distances between widgets that are "connected" (e.g., a label for a field should be to the left of the field) without having to rely on padding or margins
(2)以机器的物理尺寸为单位(比如mm,inch)

Consider Physical Dimensions

If you have something intrinsically scalable (e.g., a Button) where you had been specifying a size in pixels, you might consider switching to using millimeters (mm) or inches (in) as the unit of measure. 10mm is 10mm regardless of the screen resolution or the screen size. This way, you can ensure that your widget is sized to be finger-friendly, regardless of the number of pixels that might take.

(3)避免以像素为单位(像素跟密度,分辨率对整体显示有印象)

Avoid "Real" Pixels

(a)使用dp或dip,优点是,在物理尺寸一致时,不同的密度值,对应的物理尺寸不变,即不依赖密度。即160dpi的机器上240dpi的机器上50dip从视觉的大小上来讲是一样的。
(b)对字体单位使用sp,系统会根据用户选择的字体大小对字体进行合理配比。

(4)选择可以伸缩的图片

Choose Scalable Drawables

选择九宫图( nine-patch bitmaps ),以及xml定义的图片(比如GradientDrawable),避免静态图片。

3. 针对以上不能解决的问题,需要创建不同的资源集合
(a)Default集合,支持默认的拉伸,但拉伸算法会降低应用的执行速度;
(b)基于密度集合,创建分别以-ldpi, -mdpi, and -hdpi为后缀的资源集合,比如  res/values-hdpi/dimens.xml,是指适合hdpi设备的dimen.xml配置文件;
(c)基于尺寸集合,创建分别以
-small, -normal-large为后缀的资源集合,比如res/layout-large-land/,是指适合大屏幕(如WVGA)的横屏布局;
(d)基于版本集合,创建以-vN(N是对应版本的api级别)格式的资源集合,比如res/drawable-large-v4/,是指大屏幕,android 1.6版本或更新的文件夹。

4. 关于多分辨率多密度的android应用测试
由于基于LCD屏幕模拟器的密度一般比真机的密度小,布局方面的位置逻辑方面一致,但需要注意两个问题:
(i)真机下相同分辨率,物体会变得小很多;
(ii)使用触摸屏时,手指能点击的效果跟鼠标点击差别很大,前者在点击区域不大时,有时候很难点击上。
(a)1.6以上,特别是2.0以后模拟环境搭建测试,为了比较准确地模拟真机效果,可以选择AVD Manager,选中“Lauch Options”,选中"Scale display to real size",提供以下信息:
*模拟的机器的物理尺寸(inchs为单位)
**模拟器的dpi(可以点击右边的?获取)

但,由于基于LCD屏幕模拟器的密度一般比真机的密度小, 字体在真机下会比较失真,图片容易变得斑驳等。

(b)真机测试,可以购买代表性的几款手机(3款?),建立一个测试群,或者发布-反馈机制(用户反馈bug),或者加入deviceanywhere。

参考:
http://www.androidguys.com/2010/02/16/handling-multiple-screen-sizes-part/
http://www.androidguys.com/2010/02/18/handling-multiple-screen-sizes-part-2/
http://www.androidguys.com/2010/02/23/handling-multiple-screen-sizes-part-3/

http://www.androidguys.com/2010/03/01/handling-multiple-screen-sizes-part-4/
http://www.androidguys.com/2010/03/02/handling-multiple-screen-sizes-part-5/

 

原创粉丝点击