Android应用开发之Android性能优化教程之布局优化详解
凌雪 2018-09-21 来源 :网络 阅读 578 评论 0

摘要:本文将带你了解Android应用开发之Android性能优化教程之布局优化详解,希望本文对大家学Android有所帮助。

本文将带你了解Android应用开发之Android性能优化教程之布局优化详解,希望本文对大家学Android有所帮助。


今天,我们继续Android性能优化系列   -   布局优化。在Android中,UI布局作为展示性的标志,显示的速度直接体现了一个App对于客户直观的影响。一个好的App,在布局和UI上肯定有比较好的性能优化,所以布局优化成为了Android性能优化的有一个重点。
    Android中关于布局优化,系统为我们提供了几个抽象的标签:
    (1)include
    (2)viewstub
    (3)merge
    下面,我们就来一一介绍三个标签的作用和使用方式。
    1> include
    顾名思义,include标签可以让我们复用包含一个layout布局。例如,我们App在大部分情景下都是这样的设计:topbar ->   content -> footbar。
    那么,很多界面都是这样的布局,我们就可以抽取出topbar和footbar作为公共布局,使用include就可以复用该布局,下面看代码:
    这是一个头布局:layout_topbar.xml
    ?1234567891011<!--xml version="1.0"   encoding="utf-8"--><linearlayout   android:layout_height="wrap_content"   android:layout_width="match_parent"   android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android"> <relativelayout   android:id="@+id/rl_root_title_base"   android:layout_height="46dp"   android:layout_width="match_parent">  <textview   android:gravity="center_vertical"   android:id="@+id/tv_left_title"   android:layout_centervertical="true"   android:layout_height="wrap_content"   android:layout_marginleft="10dp"   android:layout_width="wrap_content" android:textcolor="#80C469">  <textview   android:ellipsize="end" android:id="@+id/tv_topic_title"   android:layout_centerinparent="true"   android:layout_height="wrap_content"   android:layout_width="wrap_content"   android:maxlength="10" android:singleline="true"   android:textcolor="@color/cl_font_46"   android:textsize="18dp">  <textview   android:gravity="center" android:id="@+id/tv_right_first_title"   android:layout_alignparentright="true"   android:layout_centervertical="true"   android:layout_height="wrap_content"   android:layout_marginright="10dp"   android:layout_width="wrap_content" android:minheight="30dp"   android:minwidth="30dp"   android:textcolor="@color/cl_80c469"   android:textsize="16dp">    <textview   android:gravity="center" android:id="@+id/tv_right_second_title"   android:layout_centervertical="true"   android:layout_height="wrap_content"   android:layout_marginright="10dp"   android:layout_toleftof="@id/tv_right_first_title"   android:layout_width="wrap_content"   android:minheight="30dp" android:minwidth="30dp"   android:textcolor="#80C469"> </textview></textview></textview></textview></relativelayout><view   android:background="@color/cl_font_ea"   android:id="@+id/line_root_base"   android:layout_height="0.5dp"   android:layout_width="match_parent"></view></linearlayout>
    main.xml
    ?123<linearlayout android:layout_height="match_parent"   android:layout_width="match_parent"   android:orientation="vertical"   xmlns:android="https://schemas.android.com/apk/res/android"   xmlns:app="https://schemas.android.com/apk/res-auto"> <include   layout="@layout/layout_top"></include></linearlayout>
      上面代码中我们使用include的属性layout来重用topbar布局。include还可以指定layout_width和layout_height以及id。如果指定了id属性,则被包含的根布局id将会失效,此id将会被覆盖。
    2> viewstub
    viewstub标签和include标签很相似,都是使用layout属性来加载一个布局。不同之处是include标签用来重用布局,该布局会在App启动加载时被加载进内存。而viewstub标签所引用的布局是不会被加载出来的,只有当你在某个时间需要使用时才会被加载出来,从而在解析layout时节省了cpu加载时间和内存的占用。
    使用也非常简单,看下面代码:
    ?123<linearlayout android:layout_height="match_parent"   android:layout_width="match_parent"   android:orientation="vertical"   xmlns:android="https://schemas.android.com/apk/res/android"   xmlns:app="https://schemas.android.com/apk/res-auto"> <viewstub   android:id="@+id/vstub_network_error"   android:layout="@layout/layout_network_error"   android:layout_height="match_parent"   android:layout_width="match_parent"></viewstub></linearlayout>
    在布局代码中,我们使用ViewStub的layout来加载一个布局。
    ?1234567891011private void showEmptyView()   {  if(ValidateUtils.isNullObj(viewNetWorkError)){   ViewStub   mViewStub = (ViewStub)   findViewById(R.id.vstub_network_error);  viewNetWorkError =   mViewStub.inflate(); } else   {   viewNetWorkError.setVisibility(View.VISIBLE); }}
    ?1private void showCourseContent(Course course) {
    ?123if(ValidateUtils.isNotNullObj(viewNetWorkError))   { viewNetWorkError.setVisibility(View.GONE);}
    ?1}
    我们在代码中通过findViewById来获取ViewStub,然后调用其inflate方法就可以获取到layout的view。
    ViewStub的使用场景很广泛:例如显示加载失败的界面,进度布局显示,信息出错提示等等。
    3> merge
      merge标签用来取消layout的层级的显示。大家都知道,Android顶级布局DecorView的根布局是让FrameLayout。此时在我们的布局中如果我们使用FrameLayout,就会造成布局节点的层级叠加重复绘制。Android为我们提供了一个叫做hierarchy   viewer来查看布局节点信息。该工具在sdk/tools目录下。
    要注意的一点是,merge标签必须作为布局的根标签,即不能嵌套在其他布局中。
    使用很简单,就不贴代码了。
    介绍完了以上3个抽象标签的使用方式,想必大家都知道该如何去应用在自己的布局中来优化布局,下面还有几点同时也需要大家去关注一下:
    (1)inflate:大家都知道,inflate用来在代码中加载一个现有的布局。例如我们可以使用LayoutInflater.from().inflate()来发加载,同时也可以在Activity中使用getLayoutInflater().inflate()来加载一个布局。但是每次使用inflate去加载布局是有时间和内存消耗的。所以我们要利用复用View来减少不必要的重复inflate。例如上面我们在介绍ViewStub的时候会去inflate。此时我们就可以将该View当做全局变量。下次只需要判断下是否为null,就可以去重复使用,而不必每次都inflate加载了。
    (2)ListView   Adapter的getView方法:在该方法中我们都会去inflate一个布局,然后设置一些数据,最后return该View。ListView在每一个Item被渲染加载时都回去重复调用getView。并且ListView的convertView是可以重复利用的,所以我们可以利用ViewHolder的原理来复用convertView,而不必每次都inflate。
    (3)使用SurfaceView和TextureView:SurfaceView和TextureView不会占用UI   Thread来加载渲染视图,他们都会开启一个线程来异步进行加载。所以可以使用他们来优化加载速度及显示效果。
    (4)尽量为所有分辨率创建资源:减少不必要的硬件缩放,这样会降低UI的绘制速度和占用大量的内存消耗。
      注意:之前看到网上很多朋友说将View的可见性设置为gone。该View同样不会被加载到内存。其实这种说法是错误的。从Android源码可以知道,即使将View的visiablity设置为gone,View同样会被加载的内存资源当中。即只有只有ViewStub加载的layout在App启动后才不会被加载到内存资源。
    好了,关于布局优化的内容就介绍到这里了。更多的东西还需要大家去尝试一下。下一篇我将和大家分享Android性能优化 一 数据优化    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注移动开发之Android频道!

本文由 @凌雪 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程