如何設定Android Activity間切換時的動畫

openkk 12年前發布 | 75K 次閱讀 Android Android開發 移動開發

本代碼示例介紹如何設定Activity間切換時的動畫效果。本示例使用Eclipse的Android工程編譯測試。

1. 定義清單文件(AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="my.android.test"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".Animation"

                  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=".Controls1"

                android:label="@string/app_name"

                android:theme="@android:style/Theme.Light">

        </activity>

    </application>

    <uses-sdk android:minSdkVersion="9" />

</manifest>

2. 定義字符串資源(res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="hello">Hello World,Aniation!</string>

    <string name="app_name">Animation</string>

    <string name="activity_animation_msg">Press a button to launch an activity with a custom animation.</string>

    <string name="activity_animation_fade">Fade in</string>

    <string name="activity_animation_zoom">Zoom in</string>

    <string name="controls_1_save">Save</string>

    <string name="controls_1_checkbox_1">Checkbox 1</string>

    <string name="controls_1_checkbox_2">Checkbox 2</string>

    <string name="controls_1_radiobutton_1">RadioButton 1</string>

    <string name="controls_1_radiobutton_2">RadioButton 2</string>

    <string name="controls_1_star">Star</string>

    <string name="textColorPrimary">textColorPrimary</string>

    <string name="textColorSecondary">textColorSecondary</string>

    <string name="textColorTertiary">textColorTertiary</string>

    <string name="listSeparatorTextViewStyle">listSeparatorTextViewStyle</string>

</resources>

3.  定義漸入動畫資源定義(res/anim/fade.xmlres/anim/hold.xml)

res/anim/fade.xml文件

<?xml version="1.0" encoding="utf-8"?>

<!-- 聲明動畫對象的透明度,本例使用漸入的方式,顯示Activity

            屬性說明參照zoom_exit.xml -->

<alpha xmlns:android="http://schemas.android.com/apk/res/android"

       android:interpolator="@android:anim/accelerate_interpolator"

       android:fromAlpha="0.0" android:toAlpha="1.0"

       android:duration="@android:integer/config_longAnimTime" />

res/anim/hold.xml文件

<?xml version="1.0" encoding="utf-8"?>

<!-- 聲明動畫對象的水平和垂直移動量,本例使用水平移動方式,

     android:interpolator:指定在設定時間內動畫移動過程中插補器,用于改善動畫的平滑度

     android:fromXDelta:指定動畫開始時,動畫對象的水平位置,可以用像素智設定,也可以用

             相對父窗口寬度的百分比來設定。

     android:toXDelta:指定動畫結束時,動畫對象的水平位置,可以用像素值來設定,也可以用

            相對父窗口寬度的百分比來設定。

     android:duration:指定動畫的播放時間

-->

<translate xmlns:android="http://schemas.android.com/apk/res/android"

       android:interpolator="@android:anim/accelerate_interpolator"

       android:fromXDelta="0" android:toXDelta="0"

       android:duration="@android:integer/config_longAnimTime" />

 

4. 定義縮放動畫資源定義(res/anim/zoom_enter.xmlres/anim/zoom_exit.xml)

res/anim/zoom_enter.xml文件

<?xml version="1.0" encoding="utf-8"?>

<!-- 聲明動畫對象進入屏幕時的動畫資源

     android:interpolator:指定在設定時間內動畫移動過程中插補器,用于改善動畫的平滑度

-->

<set xmlns:android="http://schemas.android.com/apk/res/android"

        android:interpolator="@android:anim/decelerate_interpolator">

    <!-- 聲明動畫對象進入屏幕時的縮放動畫,

                      屬性說明參照zoom_exit.xml -->

    <scale android:fromXScale="2.0" android:toXScale="1.0"

           android:fromYScale="2.0" android:toYScale="1.0"

           android:pivotX="50%p" android:pivotY="50%p"

           android:duration="@android:integer/config_mediumAnimTime" />

</set>

res/anim/zoom_exit.xml文件

<?xml version="1.0" encoding="utf-8"?>

<!-- 聲明Activity退出時使用的動畫資源

     android:interpolator:指定在設定時間內動畫移動過程中插補器,用于改善動畫的平滑度

     android:zAdjustment:允許再動畫播放期間,調整播放內容在Z軸方向的順序,normal0):真正播放的

            動畫內容保持當前的Z軸順序,top1):在動畫播放期間,強制把當前播放的內容放到其他內容的上面;

     bottom-1):在動畫播放期間,強制把當前播放的內容放到其他內容之下。

-->

<set xmlns:android="http://schemas.android.com/apk/res/android"

        android:interpolator="@android:anim/decelerate_interpolator"

        android:zAdjustment="top">

    <!-- 指定動畫對象的縮放因子和播放時間

        android:fromXScaleandroid:toXScale指定X軸的動畫開始和結束時的縮放因子

        android:fromYScaleandroid:toYScale指定Y軸的動畫開始和結束時的縮放因子

        android:pivoteX:在動畫對象被縮放時,X軸要保留的原始尺寸的百分比。

        android:pivoteY:在動畫對象被縮放時,Y軸要保留的原始尺寸的百分比。

        android:duration指定動畫的播放時間

     -->

    <scale android:fromXScale="1.0" android:toXScale=".5"

           android:fromYScale="1.0" android:toYScale=".5"

           android:pivotX="50%p" android:pivotY="50%p"

           android:duration="@android:integer/config_mediumAnimTime" />

    <!-- 定義動畫對象的透明度,該動畫在動畫縮放之后播放。

        android:fromAlpha:指定動畫初始時的透明度

        android:toAlpha:指定動畫結束時的透明度

        android:duratiion:指定動畫透明處理的執行時間

    -->

    <alpha android:fromAlpha="1.0" android:toAlpha="0"

            android:duration="@android:integer/config_mediumAnimTime"/>

</set>

 

5. 定義布局Activity布局資源(res/layout/activity_animation.xmlres/layout/controls_1.xml)

res/layout/activity_animation.xml文件

<?xml version="1.0" encoding="utf-8"?>

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"

    android:gravity="center_horizontal"

    android:layout_width="match_parent" android:layout_height="match_parent">

 

    <TextView

        android:layout_width="match_parent" android:layout_height="wrap_content"

        android:layout_weight="0"

        android:paddingBottom="4dip"

        android:text="@string/activity_animation_msg"/>

 

    <Button android:id="@+id/fade_animation"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:text="@string/activity_animation_fade">

        <requestFocus />

    </Button>

 

    <Button android:id="@+id/zoom_animation"

        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:text="@string/activity_animation_zoom">

    </Button>

</LinearLayout>

res/layout/controls_1.xml文件

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

    <LinearLayout

        android:orientation="vertical"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

   

        <Button android:id="@+id/button"

            android:text="@string/controls_1_save"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"/> 

        <EditText android:id="@+id/edit"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"/>        

        <CheckBox android:id="@+id/check1"

            android:paddingBottom="24sp"

            android:paddingTop="24sp"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/controls_1_checkbox_1" />  

        <CheckBox android:id="@+id/check2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/controls_1_checkbox_2" /> 

        <RadioGroup

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="vertical">  

           <RadioButton android:id="@+id/radio1"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="@string/controls_1_radiobutton_1" /> 

           <RadioButton android:id="@+id/radio2"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="@string/controls_1_radiobutton_2" />

        </RadioGroup>  

        <CheckBox android:id="@+id/star"

            style="?android:attr/starStyle"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/controls_1_star" />                          

        <ToggleButton android:id="@+id/toggle1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" /> 

        <ToggleButton android:id="@+id/toggle2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"            

        <Spinner android:id="@+id/spinner1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:drawSelectorOnTop="true"

        /> 

        <TextView

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="5dip"

            android:text="@string/textColorPrimary"

            android:textAppearance="?android:attr/textAppearanceLarge"

            android:focusable="true"

        />

        <TextView

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="5dip"

            android:text="@string/textColorSecondary"

            android:textAppearance="?android:attr/textAppearanceLarge"

            android:textColor="?android:attr/textColorSecondary"

            android:focusable="true"

        /> 

        <TextView

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="5dip"

            android:text="@string/textColorTertiary"

            android:textAppearance="?android:attr/textAppearanceLarge"

            android:textColor="?android:attr/textColorTertiary"

            android:focusable="true"

        />

        <TextView

            style="?android:attr/listSeparatorTextViewStyle"

            android:text="@string/listSeparatorTextViewStyle"

            android:layout_marginTop="5dip"

        />   

    </LinearLayout>

</ScrollView>

 

6. 創建Activity窗口(Animation.javaControls1.java

Animation.java文件

package my.android.test;

 

import android.app.Activity;

import android.os.Bundle;

import android.content.ComponentName;

import android.content.Intent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

publicclass Animationextends Activity {

    /** Activity在首次創建時調用這個方法 */

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //填充布局

        setContentView(R.layout.activity_animation);

        //查找漸入動畫啟動按鈕,并設置Click事件監聽器

        Button button = (Button)findViewById(R.id.fade_animation);

        button.setOnClickListener(mFadeListener);

        //查找縮放動畫啟動按鈕,并設置Click事件監聽器

        button = (Button)findViewById(R.id.zoom_animation);

        button.setOnClickListener(mZoomListener);

    }

   

    /**

     * 啟動一個新的Activity,新舊Activity之間切換時,新的Activity使用漸入

     * 的方式啟動。

     */

    private OnClickListenermFadeListener =new OnClickListener(){

    publicvoid onClick(View v){

        //啟動Controls1Activity

        startActivity(new Intent(Animation.this, Controls1.class));

        //startActivity()方法之后立即調用,設定新的Activity進入和

        //當前Activity退出時的動畫。

        overridePendingTransition(R.anim.fade, R.anim.hold);

    }

    };

   

    /**

     * 啟動一個新的Activity,新舊Activity之間切換時,采用縮放的方式啟動新的的Activity

     */

    private OnClickListenermZoomListener =new OnClickListener(){

    publicvoid onClick(View v){

        //啟動Controls1Activity

        startActivity(new Intent(Animation.this, Controls1.class));

        //startActivity()方法之后立即調用,設定新的Activity進入和

        //當前Activity退出時的動畫。

        overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);

    }

    };

}

Controls1.java文件

package my.android.test;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.Spinner;

import android.widget.ArrayAdapter;

 

publicclass Controls1extends Activity {

    //設定字符串數組,用于下拉列表中的單選項

    privatestaticfinal String[]mStrings = {

        "Mercury", "Venus","Earth", "Mars", "Jupiter","Saturn", "Uranus", "Neptune"

    };

   

    /**

     * Activity首次創建時,會調用這個方法。

     */

    @Override

    protectedvoid onCreate(Bundle savedInstanceState){

       super.onCreate(savedInstanceState);

       //填充布局

       setContentView(R.layout.controls_1);

       //查找布局中的下拉列表組件

       Spinner s1 = (Spinner)findViewById(R.id.spinner1);

       //用字符串數組初始數組適配器

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

             android.R.layout.simple_spinner_item,mStrings);

       //給適配器的下拉列表設置布局資源

       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

       //把字符數組適配器與下拉列表組件關聯在一起。

       s1.setAdapter(adapter); 

    }

}

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!