性能优化 ViewStub动态控件 与 tools命名空间简单使用

来源:互联网 发布:unity3d转场效果 编辑:程序博客网 时间:2024/05/20 21:57

    ViewStub动态控件

    ViewStub 是一个隐藏的,不占用内存空间的视图对象,它可以在运行时延迟加载布局资源文件。换言之,在不加载布局资源的时候,ViewStub的试图对象不占用系统资源,只有在加载该布局的时候才加载。所以,我们可以将以前单次使用View的显示与隐藏来实现展示不同界面的地方使用ViewStub,对app进行性能优化。

    ViewStub的特点:ViewStub加载对象是一个布局对象,加载时需要一个布局文件id(R.layout.xxx),而不是控件id(R.id.xxx); ViewStubd的布局对象只能加载一次,加载之后便丢失布局的,再次加载抛出IllegalStateException异常。所以实现反复的隐藏与显示只能通过View的可见性来控制。

    下面对ViewStub进行简单的使用:

   布局文件:

<?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"    tools:context="com.example.ffengz.viewstubtest.MainActivity">    <ViewStub        android:id="@+id/stub_text1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout="@layout/layout_text1"        />    <ViewStub        android:id="@+id/stub_text2"        android:layout="@layout/layout_text2"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/btn_show_one"        android:text="展示第一个Stub"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/btn_show_two"        android:text="展示第二个Stub"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></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:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        tools:text="第一个加载数据"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>
   完整代码:

package com.example.ffengz.viewstubtest;import android.content.Context;import android.support.annotation.NonNull;import android.support.annotation.StringRes;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.ViewStub;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private ViewStub stub1;    private ViewStub stub2;    private Button btnShowOne;    private Button btnShowTwo;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initUI();    }    private void initUI() {        stub1 = (ViewStub) findViewById(R.id.stub_text1);        stub2 = (ViewStub) findViewById(R.id.stub_text2);        btnShowOne = (Button) findViewById(R.id.btn_show_one);        btnShowTwo = (Button) findViewById(R.id.btn_show_two);        btnShowOne.setOnClickListener(this);        btnShowTwo.setOnClickListener(this);    }    @Override    public void onClick(View v) {        int id = v.getId();        switch (id) {            case R.id.btn_show_one:                DynamicLoadHolder.loadViewStubOne(this, stub1, R.string.text_one);                // 隐藏另一个ViewStub                stub2.setVisibility(View.GONE);                break;            case R.id.btn_show_two:                DynamicLoadHolder.loadViewStubTwo(this, stub2, R.string.text_two1, R.string.text_two2);                stub1.setVisibility(View.GONE);                break;            default:                Toast.makeText(this, "未知异常!", Toast.LENGTH_SHORT).show();        }    }    /**     * 动态加载     */    private static class DynamicLoadHolder {        /**         * 动态加载布局方法         *         * @param viewStub 加载对象         * @param text     文本展示内容         */        static void loadViewStubOne(@NonNull Context context, @NonNull ViewStub viewStub,                                    @StringRes int text) {            View infalte = null;            // 捕获重复加载抛出的异常            try {                infalte = viewStub.inflate();                viewStub.setTag(infalte);            } catch (Exception e) { // ViewStub只加载一次,如果加载过,直接显示                Log.i("info", "loadViewStub: =====" + e.toString());                infalte = (View) viewStub.getTag();                viewStub.setVisibility(View.VISIBLE);            } finally {                // 对ViewStub中的控件进行逻辑、UI处理                if (infalte != null) {                    TextView textViewOne = (TextView) infalte.findViewById(R.id.text_one);                    textViewOne.setText(context.getResources().getString(text));                }            }        }        static void loadViewStubTwo(@NonNull Context context, @NonNull ViewStub viewStub,                                    @StringRes int text1, @StringRes int text2) {            View infalte = null;            try {                infalte = viewStub.inflate();                viewStub.setTag(infalte);            } catch (Exception e) { // ViewStub只加载一次,如果加载过,直接显示                infalte = (View) viewStub.getTag();                viewStub.setVisibility(View.VISIBLE);            } finally {                if (infalte != null) {                    TextView textViewTwo1 = (TextView) infalte.findViewById(R.id.text_two1);                    textViewTwo1.setText(context.getResources().getString(text1));                    TextView textViewTwo2 = (TextView) infalte.findViewById(R.id.text_two2);                    textViewTwo2.setText(context.getResources().getString(text2));                }            }        }    }}<strong></strong>
    这里使用了Holder帮助类进行统一的ViewStub进行封装。同时使用了上述的ViewStub的特点,重复加载时抛出非法状态异常(IllegalStateException),此时直接利用可见性进行显示。在finally块中实现UI与逻辑操作,类似于一个适配器,将对ViewStub的所有操作逻辑都封在对应的加载方法中。这里我还使用注解的形式对参数进行判定,之后或许简单的总结总结。

   效果展示:点击展示对应的,并隐藏另一个。

    

    

    到此ViewStub动态加载布局优化性能到此结束。

    命名控件简单使用:

    上面ViewStub的布局中已经使用到了tools命名空间

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    </span><strong><span style="font-size:18px;">xmlns:tools="http://schemas.android.com/tools"</span></strong><span style="font-size:14px;">    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        </span><span style="font-size:18px;"><strong>tools:text="第一个加载数据"</strong></span><span style="font-size:14px;">        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout></span>
    如上重点标注部分,tools命名空间主要用于在布局预览中展示内容,而在加载的时候并不加载布局,不浪费资源,达到提升性能的目的。也就是说 布局预览中可以看到 "第一个加载的数据" ,而运行在手机上则不会消耗内存,也不会显示。tools还有很多内容,这里不多说。

    

0 0