標籤:android應用 效能最佳化
開發我們的安卓應用時,根據產品需求,可能需要在運行時根據一定的條件來動態顯某View或Layout。我們最常用的方法可能就是:把所有可能用到的View都寫在布局檔案中,先把不需要的View.setVisibility()參數設為View.GONE(不可見),然後在代碼中根據顯示的需要動態setVisibility(View.VISIBLE)。這樣做好處在於:邏輯簡單,控制靈活。缺點也很明顯:耗費資源。
更好的方法就是:使用ViewStub,它是一個輕量級的View:看不見的,不佔布局位置,佔用資源非常小的控制項。在我們的布局檔案中為ViewStub指定一個布局,在Inflate布局的時候,只有ViewStub會被初始化,然後當ViewStub被設定為可見的時候,或是調用了ViewStub.inflate()的時候,ViewStub所向的布局就會被Inflate和執行個體化,然後ViewStub的布局屬性都會傳給它所指向的布局。就可以使用ViewStub來方便的在運行時,是否要顯示出某個布局。
顯然ViewStub使用方便而且從效能上做了最佳化,不過它不可能是萬能的,說說ViewStub的一些特點:
1. ViewStub只能Inflate一次,之後ViewStub對象會被置為空白。意思是:某個被ViewStub指定的布局被Inflate後,就不會夠再通過ViewStub來控制它了,相當於我們在特定的情況下讓ViewStub中的布局可見了,就不能再讓其不可見。
2. ViewStub只能Inflate一個布局檔案,而不是某個具體的View(如TextView),當然可以把View寫在某個布局檔案中。
所以我們通常在以下情況下才考慮使用ViewStub:
1. 在程式的運行期間,某個布局在Inflate後,就不會有變化,除非重新啟動。
2. 想要控制顯示與隱藏的是一個布局檔案,而非某個View。
下面是一個很簡單的例子:
首先是含有ViewStub的而布局檔案layout.activity_result:
<?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" >
<RelativeLayout <!--初始化進入頁面就要顯示的布局-->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
<mageView
android:id="@+id/personal_page_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:src="@drawable/clear" />
<TextView
android:id="@+id/personal_page_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/personal_page_iv"
android:textColor="#333333"
android:textSize="14sp"
/>
<TextView
android:id="@+id/personal_page_id_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/personal_page_name_tv"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/personal_page_iv"
android:text="ID:"
android:textColor="#969696"
android:textSize="12sp" />
<TextView
android:id="@+id/personal_page_id_number_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/personal_page_name_tv"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/personal_page_id_tv"
android:text="8888888"
android:textColor="#969696"
android:textSize="12sp" />
<TextView
android:id="@+id/personal_page_total_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/personal_page_id_tv"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/personal_page_iv"
android:textColor="#969696"
android:textSize="12sp" />
<TextView
android:id="@+id/personal_page_total_number_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/personal_page_id_number_tv"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/personal_page_total_tv"
android:text="245"
android:textColor="#969696"
android:textSize="12sp" />
</RelativeLayout>
<ViewStub<!--進入頁面只有資料載入成功才要顯示的布局-->
android:id="@+id/stub_pay_result_success"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inflatedId="@+id/subTree"
android:layout="@layout/mystub_pay_result_fail" />
</LinearLayout>
然後我們在所在Activity的代碼中來控制ViewStub顯示:
Class MyActivity extends Activity{
ViewStub viewStub;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_result);
initViews();//初始化控制項,省略
getServerData();//擷取伺服器服務對應資料
}
private void getServerData(){
//如果擷取成功,比如成功的標誌是boolean flag=true;
if(flag){
viewStub = (ViewStub) findViewById(R.id.stub_pay_result_success);
View view = viewStub.inflate();
//然後再用findViewById()找到view中的控制項進行設定資料
}
}
}
這樣就可以在擷取成功的時候把資料顯示出來,如果擷取失敗則不會顯示資料,是不很簡單呢?
Android應用開發之效能最佳化1:ViewStub