1. 程式人生 > >Android的四個動畫特效

Android的四個動畫特效

Android的四個動畫分別是漸變透明,縮放尺寸,旋轉角度,移動方向等動畫組合,這些動畫可以在res資原始檔下中新建的anim資料夾下建立一些常用的動畫屬性檔案供自己呼叫。下面筆者用寫程式碼來說明四個動畫的執行效果,並動態的設定動畫各個引數屬性值,方便一些學習者測試,並窺視它的動畫細節,以便以掌握動畫特效。寫純程式碼實現動畫還能方便開發者打包jar.

特別宣告,請讀者務必具備一定的英語閱讀能力。

一,漸變動畫alpha

建立一個繼承Activity的漸變動畫頁面類AlphaActivity.java

public class AlphaActivity extends Activity {

	private ImageView spaceshipImage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_alpha);
		
		spaceshipImage = (ImageView) this.findViewById(R.id.imageView_alpha);
		((Button) this.findViewById(R.id.button_start_alpha)).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startAlpha();
			}
		});
	}
	
	/**
	 * Alpha漸變動畫,從透明到不透明,範圍值為百分比
	 */
	private void startAlpha() {
//		設定動畫開始時的透明度
		String text = ((EditText) this.findViewById(R.id.editText_from_alpha)).getText().toString();
		if(text.equals("")) text = "0.1";
		float fromAlpha = Float.valueOf(text);
//		設定動畫結束時的透明度
		text = ((EditText) this.findViewById(R.id.editText_to_alpha)).getText().toString();
		if(text.equals("")) text = "1.0";
		float toAlpha = Float.valueOf(text);
//		設定動畫時間
		text = ((EditText) this.findViewById(R.id.editText_duration_alpha)).getText().toString();
		if(text.equals("")) text = "1000";
		int duration = Integer.valueOf(text);
		
		Animation alphaAnimation = new AlphaAnimation(fromAlpha, toAlpha);
		alphaAnimation.setDuration(duration);
//		開始動畫
		this.spaceshipImage.startAnimation(alphaAnimation);
	}
}

下面是漸變alpha的佈局介面檔案activity_alpha.xml

<RelativeLayout 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=".AlphaActivity" >

    <TextView
        android:id="@+id/textView_alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Alpha 漸變透明度動畫" />

    <ImageView
        android:id="@+id/imageView_alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView_alpha"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:src="@drawable/test" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView_alpha"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:background="#667b7b7b"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="fromAlpha=" />

            <EditText
                android:id="@+id/editText_from_alpha"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0.1"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" >

            </EditText>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="toAlpha=" />

            <EditText
                android:id="@+id/editText_to_alpha"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="1.0"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="duration=" />

            <EditText
                android:id="@+id/editText_duration_alpha"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:hint="1000"
                android:inputType="number"
                android:maxLength="4"
                android:singleLine="true" >
            </EditText>

        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/button_start_alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="開始動畫" />

</RelativeLayout>

漸變動畫頁面效果圖:

二,縮放動畫scale

建立一個繼承Activity的縮放動畫介面類ScaleActivity.java

public class ScaleActivity extends Activity {

	private ImageView spaceshipImage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_scale);
		
		spaceshipImage = (ImageView) this.findViewById(R.id.imageView_scale);
		((Button) this.findViewById(R.id.button_start_scale)).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startScale();
			}
		});
	}

	private void startScale() {
		String text = ((EditText) this.findViewById(R.id.editText_fromx_scale)).getText().toString();
		if(text.equals("")) text = "0.1";
		float from_x_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_tox_scale)).getText().toString();
		if(text.equals("")) text = "1.0";
		float to_x_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_fromy_scale)).getText().toString();
		if(text.equals("")) text = "0.1";
		float from_y_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_toy_scale)).getText().toString();
		if(text.equals("")) text = "1.0";
		float to_y_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_pivotX_scale)).getText().toString();
		if(text.equals("")) text = "0";
		float pivotX_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_pivotY_scale)).getText().toString();
		if(text.equals("")) text = "0";
		float pivotY_scale = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_duration_scale)).getText().toString();
		if(text.equals("")) text = "1000";
		int duration = Integer.valueOf(text);
//		Animation scaleAnimation = new ScaleAnimation(from_x_scale, to_x_scale, from_y_scale, to_y_scale);
//		另外還可以設定伸縮模式pivotXType、pivotYType, 伸縮動畫相對於x,y 座標的開始位置pivotXValue、pivotYValue等。
		Animation scaleAnimation = new ScaleAnimation(from_x_scale, to_x_scale, from_y_scale, to_y_scale,
				Animation.RELATIVE_TO_SELF, pivotX_scale, Animation.RELATIVE_TO_SELF, pivotY_scale);
//		scaleAnimation.setRepeatCount(repeatCount)
		scaleAnimation.setDuration(duration);
		this.spaceshipImage.startAnimation(scaleAnimation);
	}
}

下面是縮放動畫scale的佈局檔案activity_acale.xml

<RelativeLayout 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=".ScaleActivity" >

    <TextView
        android:id="@+id/textView_scale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Scale 縮放動畫" />

    <Button
        android:id="@+id/button_start_scale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="開始動畫" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView_scale"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="#667b7b7b"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="fromXScale=" />

            <EditText
                android:id="@+id/editText_fromx_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0.1"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" >

                <requestFocus />
            </EditText>

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="toXScale=" />

            <EditText
                android:id="@+id/editText_tox_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="1.0"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="fromYScale=" />

            <EditText
                android:id="@+id/editText_fromy_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0.1"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="toYScale=" />

            <EditText
                android:id="@+id/editText_toy_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="1.0"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pivotX=" />

            <EditText
                android:id="@+id/editText_pivotX_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0%"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="pivotY=" />

            <EditText
                android:id="@+id/editText_pivotY_scale"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0%"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="duration=" />

            <EditText
                android:id="@+id/editText_duration_scale"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:hint="1000"
                android:inputType="number"
                android:maxLength="4"
                android:singleLine="true" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView_scale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView_scale"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:src="@drawable/test" />

</RelativeLayout>

縮放動畫頁面效果圖

三,旋轉動畫rotate

建立一個繼承Activity的旋轉動畫介面類RotateActivity.java

public class RotateActivity extends Activity {

	private ImageView spaceshipImage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_rotate);
		
		spaceshipImage = (ImageView) this.findViewById(R.id.imageView_rotate);
		((Button) this.findViewById(R.id.button_start_rotate)).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startScale();
			}
		});
	}
	
	private void startScale() {
		String text = ((EditText) this.findViewById(R.id.editText_start_rotate)).getText().toString();
		if(text.equals("")) text = "0";
		float start_degree_rotate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_end_rotate)).getText().toString();
		if(text.equals("")) text = "360";
		float end_degree_rotate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_pivotX_rotate)).getText().toString();
		if(text.equals("")) text = "0";
		float pivotX_rotate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_pivotY_rotate)).getText().toString();
		if(text.equals("")) text = "0";
		float pivotY_rotate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_duration_rotate)).getText().toString();
		if(text.equals("")) text = "1000";
		int duration = Integer.valueOf(text);
//		Animation rotateAnimation = new RotateAnimation(start_degree_rotate, end_degree_rotate);
//		另外還可以設定伸縮模式pivotXType、pivotYType, 伸縮動畫相對於x,y 座標的開始位置pivotXValue、pivotYValue等。
		Animation rotateAnimation = new RotateAnimation(start_degree_rotate, end_degree_rotate,
				Animation.RELATIVE_TO_SELF, pivotX_rotate, Animation.RELATIVE_TO_SELF, pivotY_rotate);
		rotateAnimation.setDuration(duration);
		this.spaceshipImage.startAnimation(rotateAnimation);
	}
}

下面是旋轉動畫rotate的佈局檔案activity_rotate.xml

<RelativeLayout 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=".RotateActivity" >

    <TextView
        android:id="@+id/textView_rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Rotate 旋轉動畫" />

    <Button
        android:id="@+id/button_start_rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="開始動畫" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView_rotate"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="#667b7b7b"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="fromDegrees=" />

            <EditText
                android:id="@+id/editText_start_rotate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0'"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" >

                <requestFocus />
            </EditText>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:text="toDegrees=" />

            <EditText
                android:id="@+id/editText_end_rotate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="360'"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pivotX=" />

            <EditText
                android:id="@+id/editText_pivotX_rotate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0%"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="pivotY=" />

            <EditText
                android:id="@+id/editText_pivotY_rotate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0%"
                android:inputType="numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="duration=" />

            <EditText
                android:id="@+id/editText_duration_rotate"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:hint="1000"
                android:inputType="number"
                android:maxLength="4"
                android:singleLine="true" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView_rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView_rotate"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:src="@drawable/test" />

</RelativeLayout>

旋轉動畫頁面效果圖

四,位移動畫translate

建立一個繼承Activity的位移動畫介面類TranslateActivity.java

public class TranslateActivity extends Activity {

	private ImageView spaceshipImage;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_translate);
		
		spaceshipImage = (ImageView) this.findViewById(R.id.imageView_translate);
		((Button) this.findViewById(R.id.button_start_translate)).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startScale();
			}
		});
	}

	private void startScale() {
		String text = ((EditText) this.findViewById(R.id.editText_fromx_translate)).getText().toString();
		if(text.equals("")) text = "0.1";
		float from_x_translate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_tox_translate)).getText().toString();
		if(text.equals("")) text = "100";
		float to_x_translate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_fromy_translate)).getText().toString();
		if(text.equals("")) text = "0.1";
		float from_y_translate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_toy_translate)).getText().toString();
		if(text.equals("")) text = "100";
		float to_y_translate = Float.valueOf(text);
		text = ((EditText) this.findViewById(R.id.editText_duration_translate)).getText().toString();
		if(text.equals("")) text = "1000";
		int duration = Integer.valueOf(text);
		Animation translateAnimation = new TranslateAnimation(from_x_translate, to_x_translate, from_y_translate, to_y_translate);
		translateAnimation.setDuration(duration);
		this.spaceshipImage.startAnimation(translateAnimation);
	}
}

下面是位移動畫translate的佈局檔案activity_translate.xml

<RelativeLayout 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=".TranslateActivity" >

    <TextView
        android:id="@+id/textView_translate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Translate 位移動畫" />

    <Button
        android:id="@+id/button_start_translate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="開始動畫" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView_translate"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="#667b7b7b"
        android:orientation="vertical"
        android:padding="10dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="fromXDelta=" />

            <EditText
                android:id="@+id/editText_fromx_translate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0.1"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" >

                <requestFocus />
            </EditText>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:text="toXDelta=" />

            <EditText
                android:id="@+id/editText_tox_translate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="100"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="fromYDelta=" />

            <EditText
                android:id="@+id/editText_fromy_translate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="0.1"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="toYDelta=" />

            <EditText
                android:id="@+id/editText_toy_translate"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:hint="100"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="3"
                android:singleLine="true" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="duration=" />

            <EditText
                android:id="@+id/editText_duration_translate"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:hint="1000"
                android:inputType="number"
                android:maxLength="4"
                android:singleLine="true" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView_translate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView_translate"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:src="@drawable/test" />

</RelativeLayout>

位移動畫頁面效果圖

五,多頁卡的主頁面

建立一個有四個選項卡的頁卡主頁面MainActivity.java,繼承與TabActivity

public class MainActivity extends TabActivity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
//		漸變動畫一
		TabHost tabHost = this.getTabHost();
		TabSpec tab1 = tabHost.newTabSpec("tab1");
		tab1.setIndicator("漸變");
		tab1.setContent(new Intent(this, AlphaActivity.class));
		tabHost.addTab(tab1);
//		縮放動畫二
		TabSpec tab2 = tabHost.newTabSpec("tab2");
		tab2.setIndicator("縮放");
		tab2.setContent(new Intent(this, ScaleActivity.class));
		tabHost.addTab(tab2);
//		旋轉動畫三
		TabSpec tab3 = tabHost.newTabSpec("tab3");
		tab3.setIndicator("旋轉");
		tab3.setContent(new Intent(this, RotateActivity.class));
		tabHost.addTab(tab3);
//		位移動畫四
		TabSpec tab4 = tabHost.newTabSpec("tab4");
		tab4.setIndicator("位移");
		tab4.setContent(new Intent(this, TranslateActivity.class));
		tabHost.addTab(tab4);
	}

}

下面是主頁面的main的佈局檔案activity_main.xml,其中tabHost,tabWidget元件id為自動設定不能修改

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

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >
            </FrameLayout>

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

            </TabWidget>

        </LinearLayout>
    </TabHost>

</RelativeLayout>

六,在AndrroidManifest.xml屬性檔案中新增上面的四個繼承與Activity類的屬性

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.animationtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.animationtest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="AlphaActivity"></activity>
        <activity android:name="ScaleActivity"></activity>
        <activity android:name="RotateActivity"></activity>
        <activity android:name="TranslateActivity"></activity>
    </application>

</manifest>

七,專案總算完成了,專案沒有報錯的話那安裝在模擬器中,執行介面按下開始動畫按鈕測試效果看看吧。

如果讀者還有什麼問題,歡迎留言。