ViewStub 是一個不可見的,大小爲0的View,最佳用途就是實現View的延遲加載,避免資源浪費,在須要的時候才加載Viewandroid
須要注意的是,加載view以後,viewstub自己就會被新加載進來的view替換掉app
效果圖:ide
(1)res/layout/main.xml實現:動畫
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/btn_add_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <ViewStub android:id="@+id/vs_btn" android:inflatedId="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_add_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <ViewStub android:id="@+id/vs_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
btn_xml和image_xml:spa
<?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/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是BUTTON" /> </LinearLayout>
<?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:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout>
(2)主Activity實現:code
package com.jqyd.viewstub; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewStub; import android.view.animation.AlphaAnimation; import android.view.animation.AnimationSet; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private Button btn_add_btn, btn_add_image; private ViewStub vs_btn, vs_image; private View view = null; private boolean bl_btn = true, bl_image = true; private AnimationSet animationSet; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_add_btn = (Button) findViewById(R.id.btn_add_btn); btn_add_image = (Button) findViewById(R.id.btn_add_image); vs_btn = (ViewStub) findViewById(R.id.vs_btn); vs_btn.setLayoutResource(R.layout.vs_btn);// 加載xml vs_image = (ViewStub) findViewById(R.id.vs_image); vs_image.setLayoutResource(R.layout.vs_image);// 加載xml btn_add_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if (bl_btn) { bl_btn = false; vs_btn.inflate(); Button btnView=(Button) findViewById(R.id.btn); // 使用ImageView的startAnimation方法執行動畫 btnView.startAnimation(getAnimationSet()); animationSet=null; } } }); btn_add_image.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if (bl_image) { vs_image.inflate(); bl_image = false; ImageView imageView=(ImageView) findViewById(R.id.iv); // 使用ImageView的startAnimation方法執行動畫 imageView.startAnimation(getAnimationSet()); animationSet=null; } } }); } private AnimationSet getAnimationSet(){ // 建立一個AnimationSet對象,參數爲Boolean型, // true表示使用Animation的interpolator,false則是使用本身的 animationSet = new AnimationSet(true); // 建立一個AlphaAnimation對象,參數從徹底的透明度,到徹底的不透明 AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); // 設置動畫執行的時間 alphaAnimation.setDuration(2000); // 將alphaAnimation對象添加到AnimationSet當中 animationSet.addAnimation(alphaAnimation); return animationSet; } }