Android中Activity之间的切换动画例子

Activity进行切换的时候可以加入一些炫酷的动画,
实现代码如下:

MainActivity:

package com.example.administrator.swichlayoutdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

import com.tandong.swichlayout.BaseEffects;
import com.tandong.swichlayout.SwitchLayout;

public class MainActivity extends Activity {
    private Button btn_ok, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7,
            btn_8, btn_9, btn_10, btn_11, btn_12, btn_13, btn_14;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        // 设置进入Activity的Activity特效动画,同理可拓展为布局动画
        SwitchLayout.getSlideFromBottom(this, false,
                BaseEffects.getQuickToSlowEffect());
        // 三个参数分别为(Activity/View,是否关闭Activity,特效(可为空))
        initListener();
    }

    private void initListener() {
        btn_ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 0);
                MainActivity.this.startActivity(in);
            }
        });

        btn_1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 1);
                MainActivity.this.startActivity(in);
            }
        });
        btn_2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 2);
                MainActivity.this.startActivity(in);
            }
        });
        btn_3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 3);
                MainActivity.this.startActivity(in);
            }
        });
        btn_4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 4);
                MainActivity.this.startActivity(in);
            }
        });
        btn_5.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 5);
                MainActivity.this.startActivity(in);
            }
        });
        btn_6.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 6);
                MainActivity.this.startActivity(in);
            }
        });

        btn_7.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 7);
                MainActivity.this.startActivity(in);
            }
        });
        btn_8.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 8);
                MainActivity.this.startActivity(in);
            }
        });
        btn_9.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 9);
                MainActivity.this.startActivity(in);
            }
        });
        btn_10.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 10);
                MainActivity.this.startActivity(in);
            }
        });
        btn_11.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 11);
                MainActivity.this.startActivity(in);
            }
        });
        btn_12.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 12);
                MainActivity.this.startActivity(in);
            }
        });
        btn_13.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 13);
                MainActivity.this.startActivity(in);
            }
        });
        btn_14.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent in = new Intent(MainActivity.this, SecondActivity.class);
                in.putExtra("key", 14);
                MainActivity.this.startActivity(in);
            }
        });
    }

    private void initView() {
        btn_ok = (Button) this.findViewById(R.id.btn_ok);
        btn_1 = (Button) this.findViewById(R.id.btn_1);
        btn_2 = (Button) this.findViewById(R.id.btn_2);
        btn_3 = (Button) this.findViewById(R.id.btn_3);
        btn_4 = (Button) this.findViewById(R.id.btn_4);
        btn_5 = (Button) this.findViewById(R.id.btn_5);
        btn_6 = (Button) this.findViewById(R.id.btn_6);
        btn_7 = (Button) this.findViewById(R.id.btn_7);
        btn_8 = (Button) this.findViewById(R.id.btn_8);
        btn_9 = (Button) this.findViewById(R.id.btn_9);
        btn_10 = (Button) this.findViewById(R.id.btn_10);
        btn_11 = (Button) this.findViewById(R.id.btn_11);
        btn_12 = (Button) this.findViewById(R.id.btn_12);
        btn_13 = (Button) this.findViewById(R.id.btn_13);
        btn_14 = (Button) this.findViewById(R.id.btn_14);

    }
}
SecondActivity:

package com.example.administrator.swichlayoutdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;

import com.tandong.swichlayout.BaseEffects;
import com.tandong.swichlayout.SwichLayoutInterFace;
import com.tandong.swichlayout.SwitchLayout;

public class SecondActivity extends Activity implements SwichLayoutInterFace {
    private Intent intent;
    private ImageView iv_back;
    private int key = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initIntent();
        // 设置进入Activity的Activity特效动画,同理可拓展为布局动画
        setEnterSwichLayout();
    }

    private void initIntent() {
        intent = getIntent();
        key = intent.getExtras().getInt("key");
        iv_back = (ImageView) this.findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                setExitSwichLayout();
            }
        });
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {// 按返回键时退出Activity的Activity特效动画

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            setExitSwichLayout();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void setEnterSwichLayout() {
        switch (key) {
            case 0:
                SwitchLayout.get3DRotateFromLeft(this, false, null);
                // 三个参数分别为(Activity/View,是否关闭Activity,特效(可为空))
                break;
            case 1:
                SwitchLayout.getSlideFromBottom(this, false,
                        BaseEffects.getMoreSlowEffect());
                break;
            case 2:
                SwitchLayout.getSlideFromTop(this, false,
                        BaseEffects.getReScrollEffect());
                break;
            case 3:
                SwitchLayout.getSlideFromLeft(this, false,
                        BaseEffects.getLinearInterEffect());
                break;
            case 4:
                SwitchLayout.getSlideFromRight(this, false, null);
                break;
            case 5:
                SwitchLayout.getFadingIn(this);
                break;
            case 6:
                SwitchLayout.ScaleBig(this, false, null);
                break;
            case 7:
                SwitchLayout.FlipUpDown(this, false,
                        BaseEffects.getQuickToSlowEffect());
                break;
            case 8:
                SwitchLayout.ScaleBigLeftTop(this, false, null);
                break;
            case 9:
                SwitchLayout.getShakeMode(this, false, null);
                break;
            case 10:
                SwitchLayout.RotateLeftCenterIn(this, false, null);
                break;
            case 11:
                SwitchLayout.RotateLeftTopIn(this, false, null);
                break;
            case 12:
                SwitchLayout.RotateCenterIn(this, false, null);
                break;
            case 13:
                SwitchLayout.ScaleToBigHorizontalIn(this, false, null);
                break;
            case 14:
                SwitchLayout.ScaleToBigVerticalIn(this, false, null);
                break;
            default:
                break;
        }

    }

    @Override
    public void setExitSwichLayout() {
        switch (key) {
            case 0:
                SwitchLayout.get3DRotateFromRight(this, true, null);
                break;
            case 1:
                SwitchLayout.getSlideToBottom(this, true,
                        BaseEffects.getMoreSlowEffect());
                break;
            case 2:
                SwitchLayout.getSlideToTop(this, true,
                        BaseEffects.getReScrollEffect());
                break;
            case 3:
                SwitchLayout.getSlideToLeft(this, true,
                        BaseEffects.getLinearInterEffect());
                break;
            case 4:
                SwitchLayout.getSlideToRight(this, true, null);
                break;
            case 5:
                SwitchLayout.getFadingOut(this, true);
                break;
            case 6:
                SwitchLayout.ScaleSmall(this, true, null);
                break;
            case 7:
                SwitchLayout.FlipUpDown(this, true,
                        BaseEffects.getQuickToSlowEffect());
                break;
            case 8:
                SwitchLayout.ScaleSmallLeftTop(this, true, null);
                break;
            case 9:
                SwitchLayout.getShakeMode(this, true, null);
                break;
            case 10:
                SwitchLayout.RotateLeftCenterOut(this, true, null);
                break;
            case 11:
                SwitchLayout.RotateLeftTopOut(this, true, null);
                break;
            case 12:
                SwitchLayout.RotateCenterOut(this, true, null);
                break;
            case 13:
                SwitchLayout.ScaleToBigHorizontalOut(this, true, null);
                break;
            case 14:
                SwitchLayout.ScaleToBigVerticalOut(this, true, null);
                break;
            default:
                break;
        }

    }

}
 

其中强大的动画效果依赖于动画库SwitchLayout1.0.jar包

时间: 2024-09-16 10:24:59

Android中Activity之间的切换动画例子的相关文章

Android 中Activity 之间传递参数

Android 中Activity 之间传递参数 1.传递简单数据 在A Activity中 findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,TheActivity.class); // 对基础的数据类型进行传递 i.

从Android中Activity之间的通信说开来

最近两个星期在研究android的应用开发,学习了android应用开发的基础知识,基本控件,基本布局,基 本动画效果,数据存储,http访问internet等等基础知识. android中有一个概念,叫做activity.什么叫 做activity呢?中文译为[活动].我觉得类比到我们.NET里面的话,就好比是WinForm中的Form窗体,或者 是ASP.NET中的Page页面,一个activity可以对应一个窗体或者是页面. 既然是窗体.页面就需要通信,需 要传值,需要之间的变换. Win

android中Activity横竖屏切换的那些事

讲解之前需要说明的是 旋转屏幕:在系统的自动旋转屏幕开启的情况下,我们旋转屏幕 手动设置屏幕:我们自己去调用Activity的 setRequestedOrientation 方法. 设置屏幕的方向 简介 值 描述 unspecified 默认值.系统自动选择屏幕方向 behind 跟activity堆栈中的下面一个activity的方向一致 landscape 横屏方向,显示的宽比高长 portrait 竖屏方向,显示的高比宽长 sensor 由设备的物理方向传感器决定,如果用户旋转设备,这屏

Android利用Theme自定义Activity间的切换动画_Android

大多Android系统默认Activity间的动画切换效果为,右边滑入,左边滑出:有时候我们的需求可能是要求所有Activity的切换为淡入淡出的效果,这时候就可能需要改变一下默认的切换风格. 下面开始实现: 首先在res文件夹下建立anim文件夹,然后在里面建立fade_in.xml和fade_out.xml两个动画资源 fade_in.xml <?xml version="1.0" encoding="utf-8"?> <alpha xmlns

Android开发中Activity之间切换出现短暂黑屏的解决方法_Android

本文实例讲述了Android开发中Activity之间切换出现短暂黑屏的解决方法.分享给大家供大家参考,具体如下: 在默认情况下,Android应用程序启动时,会有一个黑屏的时期,原因是,首个activity会加载一些数据,比如初始化列表数据.向服务器发送请求获取数据等等.同样,使用startActivity(inte -- 在默认情况下,Android应用程序启动时,会有一个黑屏的时期,原因是,首个activity会加载一些数据,比如初 始化列表数据.向服务器发送请求获取数据等等.同样,使用s

Android开发中Activity之间切换出现短暂黑屏的解决方法

本文实例讲述了Android开发中Activity之间切换出现短暂黑屏的解决方法.分享给大家供大家参考,具体如下: 在默认情况下,Android应用程序启动时,会有一个黑屏的时期,原因是,首个activity会加载一些数据,比如初始化列表数据.向服务器发送请求获取数据等等.同样,使用startActivity(inte -- 在默认情况下,Android应用程序启动时,会有一个黑屏的时期,原因是,首个activity会加载一些数据,比如初 始化列表数据.向服务器发送请求获取数据等等.同样,使用s

【ANDROID游戏开发之五】游戏注册界面DEMO-实现两个ACTIVITY之间的切换与数据交互!

本站文章均为 李华明Himi 原创,转载务必在明显处注明:  转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/android-game/301.html ----------------------- 『很多童鞋说我的代码运行后,点击home或者back后会程序异常,如果你也这样遇到过,那么你肯定没有仔细读完Himi的博文,第十九篇Himi专门写了关于这些错误的原因和解决方法,这里我在博客都补充说明下,省的童鞋们总疑惑这一块:请点击下面联系进入阅读:

|||android中activity与fragment之间利用回调函数传递数据|||

问题描述 |||android中activity与fragment之间利用回调函数传递数据||| 最近开始学习android开发,碰上大问题,想实现这样一个效果,点击fragment中的一个button按钮,然后将这个按钮文本传递给activity,同时在activity剩余的布局中也创建一个相同的按钮,,不知道看懂我说的没? 在网上各种找资料说利用fragment向activity传递数据时需要在fragment创建一个回调接口,可是创建好了还是不知道怎么传递,主要问题在于activity中如

Android中实现“程序前后台切换效果”和“返回正在运行的程序,而不是一个新Activity”

ANDROID 一.首先是返回正在运行的程序,而不是新的ACTIVITY. 多网上关于 通知栏的例子都是打开一个新的Activity,代码也很多.根据那些代码如下    public void shownotification(String tab)    {        NotificationManager barmanager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);        Notif