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. 功能完成,效果就去运行吧~~~

原文地址:https://www.cnblogs.com/cczhoufeng/p/2526527.html