Android Activity转场动画的使用

从MainActivity进入到SecondActivity,然后从SecondActivity返回到MainActivity这样一个过程中如何设置两个Activity创建和销毁的动画呢?(activity转场动画)

 

SDK中Activity类里面有设置动画的方法
/**
** @param enterAnim A resource ID of the animation resource to use for
     * the incoming activity.  Use 0 for no animation.
     * @param exitAnim A resource ID of the animation resource to use for
     * the outgoing activity.  Use 0 for no animation.
     */
*/
  public void overridePendingTransition(int enterAnim, int exitAnim) {
        try {
            ActivityManager.getService().overridePendingTransition(
                    mToken, getPackageName(), enterAnim, exitAnim);
        } catch (RemoteException e) {
        }
    }
  • 注意
    此方法的设置需要在启动新activity后也就是startActivity()之后调用,返回(销毁)时需要在finish()方法之后调用

  • 下面介绍使用方法
    overridePendingTransition(int enterAnim, int exitAnim)参数为0时无动画

    • step one:首先在res/下新建anim文件夹,新家4个动画
      in_from_left

 

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

in_from_right

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >

    <translate
        android:duration="300"
        android:fromXDelta="100%p"
        android:toXDelta="0%p" />
</set>

out_to_left

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >

    <translate
        android:duration="300"
        android:fromXDelta="0%p"
        android:toXDelta="-100%p" />
</set>

out_to_right

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >

    <translate
        android:duration="300"
        android:fromXDelta="0%p"
        android:toXDelta="100%p" />
</set>
  • 进场(启动进入)动画设置

 

        Intent intent = new Intent();
        intent.setClass(context, clazz);
        intent.putExtras(bundle);
        startActivity(intent);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
  • 出场(退出)动画设置

 

 finish();
 overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);

在实际开发中往往不需要在每个启动方法处都这样添加,一般都是写到基类中然后所有继承该基类的activity都拥有转场动画,如需修改也很方便

  • 如下

 

BaseActivity:
 public void startActivity(Class clazz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clazz);
        intent.putExtras(bundle);
        startActivity(intent);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
    }

    public void startActivity(Class clazz,) {
        Intent intent=new Intent();       
        intent.setClass(this, clazz);
        startActivity(intent);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
    }

    public void startActivityForResult( Class clazz, Bundle bundle, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, clazz);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
    }

 

 

 

 

 

一、淡入效果:

/anim/fade.xml内容如下:

 


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

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

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

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

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


/anim/hold.xml文件内容如下:

 


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

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

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

  4. android:fromXDelta="0" android:toXDelta="0"

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


调用在监听器中,像下面的代码:

 

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

当然Android本身也提供的有这个效果,调用如下格式:

 

overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);


二、缩小效果

/anim/zoom_enter.xml内容如下:

 


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

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

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

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

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

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

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

  8. </set>


/anim/zoom_exit.xml内容如下:

 


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

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

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

  4. android:zAdjustment="top">

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

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

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

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

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

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

  11. </set>

调用格式:

 

overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);  
上一篇:东芝成功研发出面向汽车驾驶员辅助系统的图像识别人工智能处理器ViscontiTM5的DNN硬件IP


下一篇:RN6752M转换视频信号接海思3519V101