Android

Android中Activity转场动画的使用

2018-11-12  本文已影响40人  fengcz

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) {
        }
    }
<?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);
    }
上一篇下一篇

猜你喜欢

热点阅读