美文网首页Android
Android中Activity转场动画的使用

Android中Activity转场动画的使用

作者: fengcz | 来源:发表于2018-11-12 22:59 被阅读40次

    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);
        }
    

    相关文章

      网友评论

        本文标题:Android中Activity转场动画的使用

        本文链接:https://www.haomeiwen.com/subject/jyehfqtx.html