可能有些人不明白什么是过分绘制,简单言,咱们app一个页面所显示的效果是由像素一帧一帧绘制而成。过分绘制就是意味着这一帧被绘制屡次。若是是静态的布局,可能影响不是很大,若是是动态的,好比ListView,GridView,ViewPager等在性能上就会差一点,常见的好比listView上下滑动,过分绘制的状况下,就会出现卡顿,或者跳跃感很明显。 固然过分绘制确定没法避免,咱们只能减小没必要要的绘制,那么如何看的出来,一个页面是否过分绘制呢?node
下面咱们来看一下,下面说的这个工具只有Android 4.2以上有此功能。android
1.打开咱们的手机设置--开发者选项--调试GPU过分绘制(可能有些手机不是这样的叫法,有的是显示GPU过分绘制,根据手机而来),选择显示过分绘制区域,此时手机页面会显示成这样,不要惊慌。。windows
这里给你们介绍下,绘制颜色的标识,从好到差:蓝色(1x次绘制)-》浅绿色(2x绘制)-》淡红色(3x绘制)-》红色(4x绘制)。app
2.怎么减小过分绘制ide
通常状况下,最好把绘制控制在2次如下,3次绘制有时候是不能避免的,尽可能避免,4次的绘制基本上是不容许的。工具
怎么减小绘制是咱们最关心的,咱们来看一个图(本身项目里面的。。咱们以图片下面的ListView为例子)从图上看的出来,被绘制3次甚至4次,布局
咱们来看下代码:listView和item性能
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <ListView 7 android:id="@+id/xListView" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:layout_marginBottom="50dp" 11 android:background="@color/white" 12 android:divider="@drawable/self_center_divider" 13 android:dividerHeight="4px" 14 android:headerDividersEnabled="false" 15 android:listSelector="@drawable/item_selector" 16 android:scrollbars="none" > 17 </ListView> 18 19 </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="wrap_content" android:background="@color/white"> <RelativeLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/self_center_item_bg"> <ImageView android:id="@+id/item_img_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dp" /> <TextView android:id="@+id/item_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="25dp" android:layout_toRightOf="@id/item_img_view" android:textColor="@color/text_color_darker" android:textSize="15sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/right_arrow_gray" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="20dp"/> </RelativeLayout> </RelativeLayout>
红色标记出来的,是问题的所在,背景颜色绘制了三次,最后一个是选择器,由于有点击效果,我如今把前2个都给删掉,只留最后一个,如今咱们看一下图片。看是否是达到预期的效果。。。。优化
这里只是一个简单的例子,当让有不少缘由组成,咱们能够从如下几个方面着手spa
第一:若是是相对比较复杂的布局,建议使用相对布局。
第二:若是须要多层嵌套,咱们可使用merge标签来减小嵌套。
第三:减小背景颜色的屡次绘制
第四:对于使用Selector当背景的Layout(好比ListView的Item,会使用Selector来标记点击,选择等不一样的状态),能够将normal状态的color设置 为”@android:color/transparent”,来解决对应的问题
第五:当一个页面有多种不一样的显示效果,最多见的是listview 中的多种item布局,咱们能够采用抽取的方式
等等。。。。
下面我在为你们简单介绍如下Android studio自带的工具,
首先 打开 Android Device mointor,找到 Hierarychy view (这里我简称 视图树)
将你要查看的项目运行到模拟器或者真机上,在Android device mointor 上windows 找到当前的模拟器或者真机,找到当前的项目,
如图:点击当前项目某一个布局,在View里面会显示当前这个布局的各个节点图,而后点击3(profile node),在视图里面就会显示4上面的三个点
他们分别表示 测量 布局 绘制,再次点击的时候,咱们就能够看到子节点上着三个圆点在变化,他有3个颜色,绿色,黄色,红色,红色表明着耗时最长,也就意味着咱们须要优化,咱们能够不断点击,查看 测量布局以及绘制所须要的时间,从而优化。。。
就简单说道这里,你们能够研究一下,有点晚了,晚安!!!