android布局优化和绘制优化
来源:互联网 发布:淘宝联盟分享赚互刷 编辑:程序博客网 时间:2024/05/23 13:54
一、下面介绍几种android布局优化方法:
1、include标签:
可以允许在一个布局中引入另外一个布局。比如说:我们程序的所有界面都有一个公共部分,这个时候这个公共部分就可以提取到一个独立的布局文件中,然后每个布局文件来引用这个公共的布局。(标题栏)
1.<?xml version="1.0" encoding="utf-8"?> 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3. android:layout_width="match_parent" 4. android:layout_height="match_parent" 5. android:orientation="vertical" > 6. 7. <include layout="@layout/titlebar" /> 8. 9. ...... 10. </LinearLayout>
2、merge标签:
merge标签时作为<include>标签的一种辅助扩展来使用的,它的主要作用是:为了防止include引用布局文件时,产生多余的布局嵌套。
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:text="OK" /> <Button android:id="@+id/cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:text="Cancel" /> </merge>
3、ViewStub标签:
仅在需要时才加载布局。ViewStub虽是view的一种,但是它是没有大小,没有绘制,也不参与布局的,所以它的绘制是不会影响布局的。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:hint="@string/edit_something_here" /> <Button android:id="@+id/more" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginRight="20dp" android:layout_marginBottom="10dp" android:text="More" /> <ViewStub android:id="@+id/view_stub" android:layout="@layout/profile_extra" android:layout_width="match_parent" android:layout_height="wrap_content" /> <include layout="@layout/ok_cancel_layout" /> </LinearLayout>
public void onMoreClick() { ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub); if (viewStub != null) { View inflatedView = viewStub.inflate(); editExtra1 = (EditText) inflatedView.findViewById(R.id.edit_extra1); editExtra2 = (EditText) inflatedView.findViewById(R.id.edit_extra2); editExtra3 = (EditText) inflatedView.findViewById(R.id.edit_extra3); } }
备注:ViewStub和merge是不能一起用的。
4、删除布局中 无用控件、无用层级。
5、在满足需求的情况下:LinearLayout和RelativeLayout,应采用LinearLayout。如果功能需要LinearLayout嵌套使用,那就优先使用RelativeLayout。
为什么LinearLayout比RelativeLayout要好:因为在xml解析的时候LinearLayout只需一次measure,而RelativeLayout需要measure两次。
二、绘制优化:
View 的onDraw要避免大量操作,主要体现在两个方面:
(1) 在onDraw中,不要创建新的局部变量。因为onDraw方法可能会被频繁调用(大量的临时对象不仅占用内存,而且还会导致系统更频繁的gc,从而降低程序的执行效率)。
(2) onDraw方法中不要做耗时操作,也不能做成千上万的循环操作,因为它们会导致view的绘制不流畅。
- android布局优化和绘制优化
- Android布局、绘制优化
- Android绘制优化(二)布局优化
- 【Android】の基础——布局优化和过度绘制
- Android布局优化之过度绘制
- android布局优化技巧,防止过度绘制
- Android中View绘制优化之一---- 优化布局层次
- (转)Android中View绘制优化之一---- 优化布局层次
- Android中View绘制优化之一---- 优化布局层次
- Android中View绘制优化之一---- 优化布局层次
- Android中View绘制优化之一---- 优化布局层次
- Android中View绘制优化之一---- 优化布局层次
- Android中View绘制优化之一---- 优化布局层次
- Android-性能优化01-布局优化&绘制优化&内存泄露优化
- Android优化--布局优化
- Andriod 布局过度绘制优化
- 优化Android布局——减少过度绘制
- 优化Android布局——减少过度绘制
- C3D:视频动作分类demo实现
- split空值问题
- 使用spring AOP事务的时候报错的原因
- Android数据存储于IO
- 【JavaEE】The Introduce of 'JSF'
- android布局优化和绘制优化
- 【安全】加密算法
- 数组相关操作(实战基础篇)
- PHP 类的使用与总结
- Java网络编程基础
- C语言实现万年历
- springboot 多数据源
- 【SQL】通过rowid查找及删除重复记录
- spring注入错误