【1-8】Android布局技巧与优化

一、思维导图

Android布局技巧与优化

二、重述知识

这节课主要讲了Android的布局是怎样绘制的,
它是按层级一层层地绘制的,所以如果层次太多就会产生性能问题。
这就需要我们去优化布局,有以下的这些手段:

  1. 多用相对布局,合理使用线性布局;
  2. 用<include/>标签去引用相同的部分
  3. 用<merge/>减少无用的层次
  4. 布局结构要清晰,删除无用的布局,选择合适的布局。

还可以利用“Android Lint”、“Hierarchy Viewer”这样的工具协助优化。
另外,不要嵌套多个使用layout_weight属性的LinearLayout。

三、具体应用场景

略。(这些优化技巧,很难从应用长什么样子看出来,用起来才知道。)

四、扩展理解(优劣?有无替代方案?除了这样用,还能怎么用?)

这里推荐徐宜生的《Android群英传》P228~P234,
有这方面内容的明细,其实根本不用写,看完、练完书上的也就学到了。

Android布局是怎样绘制出来的(是什么)?【简述】

在Android中,系统对View进行测量、布局和绘制时,都是通过对View数的遍历来进行操作的。 ——《Android群英传》

简单来说,就是一层层深入,画完“子”那一层,再回去画“父”那层。

更深入的内容,请阅读《公共技术点之 View 绘制流程》这篇文章,我初学就先不看了。

为什么我这个应用这么卡(为什么)?

首先,回答这个问题,要先知道怎样才会觉得应用不卡。
玩游戏的都知道,最佳fps是60fps,
我们人眼能感觉到的流畅是40帧/秒到60帧/秒。
在Android中, 1秒/60帧 约等于16ms。
这个数据表示,我们在手机屏幕看到的所有UI界面的东西,
都要保证在16ms内完成绘制,画不完的要等到下一次屏幕渲染,
这样就会在16ms*2的时间内,显示同一帧画面,
那我们人眼看出来的效果,就是“卡”了。

那我怎么优化才能不卡啊(怎样做)?

目前对我来说,就是上面说到那几样:优化布局层级、<include/>、<merge/>、<ViewStub/> ……

五、核心代码或操作

1.课程视频中的减少<LinearLayout/>的操作
首先从第3分钟开始,老师以拖控件的方式,
拖了一个布局出来,从“3:28~8:45”这段时间,过程如下所示:

前8分钟操作步骤 1

中间那个横向的<LinearLayout/>高度改为100dp,
在中间那个<LinearLayout/>再加个<LinearLayout/>,
再改一下大小、颜色之类的属性,就弄成下面最后的那幅图那样。

前8分钟操作步骤 2

代码大概是下面这样,这个是我自己拖的,
不是视频里面的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#888888"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:background="#567890"
        android:gravity="right"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="@color/red"
            android:orientation="horizontal"></LinearLayout>

        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

然后就可以开始减少层次了,过程如下:


[Gif备用链接]:(http://7vzri9.com1.z0.glb.clouddn.com/CutLinearLayout.GIF)

2.课程视频中的<include/>的操作
例如我有一个布局,要用到其它很多的Activity,
没理由每个Activity再重新写一遍这个布局的代码,
这时候就可以用<include/>标签,
把这个需要重用的代码放到那些Activity需要的地方。

新建一个这样的 layout_title.xml 布局

在另一个Activity把它include进来

产生这样的效果

就像上面这样,在activity_main.xml中把layout_title.xml的布局<include/>进来。

(课程视频里面其它的优化技巧说得都不太好,
看完别的资料,还需要写另一篇博文说一下。)

六、相关面试题

待补充。

七、脑内记忆

主要记住下面这件代码:

<include layout="@layout/layout_title"/>

layout → 布局
一般这句都是有两个"布局",
那个@想成象棋棋子,两个"布局"include在一起,
就是一幅象棋咯。(脑洞略大)

八、参考资料

  1. Android Layout Tricks #1
  2. Android Layout Tricks #2: Reusing layouts
  3. Android Layout Tricks #3: Optimize by merging
  4. Android Layout Tricks #3: Optimize with stubs
  5. Optimizing Your UI
  6. Analyzing UI Performance with Systrace
  7. Improving Your Code with lint
  8. Improving Layout Performance
  9. 布局优化技巧
  10. Android最佳性能实践(四)——布局优化技巧
  11. [Android]布局优化技巧](http://www.cnblogs.com/rossoneri/p/4838072.html)
  12. 【推荐】Android布局优化之过度绘制
  13. 【推荐】公共技术点之 View 绘制流程
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容