美文网首页
android性能优化笔记,UI篇

android性能优化笔记,UI篇

作者: 紫阚 | 来源:发表于2016-07-21 10:57 被阅读60次

下面是项目中优化重构时一些心得,陆续会补全

1. 减少UI层级

1.1 减少不必要的层级
<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="#ffffff">

       <net.xuele.xuelets.ui.widget.custom.ResourceView
        android:id="@+id/resourse_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

外面那层布局可以删掉,修改后如下

<net.xuele.xuelets.ui.widget.custom.ResourceView    
      xmlns:android="http://schemas.android.com/apk/res/android"   
      android:id="@+id/resourse_content"    
      android:layout_width="wrap_content"   
      android:layout_height="wrap_content" 
      android:background="#ffffff"
/>
1.2:善用<merge>减少布局层级

android的布局xml文件,需要一个顶级容器来容纳子节点,使用merge标签代替顶级容器,可以减少一层UI嵌套。

比如有个自定义的控件ResourceView,继承自RelativeLayout
xml文件如下,RelativeLayout就是顶级容器

<?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">  
     <ImageView     
       android:layout_width="match_parent"   
       android:layout_height="match_parent"    
       android:scaleType="fitXY"   
       android:src="@mipmap/yso"/>
</RelativeLayout>

此时我们使用<merge>标签代替<RelativeLayout>作为顶级容器时,表示不指定子节点ImageView的顶级容器,ImageView将被直接添加到ResourceView里。

减少UI层级优化结果

经过1.1、1.2优化之后,布局文件减少了两个层级。


before.jpg after.png

相关文章

网友评论

      本文标题:android性能优化笔记,UI篇

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