「Efficient Android Threading 笔记」- C1 Android Components and the Need for Multiprocessing Android RecyclerView Android Socket Programming Supporting Multiple Screens Make a Reusable UI in Android App Development 如何在 Android Studio 中包含 *.so library,并使用库中定义的方法? 使用 SpannableString 格式化字符串,实现前景色、下划线、超链接、图文混排等 如何使用 bound service 完成进程间通信? 创建自定义视图 Creating custom views 通过 Android Theme & Style 定制应用的样式 「译」Android ViewPropertyAnimator 介绍 Android Animation Interpolator - Android 动画插值器源码笔记 「译」Android Animation in Honeycomb by Chet Haase(Android 3.0系统中的动画机制) 从 Android Sample ApiDemos 中学习 android.animation API 的用法 如何学习 Android Animation? 如何实现 Android ListView「上拉加载更多」? 「译」向Big Nerd Ranch提问:为什么Fragment在Android App开发中非常重要? 分类整理我在 SegmentFault 上针对某些问题作的回答 Android Servcie 后台服务总结笔记 如何在Android设备旋转时暂存数据以保护当前的交互状态? Android Message Handler 消息处理机制总结笔记 如何获取FragmentTabHost中指定标签页的Fragment? Fragment子类必须包含一个public无参构造器 如何更新及替换ViewPager中的Fragment? 如何使用Android UI Fragment开发“列表-详情”界面? 一个Android音频文本同步的英文有声读物App的开发过程 「Android编程权威指南笔记」Android应用本地化 通过jfeinstein10/SlidingMenu实现Android侧滑菜单 为Ubuntu14.04部署Android App的Eclipse开发环境 「Android编程权威指南笔记」使用ListFragment显示列表 「Android编程权威指南笔记」SDK版本与兼容 「Android编程权威指南笔记」Android布局和组件 「Android编程权威指南笔记」UI Fragment 「Android编程权威指南笔记」Activity 第一次开发iOS App和Android的对比总结笔记 「App Training笔记」创建第一个应用 「App Training笔记」开发入门训练大纲 Android APP - 从远程FTP服务器下载文件到本地

Supporting Multiple Screens

2017年02月01日

术语和概念

  • Screen size 屏幕尺寸

    • 又称「屏幕大小」,是屏幕对角线的物理尺寸。

    • 单位英寸 inch,比如 Samsung Note4 是 5.7 英寸。

  • Resolution 屏幕分辨率

    • 屏幕纵横方向上物理像素的总数,比如 Samsung Note4 是 2560x1440,表示纵向有 2560 个像素,横向有 1440 个像素。

    • pixel 简写 px。

  • Screen density 屏幕密度

    • 屏幕物理区域中的像素量(quantity of pixels)。

    • 单位 dpi(dots per inch)。

    • 计算公式:对角线上的像素个数 / 屏幕尺寸,(2560^2 + 1440^2)^(1/2) / 5.7 = 515.3。

    • 六种通用密度:low, medium, high, extra-high, extra-extra-high, and extra-extra-extra-high.

    • 160 dpi 是基线密度(baseline density、mdpi)。

  • Orientation 方向

    • 从用户视角看屏幕的方向,即横屏还是竖屏(landscape or portrait),分别表示屏幕的纵横比(screen’s aspect ratio)是宽还是高。
  • Density-independent pixel (dp) 密度无关像素

    • 1dp = 1 px on a 160 dpi screen。

    • dp 单位转换为 px: px = dp * (dpi / 160),最后一节“不要使用硬编码的像素值”通过代码给出更详细的说明。

    • 应用的 UI 时应始终使用 dp 单位。

如何支持多种屏幕

屏幕尺寸和分辨率是用户关心的参数,尺寸越大用户看到的越多、分辨率越高显示越细腻。

屏幕密度是一个物理概念,由屏幕尺寸和分辨率决定。

密度独立性 Density independence(dp) 是 Android 为了解决屏幕碎片化而抽象出的一个概念,以 在各种密度的屏幕上保持 UI 元素的物理尺寸(从用户的视角)。

而对于开发者,应只关注屏幕尺寸和密度:

为不同的屏幕尺寸提供替代 layouts

  • 特别是横屏或者平板应用,需要调整 UI 元素的位置和尺寸,以利用屏幕空间(比如,竖屏时置于底部的 UI 在横屏时应位于屏幕右侧);

  • 系统提供了 4 种屏幕尺寸限定符:small, normal,large, xlarge,Androd 3.2+ 后被弃用。

  • 尺寸限定符(size qualifiers):smallestWidth sw<N>dp, Available screen width w<N>dp, Available screen height h<N>dp.

  • w<N>dp 主要用于横屏、多窗格(Multi-pane);而 h<N>dp 很少被用到,因为 UI 垂直滚动,高度更具弹性;sw<N>dp 不考虑屏幕方向,只考虑一个最小尺寸。

  • 新技术基于布局需要的空间量(the amount of space your layout needs,例如 600dp 宽),而不是尝试让您的布局容纳通用化的尺寸组 (例如大或超大)。在设计 UI 时, 主要关注的可能是 App 在 handset-style UI 与 tablet-style UI that uses multiple panes 之间切换时的实际尺寸。

  • 方向限定符(Orientation qualifiers)land 用于横屏 landscape,port 用于竖屏 portrait(默认)。

为不同的屏幕密度提供替代 bitmap images

  • 系统通过密度限定符(density qualifiers)查找匹配的资源目录,包括 ldpi、mdpi、hdpi、xhdpi、xxhdpi 和 xxxhdpi。

  • 如果设备屏幕密度是 xxhdpi,那么包含 xxhdpi 限定符(例如 drawable-xxhdpi/)的密度特定目录(density-specific directory)可能是最佳匹配项。

  • 如果密度特定目录中没有匹配资源,系统不一定使用默认资源(drawable/),而是使用其它密度特定目录进行缩放,但这可能导致模糊、变形。具体是怎样查找的,更多阅读 《Android 如何查找最佳匹配资源 How Android Finds the Best-matching Resource.》

  • Nine-Patch bitmap file(九宫格位图文件)只拉伸指定的区域。

  • 应遵循六种通用密度之间的 0.75 : 1 : 1.5 : 2 : 3 : 4 缩放比率,更多阅读 《Icon Design Guidelines》.

  • 这篇文档中有句话,不要感到困惑,因为已经过时了:

    对启动程序图标以外的 UI 元素不应使用 xxxhdpi 限定符。You should not use the xxxhdpiqualifier for UI elements other than the launcher icon. 引用自《Supporting Multiple Screens》
    ;
    Choosing to add xxxhdpi versions for the rest of your assets will provide a sharper visual experience(更清晰的视觉体验) on the Nexus 6, but does increase apk size, so you should make an appropriate decision for your app. 引用自 《Getting Your Apps Ready for Nexus 6 and Nexus 9》2014/10/23

以适当的 dp 值、wrap_contentmatch_parent 指定所有布局尺寸值,字体使用 sp(scale-independent pixel)

  • 上述 size, density qualifiers 也可以用于 values-<qualifier>.

不要使用硬编码的像素值(hard-coded pixel values)

  • Android 系统使用像素作为表示尺寸或坐标值的标准单位,例如 View.getWidth() 返回的是 px。

  • 将 dp 单位转换为像素单位:

      /**
       * Convert the dps to pixels, based on density scale
       * @param dp value expressed in dps
       * @return value expressed in pixels
       */
      private int dpToPixel(int dp) {
         // Get the screen's density scaling factor
         float scale = getResources().getDisplayMetrics().density;
         // Add 0.5f to round the figure up to the nearest whole number
         return (int) (dp * scale + 0.5f);
      }
    
  • 获取 Screen Density (dpi),例如 Samsung Note4 screen density is 640,这个数字被称为 quantized density, 然而屏幕密度的物理值是 515(计算公式见“术语和概念”一节),这个数字被称为 physical density

      getResources().getDisplayMetrics().densityDpi;
    
      weiyi$ adb shell cat /system/build.prop | grep density
    
      weiyi$ adb shell getprop ro.sf.lcd_density
      ro.sf.lcd_density=640
    
  • physical density 就是由物理参数决定的,而 quantized density 是有厂商决定的,这个值一般是屏幕密度分组的「上限值」:120,160,240,320,480,640. quantized density 决定了 图片缩放系数 Image Scaling Factor (ISF)

      DisplayMetrics().density
      ISF = ro.sf.lcd_density / 160
    

资源


知识共享许可协议
li2的博客WeiYi.Li 创作,采用 知识共享 署名-非商业性使用 4.0 国际 许可协议进行许可。
© 2011-2022. All rights reserved by WeiYi.Li. Powerd by Jekyll & LinAnYa's Theme