http://blog.csdn.net/duguang77/article/details/17539381

http://blog.csdn.net/duguang77/article/details/17539381

http://blog.csdn.net/duguang77/article/details/17539381




【官方】Android支持不同的密度或分辨率(Supporting Different Densities)

分类:Android 483人阅读 评论(1) 收藏 举报 Android支持不同的分辨率 Android图片适配



官方原文地址:http://developer.android.com/training/multiscreen/screendensities.html


本文主要介绍:

1.dip dp sp 简单用法

2.适配不同分辨率屏幕图片的处理方法


支持不同的密度或分辨率

本课介绍如何通过提供不同的资源和使用的测量分辨率独立单元支持不同屏幕密度(分辨率)


使用独立的像素密度

设计你的布局时会有一个常见的错误,你必须避免使用绝对像素来定义距离或大小。定义布局的尺寸与像素是一个问题,因为不同的屏幕有不同的像素密度,所以相同的像素数可以对应于不同的设备上不同的物理尺寸。因此,指定尺寸的时候,总是请使用DP或SP单元。一个DP是对应于一个像素的160 dpi的物理尺寸,密度无关的像素。一个sp是相同的基本单元,而是由用户首选的文本大小(它是一个独立的规模像素)的缩放,所以在定义文字大小,你应该使用这种测量单元(但从来没有为布局大小)


例如,当你指定的两个视图之间的间距,而不是使用DP PX:

(这里我感觉官方没有说清楚,控件与控件之间最好用dip来表示,而不适用dp或px)

[java] view plain copy
  1. <spanstyle="font-family:MicrosoftYaHei;"><Buttonandroid:layout_width="wrap_content"
  2. android:layout_height="wrap_content"
  3. android:text="@string/clickme"
  4. android:layout_marginTop="20dp"/></span>

当指定文字大小,总是用SP:

[java] view plain copy
  1. <spanstyle="font-family:MicrosoftYaHei;"><TextViewandroid:layout_width="match_parent"
  2. android:layout_height="wrap_content"
  3. android:textSize="20sp"/></span>

提供可供选择的位图

由于Android运行在与各种各样的屏幕密度的设备,你应该始终提供满足每个广义密度桶的位图资源:低,中,高和超高密度。这将帮助你实现所有的屏幕密度,良好的图形质量和性能。
要生成这些图片,你应该与你的原始资源开始在矢量格式和生成使用以下尺寸的规模各密度的图像:


xhdpi: 2.0
hdpi: 1.5
mdpi: 1.0 (baseline)
ldpi: 0.75

从上面可以看出,如果你生成一个200x200的图像xhdpi设备,你应该HDPI产生相同的资源在150×150,100×100的MDPI终于为LDPI设备一个75X75的形象。
然后,将生成的图像文件,在适当的子目录下的res /,系统会自动挑选根据您的应用程序运行在设备的屏幕密度正确的:

如下目录:

MyProject/
res/
drawable-xhdpi/
awesomeimage.png
drawable-hdpi/
awesomeimage.png
drawable-mdpi/
awesomeimage.png
drawable-ldpi/
awesomeimage.png

然后,您引用@绘制/ awesomeimage任何时候,系统会选择基于屏幕的DPI适当的位图。
为您的应用程序创建图标的资产更多的提示和指引,看到图标设计准则。









【官方】Android提供的支持不同屏幕大小的全部方法(Supporting Different Screen Sizes)

分类:Android 392人阅读 评论(0) 收藏 举报 Android屏幕适配 解决不同屏幕大小 Android不同分辨率

目录(?)[+]


原文地址为:http://developer.android.com/training/multiscreen/screensizes.html

本文将告诉你如何让你的应用程序支持各种不同屏幕大小,主要通过以下几种办法:

  • 让你的布局能充分的自适应屏幕
  • 根据屏幕的配置来加载合适的UI布局
  • 确保正确的布局应用在正确的设备屏幕上
  • 提供可以根据屏幕大小自动伸缩的图片

使用 "wrap_content" 和 "match_parent"

为了确保你的布局能够自适应各种不同屏幕大小,你应该在布局的视图中使用"wrap_content""match_parent"来确定它的宽和高。如果你使用了"wrap_content",相应视图的宽和高就会被设定成刚好能够包含视图中内容的最小值。而如果你使用了"match_parent"(在Android API 8之前叫作"fill_parent"),就会让视图的宽和高延伸至充满整个父布局。

通过使用"wrap_content""match_parent"来替代硬编码的方式定义视图大小,你的视图要么仅仅使用了需要的那边一点空间,要么就会充满所有可用的空间。例如:

[html]view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <LinearLayoutandroid:layout_width="match_parent"
  6. android:id="@+id/linearLayout1"
  7. android:gravity="center"
  8. android:layout_height="50dp">
  9. <ImageViewandroid:id="@+id/imageView1"
  10. android:layout_height="wrap_content"
  11. android:layout_width="wrap_content"
  12. android:src="@drawable/logo"
  13. android:paddingRight="30dp"
  14. android:layout_gravity="left"
  15. android:layout_weight="0"/>
  16. <Viewandroid:layout_height="wrap_content"
  17. android:id="@+id/view1"
  18. android:layout_width="wrap_content"
  19. android:layout_weight="1"/>
  20. <Buttonandroid:id="@+id/categorybutton"
  21. android:background="@drawable/button_bg"
  22. android:layout_height="match_parent"
  23. android:layout_weight="0"
  24. android:layout_width="120dp"
  25. style="@style/CategoryButtonStyle"/>
  26. </LinearLayout>
  27. <fragmentandroid:id="@+id/headlines"
  28. android:layout_height="fill_parent"
  29. android:name="com.example.android.newsreader.HeadlinesFragment"
  30. android:layout_width="match_parent"/>
  31. </LinearLayout>
注意上面的例子中是如何使用"wrap_content""match_parent"来给控件定义宽高的,这让整个布局可以正确地适应不同屏幕的大小,甚至是横屏。

下图是这个布局分别在竖屏和横屏时显示的结果,注意控件的宽和高是根据屏幕自适应的。

Android支持不同的密度或分辨率_第1张图片

使用RelativeLayout

通过多层嵌套LinearLayout和组合使用"wrap_content""match_parent"已经可以构建出足够复杂的布局。但是LinearLayout无法允许你准确地控制子视图之前的位置关系,所有LinearLayout中的子视图只能简单的一个挨着一个地排列。如果你需要让子视图能够有更多的排列方式,而不是简单地排成一行或一列,使用RelativeLayout将会是更好的解决方案。RelativeLayout允许布局的子控件之间使用相对定位的方式控制控件的位置,比如你可以让一个子视图居屏幕左侧对齐,让另一个子视图居屏幕右侧对齐。

例如:

[html]view plaincopy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:id="@+id/label"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:text="Typehere:"/>
  10. <EditText
  11. android:id="@+id/entry"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_below="@id/label"/>
  15. <Button
  16. android:id="@+id/ok"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_below="@id/entry"
  20. android:layout_alignParentRight="true"
  21. android:layout_marginLeft="10dp"
  22. android:text="OK"/>
  23. <Button
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_toLeftOf="@id/ok"
  27. android:layout_alignTop="@id/ok"
  28. android:text="Cancel"/>
  29. </RelativeLayout>
下图展示了这个布局在QVGA屏幕上显示的结果。

Android支持不同的密度或分辨率_第2张图片

下图展示了这个布局在一个更大的屏幕上显示的结果。

Android支持不同的密度或分辨率_第3张图片

可以注意到,即使屏幕的大小改变,视图之前的相对位置都没有改变。

使用Size限定符

虽然使用以上几种方式可以解决屏幕适配性的问题,但是那些通过伸缩控件来适应各种不同屏幕大小的布局,未必就是提供了最好的用户体验。你的应用程序应该不仅仅实现了可自适应的布局,还应该提供一些方案根据屏幕的配置来加载不同的布局,可以通过配置限定符(configuration qualifiers)来实现。配置限定符允许程序在运行时根据当前设备的配置自动加载合适的资源(比如为不同尺寸屏幕设计不同的布局)。

现在有很多的应用程序为了支持大屏设备,都会实现“two pane”模式(程序会在左侧的面板上展示一个包含子项的List,在右侧面板上展示内容)。平板和电视设备的屏幕都很大,足够同时显示两个面板,而手机屏幕一次只能显示一个面板,两个面板需要分开显示。所以,为了实现这种布局,你可能需要以下文件:

res/layout/main.xml,single-pane(默认)布局:

[html]view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <fragmentandroid:id="@+id/headlines"
  6. android:layout_height="fill_parent"
  7. android:name="com.example.android.newsreader.HeadlinesFragment"
  8. android:layout_width="match_parent"/>
  9. </LinearLayout>
res/layout-large/main.xml,two-pane布局: [html]view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="horizontal">
  5. <fragmentandroid:id="@+id/headlines"
  6. android:layout_height="fill_parent"
  7. android:name="com.example.android.newsreader.HeadlinesFragment"
  8. android:layout_width="400dp"
  9. android:layout_marginRight="10dp"/>
  10. <fragmentandroid:id="@+id/article"
  11. android:layout_height="fill_parent"
  12. android:name="com.example.android.newsreader.ArticleFragment"
  13. android:layout_width="fill_parent"/>
  14. </LinearLayout>
请注意第二个布局的目录名中包含了large限定符,那些被定义为大屏的设备(比如7寸以上的平板)会自动加载此布局,而小屏设备会加载另一个默认的布局。

使用Smallest-width限定符

使用Size限定符有一个问题会让很多程序员感到头疼,large到底是指多大呢?很多应用程序都希望能够更自由地为不同屏幕设备加载不同的布局,不管它们是不是被系统认定为"large"。这就是Android为什么在3.2以后引入了"Smallest-width"限定符。

Smallest-width限定符允许你设定一个具体的最小值(以dp为单位)来指定屏幕。例如,7寸的平板最小宽度是600dp,所以如果你想让你的UI在这种屏幕上显示two pane,在更小的屏幕上显示single pane,你可以使用sw600dp来表示你想在600dp以上宽度的屏幕上使用two pane模式。

res/layout/main.xml,single-pane(默认)布局:

[html]view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <fragmentandroid:id="@+id/headlines"
  6. android:layout_height="fill_parent"
  7. android:name="com.example.android.newsreader.HeadlinesFragment"
  8. android:layout_width="match_parent"/>
  9. </LinearLayout>
res/layout-sw600dp/main.xml,two-pane布局: [html]view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="horizontal">
  5. <fragmentandroid:id="@+id/headlines"
  6. android:layout_height="fill_parent"
  7. android:name="com.example.android.newsreader.HeadlinesFragment"
  8. android:layout_width="400dp"
  9. android:layout_marginRight="10dp"/>
  10. <fragmentandroid:id="@+id/article"
  11. android:layout_height="fill_parent"
  12. android:name="com.example.android.newsreader.ArticleFragment"
  13. android:layout_width="fill_parent"/>
  14. </LinearLayout>
这意味着,那些最小屏幕宽度大于600dp的设备会选择layout-sw600dp/main.xml(two-pane)布局,而更小屏幕的设备将会选择layout/main.xml(single-pane)布局。

然而,使用早于Android 3.2系统的设备将无法识别sw600dp这个限定符,所以你还是同时需要使用large限定符。这样你就需要在res/layout-largeres/layout-sw600dp目录下都添加一个相同的main.xml。下节你将会看到如何避免重复定义这种布局的技巧。

使用布局别名

Smallest-width限定符仅在Android 3.2及之后的系统中有效。因而,你也需要同时使用Size限定符(small, normal, large和xlarge)来兼容更早的系统。例如,你想手机上显示single-pane界面,而在7寸平板和更大屏的设备上显示multi-pane界面,你需要提供以下文件:

  • res/layout/main.xml: single-pane布局
  • res/layout-large: multi-pane布局
  • res/layout-sw600dp:multi-pane布局

最后的两个文件是完全相同的,为了要解决这种重复,你需要使用别名技巧。例如,你可以定义以下布局:

  • res/layout/main.xml, single-pane布局
  • res/layout/main_twopanes.xml, two-pane布局

加入以下两个文件:

res/values-large/layout.xml:

[html]view plaincopy
  1. <resources>
  2. <itemname="main"type="layout">@layout/main_twopanes</item>
  3. </resources>
res/values-sw600dp/layout.xml:
[html]view plaincopy
  1. <resources>
  2. <itemname="main"type="layout">@layout/main_twopanes</item>
  3. </resources>

最后两个文件有着相同的内容,但是它们并没有真正去定义布局,它们仅仅只是给main定义了一个别名main_twopanes。这样两个layout.xml都只是引用了@layout/main_twopanes,就避免了重复定义布局文件的情况。

使用Orientation限定符

有些布局会在横屏和竖屏的情况下都显示的很好,但是多数情况下这些布局都可以再调整的。在News Reader示例程序中,布局在不同屏幕尺寸和不同屏幕方向中是这样显示的:

  • 小屏幕, 竖屏: 单面板, 显示logo
  • 小屏幕, 横屏: 单面板, 显示logo
  • 7寸平板, 竖屏: 单面板, 显示action bar
  • 7寸平板, 横屏: 双面板, 宽, 显示action bar
  • 10寸平板, 竖屏: 双面板, 窄, 显示action bar
  • 10寸平板, 横屏: 双面板, 宽, 显示action bar
  • 电视, 横屏: 双面板, 宽, 显示action bar

所有这些布局都是定义在res/layout/这个目录下,为了要让设备根据屏幕配置来加载正确的布局,程序需要使用布局别名来实现。

res/layout/onepane.xml:

[html]view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <fragmentandroid:id="@+id/headlines"
  6. android:layout_height="fill_parent"
  7. android:name="com.example.android.newsreader.HeadlinesFragment"
  8. android:layout_width="match_parent"/>
  9. </LinearLayout>
res/layout/onepane_with_bar.xml: [html]view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <LinearLayoutandroid:layout_width="match_parent"
  6. android:id="@+id/linearLayout1"
  7. android:gravity="center"
  8. android:layout_height="50dp">
  9. <ImageViewandroid:id="@+id/imageView1"
  10. android:layout_height="wrap_content"
  11. android:layout_width="wrap_content"
  12. android:src="@drawable/logo"
  13. android:paddingRight="30dp"
  14. android:layout_gravity="left"
  15. android:layout_weight="0"/>
  16. <Viewandroid:layout_height="wrap_content"
  17. android:id="@+id/view1"
  18. android:layout_width="wrap_content"
  19. android:layout_weight="1"/>
  20. <Buttonandroid:id="@+id/categorybutton"
  21. android:background="@drawable/button_bg"
  22. android:layout_height="match_parent"
  23. android:layout_weight="0"
  24. android:layout_width="120dp"
  25. style="@style/CategoryButtonStyle"/>
  26. </LinearLayout>
  27. <fragmentandroid:id="@+id/headlines"
  28. android:layout_height="fill_parent"
  29. android:name="com.example.android.newsreader.HeadlinesFragment"
  30. android:layout_width="match_parent"/>
  31. </LinearLayout>
res/layout/twopanes.xml: [html]view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="horizontal">
  5. <fragmentandroid:id="@+id/headlines"
  6. android:layout_height="fill_parent"
  7. android:name="com.example.android.newsreader.HeadlinesFragment"
  8. android:layout_width="400dp"
  9. android:layout_marginRight="10dp"/>
  10. <fragmentandroid:id="@+id/article"
  11. android:layout_height="fill_parent"
  12. android:name="com.example.android.newsreader.ArticleFragment"
  13. android:layout_width="fill_parent"/>
  14. </LinearLayout>
res/layout/twopanes_narrow.xml: [html]view plaincopy
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="horizontal">
  5. <fragmentandroid:id="@+id/headlines"
  6. android:layout_height="fill_parent"
  7. android:name="com.example.android.newsreader.HeadlinesFragment"
  8. android:layout_width="200dp"
  9. android:layout_marginRight="10dp"/>
  10. <fragmentandroid:id="@+id/article"
  11. android:layout_height="fill_parent"
  12. android:name="com.example.android.newsreader.ArticleFragment"
  13. android:layout_width="fill_parent"/>
  14. </LinearLayout>
现在所有需要的布局都已经定义好了,剩下的只要使用限定符来让各个设备根据屏幕配置加载正确的布局了。你现在就可以使用布局别名技术:

res/values/layouts.xml:

[html]view plaincopy
  1. <resources>
  2. <itemname="main_layout"type="layout">@layout/onepane_with_bar</item>
  3. <boolname="has_two_panes">false</bool>
  4. </resources>
res/values-sw600dp-land/layouts.xml: [html]view plaincopy
  1. <resources>
  2. <itemname="main_layout"type="layout">@layout/twopanes</item>
  3. <boolname="has_two_panes">true</bool>
  4. </resources>
res/values-sw600dp-port/layouts.xml: [html]view plaincopy
  1. <resources>
  2. <itemname="main_layout"type="layout">@layout/onepane</item>
  3. <boolname="has_two_panes">false</bool>
  4. </resources>
res/values-large-land/layouts.xml: [html]view plaincopy
  1. <resources>
  2. <itemname="main_layout"type="layout">@layout/twopanes</item>
  3. <boolname="has_two_panes">true</bool>
  4. </resources>
res/values-large-port/layouts.xml: [html]view plaincopy
  1. <resources>
  2. <itemname="main_layout"type="layout">@layout/twopanes_narrow</item>
  3. <boolname="has_two_panes">true</bool>
  4. </resources>

使用Nine-Patch图片

支持不同屏幕大小通常情况下也意味着,你的图片资源也需要有自适应的能力。例如,一个按钮的背景图片必须能够随着按钮大小的改变而改变。

如果你想使用普通的图片来实现上述功能,你很快就会发现结果是令人失望的,因为运行时会均匀地拉伸或压缩你的图片。解决方案是使用nine-patch图片,它是一种被特殊处理过的PNG图片,你可以指定哪些区域可以拉伸而哪些区域不可以。

因而,当你设计需要在不同大小的控件中使用的图片时,最好的方法就是用nine-patch图片。为了将图片转换成nine-patch图片,你可以从一张普通的图片开始:

Android支持不同的密度或分辨率_第4张图片

然后通过SDK中带有的draw9patch工具打开这张图片(工具位置在SDK的tools目录下),你可以在图片的左边框和上边框绘制来标记哪些区域可以被拉伸。你也可以在图片的右边框和下边框绘制来标记内容需要放置在哪个区域。结果如下图所示:

Android支持不同的密度或分辨率_第5张图片

注意图片边框上的黑色像素,在上边框和左边框的部分表示当图片需要拉伸时就拉伸黑点标记的位置。在下边框和右边框的部分表示内容将会被放置的区域。

同时需要注意,这张图片的后缀名是 .9.png。你必须要使用这个后缀名,因为系统就是根据这个来区别nine-patch图片和普通的PNG图片的。

当你需要在一个控件中使用nine-patch图片时(如android:background="@drawable/button"),系统就会根据控件的大小自动地拉伸你想要拉伸的部分,效果如下图所示:

Android支持不同的密度或分辨率_第6张图片













【官方】Android多屏幕的设计与适配(Designing for Multiple Screens)

分类:Android 333人阅读 评论(0) 收藏 举报 android支持不同的屏幕尺寸 android支持不同的屏幕密度 android实施适应性研究的UI流量


官方原文地址:http://developer.android.com/training/multiscreen/index.html


多屏幕的设计与适配

Android的缺点是数百个不同屏幕尺寸的设备类型,从小型的手机到大电视机。因此,重要的是你设计你的应用程序能与所有的屏幕尺寸兼容,因此它提供给尽可能多的用户可能很重要。

但作为与不同设备类型兼容是不够的。每个屏幕大小提供不同的可能性和用户交互的挑战,因此为了真正满足和打动你的用户,你的应用必须超越仅仅是支持多个屏幕:它必须优化每个屏幕配置的用户体验。

本课程将告诉您如何实现的几个屏幕配置优化的用户界面。

在每节课的代码来自演示在优化的多个屏幕的最佳实践示例应用程序。你可以将示例下载(右侧) ,并把它作为可重用的代码为自己的应用程序的来源。

注:本类和相关的示例使用支持库才能使用该片段上的API版本比Android 3.0低。你必须下载和库添加到您的应用程序才能使用的所有API在这个类中。

课程:

支持不同的屏幕尺寸http://blog.csdn.net/u011112840/article/details/17539939
本课将引导您如何设计布局,能够适应多种不同的屏幕尺寸(使用灵活的尺寸意见,RelativeLayout,屏幕大小和方向限定词,叠滤波器,以及九补丁位图)


支持不同的屏幕密度:http://blog.csdn.net/u011112840/article/details/17539381
本课程将告诉您如何支持具有不同的像素密度(用密度无关的像素,并提供适当的位图每个密度)屏幕


实施适应性研究的UI流量
本课程将告诉您如何实施,能够适应多种屏幕尺寸/密度组合(运行时检测的积极布局,根据目前的布局反应,处理屏幕配置更改)的方式你的UI流程





http://blog.csdn.net/duguang77/article/details/17540091
http://blog.csdn.net/duguang77/article/details/17540091
http://blog.csdn.net/duguang77/article/details/17540091
http://blog.csdn.net/duguang77/article/details/17540091











更多相关文章

  1. 安卓相对布局
  2. Android基本布局案例(2)
  3. android 常用布局公用属性
  4. Android UI布局中设置了fill_parenet仍然没有全屏的解决办法
  5. 跟大家分享下Android布局文件layout.xml的一些属性值。
  6. Android中画面的布局工具

随机推荐

  1. Android动画效果生动有趣的通知NiftyNoti
  2. Android应用程序开发(第三版)-课后习题解答
  3. android屏幕监视工具 android screen mon
  4. Android(安卓)TextView长按复制文本
  5. Android SDK版本和ADT版本
  6. Android中定义样式(1)
  7. Android的MediaPlayer架构介绍
  8. android 入门之二【android 体系架构】
  9. 【Android 开发】:通知之Notifications To
  10. handler机制