Android性能优化

1.布局优化
1.1使用标签重用layout
项目种使用共通ui时,多个页面绘制布局会出现重复,不容易后期维护等问题,就需要将相同布局元素提出,写一个共通的xml布局文件
如:
通用layout:topbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />
</LinearLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.day.read.MainActivity">

    <include layout="@layout/topbar"
        />



</RelativeLayout>

注意:如需要在标签覆盖类似原布局中的宽、高需指定宽、高属性

1.2使用实现view的延迟加载
有时我们需要布局两个东东,其中一个在特定情况下才会显示,比如点了某个按钮才需显示其布局,这时我们想到我把布局全部写好把暂时不需要的布局先隐藏GONE掉不就ok了么,但系统会将所有画好的ui进行渲染这时就会耗费cpu运行时间效率会降低,但使用却不会去渲染整个布局,而是需要时再去渲染,这样就会提高效率。
topbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />
</LinearLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.day.read.MainActivity">

   <ViewStub
       android:layout_width="match_parent"
       android:layout_height="wrap_content" 
       android:layout="@layout/topbar"/>



</RelativeLayout>

MainActivity:

public class MainActivity extends AppCompatActivity {

    private Button a;
    private ViewStub stub;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        a= (Button) findViewById(R.id.button);
        stub = (ViewStub) findViewById(R.id.stub);
        a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View inflateView = stub.inflate();
                TextView textView = (TextView) inflateView.findViewById(R.id.textView);
                textView.setText("hellow shanghai");

            }
        });

    }

}

这就是常用布局优化,还有就是避免嵌套过多,降低view树的高度

2.性能优化
2.1Android系统内存分配和回收方式
(1)一个APP通常就是一个进程对应一个虚拟机
(2)gc只在heap(堆)剩余空间不够时才发出垃圾回收
(3)前后台切换时有一个ontrimmemory()回调方法用来通知当前app是否处于被系统回收掉的危险
2.2android内存优化方法
(1)频繁使用字符串拼接用stringbuilder
(2)ArrayMap、SparseArray替换HashMap
(3)避免在OnDraw方法里面执行对象创建
(4)对常量使用static修饰符
(5)使用静态方法,静态方法会比普通方法提高15%左右的访问速度
(6)减少成员变量也就是习惯说全局变量的使用,局部变量会随某个方法的结束而结束,成员变量却和activity生命周期一样
(7)减少不必要的对象,使用基础类型会比使用对象更节省资源
2.3内存泄漏
(1)图片问题,bitmap释放以及优化(网上代码很多)
(2)用application context而不是activity context
(3)强引用有可能activity结束时还被其他所引用,这时activity销毁时也不会被gc回收
(4)软引用 (softreference)生命周期会被gc回收

性能优化是一个漫长的过程,所以开发过程中需注意这些小的细节。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值