美文网首页
Android性能优化:布局优化

Android性能优化:布局优化

作者: 王多鱼2 | 来源:发表于2019-08-19 00:28 被阅读0次

    在开发中布局性能的好坏 主要影响 :Android应用中的页面显示速度。
    然后布局影响Android性能的实质:页面的测量 & 绘制时间;
    所以从测量和绘制过程中解决。
    优化方向:布局性能、布局层级、布局复用性 和 测量 & 绘制时间

    1,选择 耗费性能较少的布局

    • 性能耗费低的布局 = 功能简单 = FrameLayout、LinearLayout
    • 性能耗费高的布局 = 功能复杂 = RelativeLayout


      image.png

    2 减少布局的层级

    • 原理:布局层级少 ->> 绘制的工作量少 ->> 绘制速度快 ->> 性能提高
    • 优化方式:使用布局标签<merge> & 合适选择布局类型
      配合<include>标签使用,可优化 加载布局文件时的资源消耗
      // 使用说明:
        // 1. <merge>作为被引用布局A的根标签
        // 2. 当其他布局通过<include>标签引用布局A时,布局A中的<merge>标签内容(根节点)会被去掉,在<include>里存放的是布局A中的<merge>标签内容(根节点)的子标签(即子节点),以此减少布局文件的层次
    
       /** 
         * 实例说明:在上述例子,在布局B中 通过<include>标签引用布局C
         * 此时:布局层级为 =  RelativeLayout ->> Button 
         *                                  —>> RelativeLayout ->> Button
         *                                                     ->> TextView
         * 现在使用<merge>优化:将 被引用布局C根标签 的RelativeLayout 改为 <merge>
         * 在引用布局C时,布局C中的<merge>标签内容(根节点)会被去掉,在<include>里存放的是布局C中的<merge>标签内容(根节点)的子标签(即子节点)
         * 即 <include>里存放的是:<Button>、<TextView>
         * 此时布局层级为 =  RelativeLayout ->> Button 
         *                                ->> Button
         *                                ->> TextView
         * 即 已去掉之前无意义、多余的<RelativeLayout>
         */  
    
         // 被引用的公共部分:布局C = layout_c.xml
         <?xml version="1.0" encoding="utf-8"?>
        <merge xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_10"/>
    
            <TextView
                android:id="@+id/textview"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_10"/>
    
        </merge>
    
        // 布局B:layout_b.xml
        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <Button
                android:id="@+id/Button"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="@dimen/dp_10" />
    
            <include layout="@layout/layout_c.xml" />
    
        </RelativeLayout>
    

    3 提高 布局 的复用性

    原因提取布局间的公共部分,通过提高布局的复用性从而减少测量 & 绘制时间
    优化方案
    使用 布局标签 <include>

    // 使用说明:
      // a. 通过<include>标签引入抽取的公共部分布局C
      // b. <include>标签所需属性 = 公共部分的layout属性,作用 = 指定需引入、包含的布局文件
    
    // 实例说明:抽取 布局A、B中的公共部分布局C & 放入到布局B中使用
    
       /** 
         * 布局B:layout_b.xml
         */  
        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <Button
                android:id="@+id/Button"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="@dimen/dp_10" />
    
            // 通过<include>标签引入抽取的公共部分布局C
            // <include>标签所需属性 = 公共部分的layout属性,作用 = 指定需引入、包含的布局文件
            <include layout="@layout/layout_c.xml" />
    
        </RelativeLayout>
    
        /** 
         * 公共部分的布局C:layout_c.xml
         */
         <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_10"/>
    
            <TextView
            android:id="@+id/textview"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_10"/>
    
        </RelativeLayout>
    

    4, 减少初次测量 & 绘制时间 ViewStub 如:进度显示布局、信息出错出现的提示布局等

    // 使用说明:
        // 1. 先设置好预显示的布局
        // 2. 在其他布局通过<ViewStub>标签引入外部布局(类似<include>);注:此时该布局还未被加载显示
        // 3. 只有当ViewStub被设置为可见 / 调用了ViewStub.inflate()时,ViewStub所指向的布局文件才会被inflate 、实例化,最终 显示<ViewStub>指向的布局
    
       /** 
         * 实例说明:在布局A中引入布局B,只有在特定时刻C中才显示
         */  
    
         // 步骤1:先设置好预显示的布局B = layout_b.xml
         <?xml version="1.0" encoding="utf-8"?>
         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_10"/>
    
            <TextView
                android:id="@+id/textview"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_10"/>
    
        </RelativeLayout>
    
        // 步骤2:在布局A通过<ViewStub>标签引入布局B(类似<include>);注:此时该布局还未被加载显示
        // 布局A:layout_a.xml
        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <Button
                android:id="@+id/Button"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="@dimen/dp_10" />
    
            <ViewStub
                android:id="@+id/Blayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout="@layout/layout_b" />
    
        </RelativeLayout>
    
        // 步骤3:只有当ViewStub被设置为可见 / 调用了ViewStub.inflate()时,ViewStub所指向的布局文件才会被inflate 、实例化,最终 显示<ViewStub>指向的布局
        ViewStub stub = (ViewStub) findViewById(R.id.Blayout);   
        stub.inflate();
    
    // 特别注意
     // 1. ViewStub中的layout布局不能使用merge标签,否则会报错
     // 2. ViewStub的inflate只能执行一次,显示了之后,就不能再使用ViewStub控制它了
    // 3. 与View.setVisible(View.Gone)的区别:View 的可见性设置为 gone 后,在inflate 时,该View 及其子View依然会被解析;而使用ViewStub就能避免解析其中指定的布局文件,从而节省布局文件的解析时间 & 内存的占用
    

    相关文章

      网友评论

          本文标题:Android性能优化:布局优化

          本文链接:https://www.haomeiwen.com/subject/yzkjsctx.html