浅谈Android界面绘制避免重复渲染
2015-12-08 14:56
501 查看
当你在Application中创建复杂的布局时,页面的渲染过程也变得更加缓慢。
此时,我们需要利用 <include />标签(避免重复渲染)和 ViewStub类(延迟加载)来优化我们的页面。
(原文地址:/article/1336217.html)
一、利用<include />标签来避免重复渲染
当我们需要为App中的每个View都添加一个header或者footer时,你会怎么做?
重复地复制粘贴可以解决这个问题,但未免太繁杂。可以试着使用<include />标签:
第一种方式,在<include />标签内指定width及height:
main.xml
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
<Button
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="center_vertical"
android:onClick ="onShowMap"
android:text ="@string/show_map" />
<include
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_alignParentBottom ="true"
android:layout_marginBottom ="30dp"
layout ="@layout/footer" />
</RelativeLayout>
footer.xml
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
<TextView xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "0dp"
android:layout_height= "0dp"
android:gravity= "center"
android:text= "@string/footer_text" />
[align=left]有个小细节需要注意,在footer.xml中,我们将width及height都设为0dp.目的是为了配合<include/>标签中对width及height的定义。[/align]
[align=left]第二种方式,直接引用:[/align]
[align=left]main.xml[/align]
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
<Button
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="center_vertical"
android:onClick ="onShowMap"
android:text ="@string/show_map" />
<include layout ="@layout/footer" />
</RelativeLayout>
footer.xml
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
<TextView xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:layout_alignParentBottom= "true"
android:layout_marginBottom= "30dp"
android:gravity= "center"
android:text= "@string/footer_text" />
[align=left]二、利用ViewStub类来延迟加载视图[/align]
[align=left]在设计视图时,有时会考虑到某些视图的可见性是依赖于用户的操作或者运行设备的具体环境的。[/align]
[align=left]此时你会如何设计?仅仅是改变View的visible属性?[/align]
[align=left]我们先来看看ViewStub的介绍:[/align]
[align=left] ViewStub是一个不可见、不占空间(zero-sized)的控件,它可以用来在运行时延迟加载视图资源。只有当我们将ViewStub的可见性设为true,或者调用inflate()方法,它的视图资源才会被加载。[/align]
[align=left]假设我们设计的一个页面中包含地图View,试想一下以下这种情况:[/align]
[align=left] 有些用户不需要看到地图。[/align]
[align=left]既然用户不需要看到地图,我们为何还坚持不懈地加载它?这反而影响了我们App的Performance。[/align]
[align=left]此时,我们就可以利用ViewStub类了:[/align]
[align=left]main.xml[/align]
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
<Button
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="center_vertical"
android:onClick ="onShowMap"
android:text ="@string/show_map" />
<ViewStub
android:id ="@+id/map_stub"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:inflatedId ="@+id/map_view"
android:layout ="@layout/map" />
</RelativeLayout>
map.xml
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
<com.google.android.maps.MapView xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:apiKey= "my_api_key"
android:clickable= "true" />
接下来看看MainActivity
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
public class MainActivity extends MapActivity {
private View mViewStub;
@Override
public void onCreate (Bundle savedInstanceState ) {
super. onCreate( savedInstanceState );
setContentView( R. layout. main);
mViewStub = findViewById( R. id. map_stub);
}
public void onShowMap (View v) {
mViewStub. setVisibility (View .VISIBLE );
}
....
}
如你所见,在需要显示图像时我们才调用onShowMap来改变map_stub的可见性。在改变之前,map_stub都不会渲染加载视图资源。
[align=left]小结:[/align]
[align=left] 1.当我们的页面变得复杂,XML文件内容过多时,<include />标签可以有效地帮助我们整理文件内容,同时提高了XML文件的可读性。同时,它的用法也与Fragment类似。[/align]
[align=left] 2.ViewStub是一个极佳的延迟加载视图资源的方式。只要你设计的视图是依赖于上下文来改变其可见性的,就利用ViewStub类吧。也许当你只将其应用在一个简单的页面当中时,并不会感觉到在性能上有任何提升,但是在复杂页面中,它的效果是极佳的。[/align]
此时,我们需要利用 <include />标签(避免重复渲染)和 ViewStub类(延迟加载)来优化我们的页面。
(原文地址:/article/1336217.html)
一、利用<include />标签来避免重复渲染
当我们需要为App中的每个View都添加一个header或者footer时,你会怎么做?
重复地复制粘贴可以解决这个问题,但未免太繁杂。可以试着使用<include />标签:
第一种方式,在<include />标签内指定width及height:
main.xml
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
<Button
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="center_vertical"
android:onClick ="onShowMap"
android:text ="@string/show_map" />
<include
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_alignParentBottom ="true"
android:layout_marginBottom ="30dp"
layout ="@layout/footer" />
</RelativeLayout>
footer.xml
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
<TextView xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "0dp"
android:layout_height= "0dp"
android:gravity= "center"
android:text= "@string/footer_text" />
[align=left]有个小细节需要注意,在footer.xml中,我们将width及height都设为0dp.目的是为了配合<include/>标签中对width及height的定义。[/align]
[align=left]第二种方式,直接引用:[/align]
[align=left]main.xml[/align]
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
<Button
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="center_vertical"
android:onClick ="onShowMap"
android:text ="@string/show_map" />
<include layout ="@layout/footer" />
</RelativeLayout>
footer.xml
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
<TextView xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:layout_alignParentBottom= "true"
android:layout_marginBottom= "30dp"
android:gravity= "center"
android:text= "@string/footer_text" />
[align=left]二、利用ViewStub类来延迟加载视图[/align]
[align=left]在设计视图时,有时会考虑到某些视图的可见性是依赖于用户的操作或者运行设备的具体环境的。[/align]
[align=left]此时你会如何设计?仅仅是改变View的visible属性?[/align]
[align=left]我们先来看看ViewStub的介绍:[/align]
[align=left] ViewStub是一个不可见、不占空间(zero-sized)的控件,它可以用来在运行时延迟加载视图资源。只有当我们将ViewStub的可见性设为true,或者调用inflate()方法,它的视图资源才会被加载。[/align]
[align=left]假设我们设计的一个页面中包含地图View,试想一下以下这种情况:[/align]
[align=left] 有些用户不需要看到地图。[/align]
[align=left]既然用户不需要看到地图,我们为何还坚持不懈地加载它?这反而影响了我们App的Performance。[/align]
[align=left]此时,我们就可以利用ViewStub类了:[/align]
[align=left]main.xml[/align]
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
<Button
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="center_vertical"
android:onClick ="onShowMap"
android:text ="@string/show_map" />
<ViewStub
android:id ="@+id/map_stub"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:inflatedId ="@+id/map_view"
android:layout ="@layout/map" />
</RelativeLayout>
map.xml
[html] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
<com.google.android.maps.MapView xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:apiKey= "my_api_key"
android:clickable= "true" />
接下来看看MainActivity
[java] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
public class MainActivity extends MapActivity {
private View mViewStub;
@Override
public void onCreate (Bundle savedInstanceState ) {
super. onCreate( savedInstanceState );
setContentView( R. layout. main);
mViewStub = findViewById( R. id. map_stub);
}
public void onShowMap (View v) {
mViewStub. setVisibility (View .VISIBLE );
}
....
}
如你所见,在需要显示图像时我们才调用onShowMap来改变map_stub的可见性。在改变之前,map_stub都不会渲染加载视图资源。
[align=left]小结:[/align]
[align=left] 1.当我们的页面变得复杂,XML文件内容过多时,<include />标签可以有效地帮助我们整理文件内容,同时提高了XML文件的可读性。同时,它的用法也与Fragment类似。[/align]
[align=left] 2.ViewStub是一个极佳的延迟加载视图资源的方式。只要你设计的视图是依赖于上下文来改变其可见性的,就利用ViewStub类吧。也许当你只将其应用在一个简单的页面当中时,并不会感觉到在性能上有任何提升,但是在复杂页面中,它的效果是极佳的。[/align]
相关文章推荐
- Android studio gradle 打包 那些事
- android设备获取 关于手机-》手机名称
- Android基于插件的开发
- android中list在remove()时出现java.lang.IndexOutOfBoundsException异常
- 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高
- android加壳利器-apkprotectlite
- androidstudio的Rendering Problems Missing styles错误解决
- Android——客户端日期定制显示
- 【原】Android高德地图开发——路况+定位
- Android群英传》读书笔记 (4) 第八章 Activity和Activity调用栈分析 + 第九章 系统信息与安全机制 + 第十章 性能优化
- Android硬件加速
- Android SharedPreferences使用以及原理详解
- Android中监听Home键的4种方法总结
- Ant开篇之安装
- Android群英传》读书笔记 (3) 第六章 Android绘图机制与处理技巧 + 第七章 Android动画机制与使用技巧
- android中ViewPager图片滑动和自动播放
- 《Android开发艺术探索》读书笔记 (13) 第13章 综合技术、第14章 JNI和NDK编程、第15章 Android性能优化
- Android开发艺术探索》读书笔记 (12) 第12章 Bitmap的加载和Cache
- 《Android开发艺术探索》读书笔记 (11) 第11章 Android的线程和线程池
- Android setAdapter addHeaderView 报错解决方案