Android performance optimization (1) - layout optimization

Android performance optimization (1) - layout optimization

foreword

Recently, I have been reading some high-quality books non-stop. I have read "Android Development Art Exploration" three times, but not every time I read it in a very meticulous manner. It should be the first time I read it. It's like seeing flowers at a glance. I have an overall understanding of the book. Every time I read it, I focus on it. I learned a lot from it. . I also read the book "In-depth Understanding of Java Virtual Machine", this is more in-depth, it may seem a little laborious, but it is really interesting. It’s a bit far-fetched. The purpose of my blog is very simple. It is to consolidate or summarize the knowledge I have learned. If I am lucky, I can help the children’s boots around me.

  • Performance optimization mainly includes the following: layout optimization, drawing optimization, memory leak optimization, response speed optimization, ListView and BitMap optimization, and thread optimization. In the later period, if I have time, I will write a blog about each module, which can be regarded as a deep understanding. Layout optimization is divided into three parts, namely, our commonly used include tags, as well as merge tags and ViewStub.

  • The include tag is estimated to be the most used. This is very simple. For example, we want to write two buttons in the Activity. Under normal circumstances, we will directly write the code as follows, but if the layout here will be used repeatedly, each time Wouldn't it be very troublesome to write it every time, and it can be encapsulated in the include tag, which greatly reduces our workload. This is often used in, for example, the TitleBar at the top of an Activity.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.oman.touchevent.test.MergeActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button2"
        app:layout_constraintTop_toBottomOf="@id/button1" />

</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.oman.touchevent.test.model.TestActivity">

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

</android.support.constraint.ConstraintLayout>

  • Next, let’s talk about the merge tag, because when we write page layouts, we often pay attention not only to beauty but also to efficient operation. We all know that most Android display screens will update 60 frames per second, which means that the average is 1/60s, That is to say, it takes about 16ms to render the UI. If the rendering time is too long and exceeds this time, the interface will be stuck. At that time, the user experience is extremely poor, and unexpected surprises will be waiting for you. Then we need to speed up the rendering so that the user does not feel stuck. How to do it? Reducing the level of the layout is one of the solutions. Using the merge tag, or in the above example, we will make the outermost layout the outermost layer. The ConstraintLayout layout has been removed, and the merge tag is used, as follows. When using it, you can use the include tag to directly reference it, which will reduce one layout level and speed up the rendering.
<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/button1"
        android:text="button2" />
</merge>
  • Attached below are two pictures to compare the layout level without the merge tag VS the layout level with the merge tag (here I use the Hierarchy View tool that comes with Android, and the friends who don't know how to learn by Baidu), we found here There are two ConstraintLayouts in the layout level without merge, and there is only one ConstraintLayout in the layout level with merge, which is very useful for optimization when the layout level is too nested, and if we use LinearLayout when developing If you can use RelativeLayout, you can use LinearLayout layout. If there are too many levels of nesting, use RelativeLayout and the merge tag above.

Layout hierarchy without merge

Layout hierarchy using merge


  • Next, let's talk about the ViewStub control. The function of this control is that sometimes an interface does not need to display some controls at the beginning, and then displays it when it needs to be displayed, so that the performance consumption of the system will be reduced. Let's talk about the specific use:

  • First write a layout view_stub.xml with two buttons

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

        <Button
            android:id="@+id/edit_extra1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:text="我是ViewStub布局按钮1" />

        <Button
            android:id="@+id/edit_extra2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:text="我是ViewStub布局按钮2" />

    </LinearLayout>
  • We use the ViewStub control to write it into the layout
<ViewStub
        android:id="@+id/view_stub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/view_stub" />
  • The method used in the code is as follows. First, findViewById to find the ViewStub. After finding it, you need to use the method inflate() to fill it to display it. If this method is called multiple times, an error will be reported. At this time, if the inflate() method has been called, but it has been hidden. For this control, if we want to display it again, we need to use setVisibility(View.VISIBLE). Another point to note is that the return value of the inflate() method is the parent class control of ViewStub. The code is as follows
                if (!isShow) {
                    try {
                        //返回的是父布局  如果调用过inflate方法再次调用就会报错,得使用setVisibility方法
                        LinearLayout linearLayout = (LinearLayout) mViewStub.inflate();
                        linearLayout.findViewById(R.id.edit_extra1).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(PreActivity.this, ((Button) v).getText() + "", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } catch (Exception e) {
                        mViewStub.setVisibility(View.VISIBLE);
                    }
                    isShow = true;
                } else {
                    mViewStub.setVisibility(View.GONE);
                    isShow = false;
                }

Conclusion: Today's sharing is here! ! ! Welcome to my other blog posts – link

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325641824&siteId=291194637