Activity切换之overridePendingTransition初探

第一次见到此函数是在别人的代码中,于是很好奇此函数的作用。在网上找了很久在找到了这篇博文(http://blog.csdn.net/menxu_work/article/details/10258787)有一个比较基础的介绍,于是自己动手写了一个类似的代码,也算是有了一个大体的认识了。

代码中比较关键的地方是:

Intentintent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);

overridePendingTransition只能在startActivity和finish方法之后调用。网上都是这么说的,具体的原因还有待探究。
现在了写下个人对此函数的体会:
overridePendingTransition函数第一个参数,是将要打开的activity的动画过程,第二个参数是,将要退出的activity的动画过程,以本例为例在本activity中执行上面那段话,就是本activity是退出的,startActivity是将要打开的activity。所以,本activity就会执行退出的动画即(R.anim.out_to_left),startActivity就会执行打开的动画即(R.anim.in_from_right)至于动画(anim)的制作等下次详细介绍。
R.anim.out_to_lef是自己定义的动画,也可以调用系统的动画如:android.R.anim.slide_in_left等等
贴出部分代码:
MainActivity.java

public class MainActivity extends Activity {

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

        Button btn = (Button)findViewById(R.id.btnChange);
        btn.setOnClickListener(new OnClickListener() {

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

                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
//              overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
//              overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });
    }
}

SecondActivity.java

public class SecondActivity extends android.app.Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_main);

        Button btn = (Button)findViewById(R.id.btnChange);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(SecondActivity.this, MainActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
            }
        });
    }
}

这个是Activity切换时的代码,主要是onClick中的响应事件即为切换操作,记得在AndroidManifest.xml中声明

<activity android:name="com.pachong.myoverridepending.SecondActivity"></activity>

in_from_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="-100%"
        android:toXDelta="0%"
        android:duration="800"/>
</set>

out_to_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:duration="800"/>
</set>

in_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="100%"
        android:toXDelta="0%"
        android:duration="800"
        />
</set>

out_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="0%"
        android:toXDelta="-100%"
        android:duration="800"/>
</set>

这是anim的文件在res/anim目录下,如果没有需要主动创建

activity_main.xml

<LinearLayout 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"
    android:orientation="vertical"
    android:background="#ff00ff" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一个Activity" />

    <Button 
        android:id="@+id/btnChange"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="切换"/>

</LinearLayout>

second_main.xml

<LinearLayout 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"
    android:orientation="vertical"
    android:background="#ffff00" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个Activity" />

    <Button 
        android:id="@+id/btnChange"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="切换"/>

</LinearLayout>

这是layout布局,就不介绍了

以下是参考博文:
http://blog.csdn.net/menxu_work/article/details/10258787
http://blog.csdn.net/daodaowolf/article/details/8290902
http://www.tuicool.com/articles/zY3ieq
http://www.bkjia.com/Androidjc/837942.html

今天就讲到这里算是一个overridePendingTransition的入门吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Activity 切换效果通常可以使用 Android 系统提供的四种动画效果来实现,分别是:overridePendingTransitionActivityOptions、ViewAnimationUtils 和 Transition。具体来说: 1. overridePendingTransition:这是一种简单的 Activity 切换动画效果,可以在 startActivity 或者 finish 方法中调用,用于在当前 Activity 退出或者新的 Activity 进入时添加动画效果。该方法需要传入两个参数,分别是进入和退出时的动画效果。 2. ActivityOptions:这是一种比较灵活的 Activity 切换效果,可以实现一些特殊的切换动画效果,如共享元素动画等。该方法需要传入一个 Bundle 对象,用于指定要添加的动画效果。 3. ViewAnimationUtils:这是一种可以实现圆形揭示动画的效果,适用于 Android 5.0 及以上版本的系统。该方法需要传入一个 View 对象、开始和结束的圆形半径以及动画的持续时间。 4. Transition:这是一种比较复杂的 Activity 切换效果,可以实现多种动画效果,如淡入淡出、滑动、缩放等。该方法需要使用 Android 系统提供的 Transition 类实现,并且需要在 XML 文件中定义动画效果。使用时需要在 Activity 中调用 getWindow().setEnterTransition() 和 getWindow().setExitTransition() 方法来指定进入和退出时的动画效果。 以上四种方法都可以用于实现 Activity 切换效果,具体选择哪种方法可以根据实际需求和个人喜好来决定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值