云南seo简单整站优化,wordpress模板yunnut,帮别人做网站怎么赚钱,青岛公司做网站从五年前刚接触Android开发#xff0c;到初入职场。不管想写什么小demo#xff0c;或者做什么课程设计#xff0c;拿到需求#xff0c;我就开始自己的线性布局之旅了。不管多么复杂的布局#xff0c;我几乎都能通过各种嵌套来实现。但是#xff0c;逐渐的#xff0c;我也… 从五年前刚接触Android开发到初入职场。不管想写什么小demo或者做什么课程设计拿到需求我就开始自己的线性布局之旅了。不管多么复杂的布局我几乎都能通过各种嵌套来实现。但是逐渐的我也听到身边的同事说线性布局嵌套会导致过度绘制影响性能等等的说法。那时我还一脸懵逼。确实那时候我也只是想着完成功能。 那么为什么我想写一篇文章来对比一下这三种布局的性能呢因为以前我用线性布局有同事说相对布局好一些然后我会适量的使用相对布局。后来随着约束布局的出现和使用人群日渐增多我在否定约束布局很久后终于也没逃过“真香定理”。确实约束布局她太香了。尝试过一次约束布局后我开始大量的使用以至于我最喜欢的线性布局我都很少去触碰它了。但是我确实没有自己去验证一下这三种布局是否在性能上的差别。好接下来我就以我的方式去对比一下这三种布局的性能。
一.从需求入手 首先我们给自己一个需求。就做一个老生常谈的登录界面吧很简单的登录界面 二.性能比较 首先说明一下这里的性能比较是在不嵌套布局的情况下去对比。我们分别用线性布局相对布局和约束布局去实现上面的登录界面。比较方式我们在这里也没有去详细的比较绘制的三个流程分别耗时多少我们就打印一下setContentView的耗时情况。注1不统计除此启动耗时2均为杀死进程后重新启动3测试手机为小米Max 2 long startTime System.currentTimeMillis();setContentView(R.layout.activity_main_linear);long endTime System.currentTimeMillis();long totalTime endTime-startTime;Log.d(TTTT,totalTime:totalTime);
1.线性布局
1代码
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticaltools:context.MainActivityEditTextandroid:idid/edt_1android:layout_width200dpandroid:layout_height40dpandroid:layout_gravitycenter_horizontalandroid:layout_marginTop200dp /EditTextandroid:idid/edt_2android:layout_width200dpandroid:layout_height40dpandroid:layout_gravitycenter_horizontalandroid:layout_marginTop10dp /Buttonandroid:idid/buttonandroid:layout_width200dpandroid:layout_height40dpandroid:textlogin inandroid:layout_gravitycenter_horizontalandroid:layout_marginTop10dp //LinearLayout 2耗时情况
线性布局耗时表单位ms 12345678910平均130129131130129132131131129129130 2.相对布局
1代码
?xml version1.0 encodingutf-8?
RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivityEditTextandroid:idid/edt_1android:layout_width200dpandroid:layout_height40dpandroid:layout_alignParentToptrueandroid:layout_centerHorizontaltrueandroid:layout_marginTop200dp /EditTextandroid:idid/edt_2android:layout_width200dpandroid:layout_height40dpandroid:layout_belowid/edt_1android:layout_centerHorizontaltrueandroid:layout_marginTop10dp /Buttonandroid:idid/buttonandroid:layout_width200dpandroid:layout_height40dpandroid:layout_belowid/edt_2android:textlogin inandroid:layout_centerHorizontaltrueandroid:layout_marginTop10dp //RelativeLayout
2耗时情况
相对布局耗时表单位ms 12345678910平均132130130131128129130127129130130
3.约束布局
1代码
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivityEditTextandroid:idid/edt_1android:layout_width200dpandroid:layout_height40dpandroid:layout_marginTop200dpapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent /EditTextandroid:idid/edt_2android:layout_width200dpandroid:layout_height40dpandroid:layout_marginTop10dpapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/edt_1 /Buttonandroid:idid/buttonandroid:layout_width200dpandroid:layout_height40dpandroid:layout_marginTop10dpandroid:textlogin inapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/edt_2 //androidx.constraintlayout.widget.ConstraintLayout
2耗时情况
约束布局耗时表单位ms 12345678910平均160163163162161159168168156158162
4.比较结果 经过上面的数据我们可以看出在简单布局下相对布局和线性布局的耗时情况差不多。但是约束布局的耗时反而会多了30ms。大家都知道在不考虑布局嵌套的情况下相对布局由于会在measure中计算两次会导致耗时比线性布局稍微多一些。而约束布局是相对布局的进化版在处理简单布局的时候耗时确实有所增加。
三.个人观点 根据自己的实际工作以及了解到的一些同事对布局的使用情况说一下个人的几个观点
1在布局简单的情况下优先使用LinearLayout其次考虑RelativeLayout和ConstraintLayout。原因简单布局下LinearLayout确实简单且耗时不多代码量也相对少一些。
2在布局比较复杂的情况下优先使用RelativeLayout和ConstraintLayout不建议使用LinearLayout各种嵌套。原因布局过多层次的嵌套会增加绘制时间。
3不必刻意追求使用ConstraintLayout如果你习惯了使用RelativeLayout。原因ConstraintLayout和RelativeLayout在处理复杂布局时我没做过更多的耗时比较。但是有很多人确实习惯使用RelativeLayout那么也不必强行使用约束布局因为有些人会觉得约束布局会写很多的代码。。。