android動畫切換(淡入淡出效果)實例


實例效果:實現第一個Activity淡出,顯示第二個Activity

點擊按鈕后,5秒淡入第二個Activity

實現步驟:

1.創建項目animation,我選擇的版本是Android3.0,

Application Name: Animation

Package Name: zf.itcast.animation

Create Activity:MainActivity

2. 創建第二個Activity,實現Activity的切換效果,代碼如下

package zf.itcast.animation;

import android.app.Activity;
import android.os.Bundle;

public class OtherActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other);
    }
    
}

3. 定義OtherActivity的layout布局文件other.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#6600FF" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/new_activity" />

</LinearLayout>

4. 打開清單文件AndroidManifest.xml,完成對OtherActivity的配置

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".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=".OtherActivity"
            android:label="@string/new_window"
            ></activity>
    </application>

</manifest>

5. 在main.xml的主頁面中,添加“打開新Activity”按鈕,並且點擊事件調用MainActivity類中“openActivity”方法

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" 
        android:onClick="openActivity"/>

</LinearLayout>

6. 在類MainActivity中實現openActivity方法,打開新的Activity

package zf.itcast.animation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    /**
     * <p>功能:打開新的Activity</p>
     * @author 周楓
     * @date 2012-5-30
     * @param 
     * @return void
     */
    public void openActivity(View v){
        Intent intent = new Intent(this, OtherActivity.class);
        startActivity(intent);
        //屏幕動畫淡入淡出效果切換,調用anim文件夾中創建的enteralpha(進入動畫)和exitalpha(淡出動畫)兩個動畫(注意:兩個xml文件命名不能有大寫字母)
        //如果想定義其他動畫效果,只需要改變enteralpha和exitalpha兩個文件
        this.overridePendingTransition(R.anim.enteralpha,R.anim.exitalpha);
    }
}

7. 在“res”文件夾下創建“anim”文件夾,其中再創建淡入淡出動畫效果文件,分別為enteralpha.xml和exitalpha.xml(注意:文件名只能為小寫a-z,數字0-9和下划線),具體為:

其中,enteralpha.xml為

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
    <!-- 動畫效果  從看不見到可以看見,也就是0到1,變幻時間為5000毫秒-->
    <alpha 
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="5000"
    />
</set>

exitalpha.xml為

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
    <!-- 退出動畫效果  從可以看見到不可以看見,也就是1.0到0,變幻時間為5000毫秒-->
    <alpha 
        android:fromAlpha="1.0"
        android:toAlpha="0"
        android:duration="5000"
    />
</set>

8. 因為文件中空間的text值使用string.xml定義,所以string.xml文件為:

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

    <string name="hello">打開新Activity</string>
    <string name="app_name">Animation</string>
    <string name="new_window">新窗口</string>
    <string name="new_activity">這是新Activity</string>

</resources>

9. 功能完成,效果就去運行吧~~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM