Android 性能优化(一)

Android 常用到的性能优化的梳理总结

布局优化

Android 绘制/渲染机制

用户体验中很重要的一点就是流畅,所谓流畅拿数据来分析就是帧数达到60帧/秒,就是说每帧的时间控制在16ms.在Android中,系统使用VSYNC信号触发对UI进行绘制,渲染.如果超过这个时间就会出现丢帧,感觉卡顿.

布局优化-层级优化

Android中View的绘制,是通过View视图树,遍历来进行操作的,如果层级嵌套过多,过深,自然就花费的时间长.Google的API建议View树高度不要超过10层

如何做呢?

删除布局中多余的控件和嵌套,选择结构简单的ViewGroup,比如LinearLayout,FrameLayout
.在早期Android,我们创建布局文件Google默认是LinearLayout,而现在默认的是RelativeLayout.在Google看来,通常后者LinearLayout比前者性能高.但是不绝对,一般情况下是的.
- LinearLayout 一般情况下LinearLayout性能和FrameLayout差不多,但是子View嵌套太多性能就低于RelativeLayout
- RelativeLayout 功能相对复杂,布局过程要花费更多的CPU事件.但是他是扁平的不像LinearLayout是嵌套的,当子view较多是,它的性能又高于LinearLayout.

布局优化-使用标签

当多个布局中是要同一个View,比如每个页面上都有一个标题titleBar,就可以使用标签.其思路就是Java代码中,多个类使用相同代码的抽取.

layout/titlebar.xml文件
<?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="30dp"
              android:orientation="horizontal">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:gravity="center"
        android:text="标题"/>

</LinearLayout>
layout/activity_test1.xml文件
<?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">

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

    <TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="我是第一页面"/>

</LinearLayout>
layout/activity_test2.xml文件
<?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">

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="我是第二页面"/>

</LinearLayout>

上述代码通过的方式,指向另外一个文件,这样就不用再把titlebar中内容给在写一次.

注意:
1. 标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖
2. 标签只支持已android:layout_开头的属性(android:id例外)
3. 标签,前提是必须要写layout_width和layout_height两个属性,不然所有的android:layout_开头的属性全部无效.

布局优化-使用标签

一般情况下标签与标签配合使用,目的是减少布局的层级.来达到优化性能的目的.

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        />

</merge>
布局优化-使用ViewStub

ViewStub是一个非常轻量级的组件.它最大的特点就是:只有在需要显示的时候它才会去绘制.在默认其情况下它的大小为0.

写了这样一个效果,有一个按钮,点击后才显示下方文字

<?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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="我是文字"/>

</LinearLayout>

和使用标签类似,在主页面中应用上面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    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="com.heima.testapp.MainActivity">


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="show"
        android:text="显示文字"/>

    <ViewStub
        android:id="@+id/vs_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout="@layout/activity_test1"
        />

</LinearLayout>

在默认情况下中的内容是不显示出来的,



import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewStub;


public class MainActivity extends AppCompatActivity {


    private ViewStub mViewStub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewStub = (ViewStub) findViewById(R.id.vs_test);

    }

    public void show(View view) {
        mViewStub.setVisibility(View.VISIBLE);
    }

}

在点击事件中 我们使用mViewStub.setVisibility(View.VISIBLE); 让他显示出来
同样 使用mViewStub.inflate();也是可以做到的.但是使用inflate(),可以返回布局,还用一通过findViewById找打子view进行操作.

注意!当ViewStub显示出来后ViewStub就不存在了,取而代之就是实际显示出来的布局了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值