Android性能优化之加载速度优化(一)ViewStub使用

     一个体验良好的app不仅仅在内容上需要做的精简和优化,在各个操作的细节上也需要做到极致!app的启动速度、加载页面速度等让一个好的app锦上添花,让用户感觉到你的产品不是一个山寨的产品!笔者将围绕加载速度做一个小的专题,首先介绍的是ViewStub的使用!

    ViewStub extends View,官方解释如下

  A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or      when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views. Therefore,  the ViewStub exists in the view hierarchy until setVisibility(int) or inflate() is invoked. The inflated View is added to the ViewStub's parent with  the ViewStub's layout parameters. Similarly, you can define/override the inflate View's id by using the ViewStub's inflatedId property. 

    之所以先讲解ViewStub,是因为笔者在开发过程中遇到优化app启动速度问题,背景是:现在较复杂的app可能在启动时需要加载很多view,但是显示的只是其中的一个布局,那么启动app就加载很多布局必然会影响到启动速度,启动速度的优化能够对app的体验能够有很大的提高。

   步入正题:

   ViewStub类似于一个空View,但这是一个消耗资源内存很小的控件,当你不需要显示布局但需要将布局加载到父控件上时,使用这个控件就能提高加载速度,如使用ViewPager时,需要先将view先加载到ViewPager,当需要展示布局时,使用ViewStub.inflate()就可以显示你需要展示的布局,具体使用如下:

   <LinearLayout 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=".MainActivity" 
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
    <ViewStub 
        android:id="@+id/stub"
        android:inflatedId="@+id/stubinflate"
        android:layout="@layout/stub_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button 
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/expand"/>
    <Button 
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/shrink"/>
</LinearLayout>

  stub_view.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" >
    
    <ImageView 
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:background="@drawable/ic_launcher"/>
</LinearLayout>

java代码控制显示和隐藏:

Button expandButton = (Button)findViewById(R.id.expand);
        expandButton.setOnClickListener(new OnClickListener()
        {            
            @Override
            public void onClick(View v)
            {                
                if(null == viewStu)
                {
                    ViewStub stub = (ViewStub)findViewById(R.id.stub);
                    stub.setOnInflateListener(new OnInflateListener()
                    {                        
                        @Override
                        public void onInflate(ViewStub stub, View inflated)
                        {
                            System.out.println("inflate success 11111111!!!!");
                        }
                    });
                    viewStu = (LinearLayout)stub.inflate();
                    System.out.println("inflate success 000000000!!!!");
                }
                else
                {
                    viewStu.setVisibility(View.VISIBLE);
                }
            }
        });
        
        Button thrinkButton = (Button)findViewById(R.id.shrink);        
        thrinkButton.setOnClickListener(new OnClickListener()
        {           
            @Override
            public void onClick(View v)
            {
                if(null != viewStu)
                {
                    viewStu.setVisibility(View.GONE);
                }                
            }
        });

       注意stub.setOnInflateListener(new OnInflateListener()一定要在 inflate之前调用,否则inflate之后不会调用onflate方法。 

         ViewStub的一些特点:

         1. ViewStub只能Inflate一次,之后ViewStub对象会被置为空。按句话说,某个被ViewStub指定的布局被Inflate后,就不会够再通过ViewStub来控          制它了,再次使用会导致程序崩溃,所以需要把inflate出来的view赋值保存到内存;

         2. ViewStub只能用来Inflate一个布局文件,而不是某个具体的View,当然也可以把View写在某个布局文件中。

         使用起来也很简单,但是很有用的一个控件!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值