Android screen adaptation; + knowledge + Raiders Interview Highlights

Primer

Screen adaptation is android  development / interview  a not open around the problem. This article will screen adaptation of 知识要点the complete show to our readers.

Body outline

  • android screen adaptation of reason needs to be done

  • Basics points (very important)

  • Screen adaptation Raiders

text

android screen adaptation of reason needs to be done

Keywords:android碎片化

Since the launch of android, google android open source system, various manufacturers fragmented, resulting in no uniform standard screen size, screen aspect ratio of a wide variety, screen density also various manufacturers comparisons of capital, leading to Android developers want to use a code to fit all of the equipment has become particularly difficult, although google the dp units instead of px, dp but still has its place not fully fit.
This is, we need to use other means to complete the adaptation of place.

Interview point

Q : About screen adaptation, Why have dpunits still have to fit it?
AdpAlthough the adapter can solve the problem in most cases, but for a few models, unable to adapt,
reason:  googleto provide the dpunits in the existing androidmarket environment, 不可靠and the time code  dp, at run time converted to  px, at this time can not be accurately controlled px UI of originally designed.
The most visual expression is: Similarly 300dp, in the A cell phone can fill the full width , but on the B cell phones, accounting for less than , or exceed the full width .
至于为什么dp不可靠? I will explain later.

Basics points (very important)

The following concepts are very basic, but very important, basics missing link, to understand the complexity behind the Raiders have obstacles.

1. pixels

Pixel is a physical concept of the term to explain, this passage is as follows:
通常所说的像素,就是CCD/CMOS上光电感应元件的数量,一个感光元件经过感光,光电信号转换,A/D转换等步骤以后,在输出的照片上就形成一个点,我们如果把影像放大数倍,会发现这些连续色调其实是由许多色彩相近的小方点所组成,这些小方点就是构成影像的最小单位“像素”(Pixel)。简而言之,像素就是手机屏幕的最小构成单元。

Simply put, the pixel is 屏幕上色彩区域的最小单位. On all devices, 1px are identical. UI sister usually plotted to px units.

2. Resolution

For example: 1920 1080 resolution, a screen indicating the current device is made transversely 1080  compositions from 1920 pixels vertical.

3. Screen Size

The length of the screen diagonal. Usually in inches, common screen sizes  4.7 ,  5.5 , 6.0 .

4. The screen density

定义: 一平方英寸面积内,存在多少个像素点。
同样是 1920*1080 的分辨率,存在两款手机,一个是4.7英寸,一个是6.0英寸,这两者的 像素密度是不一样的。
屏幕密度的单位: dpi(dots per inch).
标准屏幕分辨率 :160dpi 。即:一平方英寸面积,存在160像素点。(mdpi).

5. 屏幕尺寸,分辨率,像素密度 三者之间的关系

像素密度,可以由屏幕尺寸分辨率计算得出。
计算公式为:

webp

比如下图所示:

webp

我这里有一块1920*1080的5寸屏幕,通过上面的公式计算得出,它的像素密度为 440dpi .
可见,px是和像素密度有直接关系的 像素单位。

6. 密度无关的像素单位(dp,或者dip)

全英文为:density-independent pixel.
(注意此处是dip,上面的像素密度是 dpi, 千万不要因为这个,在面试的时候出丑 )
使用dp作为长度单位,可以保证在不同的屏幕像素密度的手机上显示 很相似的效果。
比如: 使用480x800的手机上,要画一条长度为一半屏幕宽的线条,我们可以设置线条的长度为 240px,而在320x480的屏幕上,我们只需要160px,但是我们可以直接使用160dp,来同时让两个屏幕上的这条线占全宽的一半。

px和dp的换算公式为:px = dp * (dpi / 160)
公式解读:
如果有一个屏幕密度为 160dpi的手机,在它上面,1px=1dp;
而如果是 320dpi的手机,则 1px = 0.5dp.
简而言之规律就是: 屏幕像素密度(dpi)越高的手机,1dp所代表的px就越多.

下面解释为什么dp有时候靠不住.

如果UI小姐姐以320*480 dpi160的屏幕为标准,画了一条长度为160px的线,长度为屏幕宽度的一半,这个时候,你直接用 160dp,一般就能完成适配。但是如果这样的布局 运行在320x480分辨率,但是屏幕密度为150dpi 略小于 160dpi,这个时候 你布局里面写的160dp就会实际超过160px 不再是占屏幕宽的一半,而是略微超过。

7. 独立比例像素 (sp)

全英文为: scale-independent pixel (简称sp或者sip)
专门用于字体大小表示。
推荐使用12sp以上的偶数作为 字体大小, 不要使用奇数,或者浮点型小数,因为容易造成精度丢失。

面试点

Q: sp和dp有什么区别?
A: 通常情况下,dp和sp效果类似,但是有一点,如果用户 调整了手机字体,比如 从标准,变成了 超大,那么,1dp 原本等于1px依然不变,但是1sp就会从1px变成3px(只是举个例子,数值不必认真).
因此,在用户调整字体的情况下,同样的布局,可能出现 窗口大小不变,但是文字尺寸发生变化的情况。


屏幕适配攻略

1. 布局适配

使用多套布局适应不同屏幕。此方法必须要了解 布局限定符的概念。
如图:

webp

限定符: 在原本的layout后面加上横杠,然后加上限定名。形成 layout-XXX的形式,具体规则请参考谷歌官网: https://developer.android.google.cn/training/multiscreen/screensizes, 重点摘录如下:

webp

webp

使用上面的限定符,几乎可以解决所有的适配问题,但是另外还有一个小技巧帮我们节省工作量 :布局别名.
适用情形为:当多个加了限定符的 layout.xml中都引用了同一个 子布局,而子布局的内容可能相同,也可能不同。
这个时候,使用布局别名,可以节省操作量。

webp

webp

方案评价: 讲道理,这个是下下策,因为在你需要修改布局的时候,你可能需要修改多套布局,这个要多恶心就多恶心,懂的人都懂.

2 代码适配

我们通过java代码去获取屏幕的宽高,动态去指定控件的宽高 。
获取屏幕宽高的方式我不想在这里占篇幅。
代码适配,一般用于动态创建 控件,或者自定义view自己绘制图形的时候。
特别提一下接口适配:当你去向后台请求图片的时候,我们可以在参数中带入屏幕的宽高,或者是控件的宽高,来获取我们想要的图片,在图片返回之后直接就能显示得最优,而不需要我们app代码中多动手脚。

方案评价:这个比下下策稍微好一点,但是 控件大小直接进入了代码,还是让代码不够雅观.

3. 布局组件的适配

尽量直接通过一套布局解决所有的麻烦,这个就叫布局组件的适配。这个是上上策.
包含如下手段:

  • 使用像素密度无关的单位 dp sp

  • 杜绝使用绝对布局,多使用相对和线性布局

  • 多使用 wrap_content match_parent 以及线性布局的权重

  • 多用 minWidth minHeight,lines 等属性

  • 使用多套限定的 dimens中定义的尺寸

可能面试点

Q:同一个布局文件中,我使用线性布局和相对布局都可以达成目的,那么如何抉择?
A: 使用相对布局,很有可能出现 第一次测量"不满意"的情况,从而会测量第二次。如果两者都可以达成目的,并且两者的布局层级相同,并且线性布局中没有使用到权重(权重可能也会触发第二次测量),此时,优先使用线性布局。 除此之外,都选择相对布局。

结语

屏幕适配,工作中并不困难,但是面试多会问到。
现今主流的两种完美适配方案,有头条方案(直接变更DisplayMetrics中的density),以及 多套dimens适配方案,两者各有千秋。


Guess you like

Origin blog.51cto.com/14048760/2473549